import java.util.*;
public class ValuteExpression {
public static void main (String args[]){
String expression = "(3.0^(3.0^(1.0/4.0)*2.0))*(sqrt(13.0)*3.0^2.0+25.0-1.0)";
System.out.println("Risolvo l'espressione: '" + expression +"'");
expression = sanitize(expression);
expression = Function.computeAll(expression);
expression = compute(expression);
System.out.println(expression);
}
public static String sanitize(String expression){
if (expression.charAt(0)=='-' || expression.charAt(0)=='+')
expression="0.0"+expression;
return expression;
}
public static String compute(String expression){
//expression = sanitize(expression);
expression="("+expression+"+0.0)";
System.out.println(expression);
MathExpression MExp = new
MathExpression(expression);
Dominio dominio = MExp.internalBrackets();
while (MExp.operators.size() > 0 && MExp.numbers.size() >=2){
dominio = MExp.internalBrackets();
String subExpression = expression.substring(dominio.inizio(),dominio.fine()+1);
System.out.println(subExpression);
MathExpression MExp2 = new MathExpression(subExpression);
int n = MExp2.numPow();
for(int k = 0; k < n; k++) {
for(int j = 0; j < MExp2.numbers.size()-1; j++){
double num0 = MExp2.numbers.get(j).value();
double num1 = MExp2.numbers.get(j+1).value();
int pos0 = MExp2.numbers.get(j).position();
int pos1 = MExp2.numbers.get(j+1).position();
int posOP = MExp2.operators.get(j).position();
char typeOP = MExp2.operators.get(j).type();
if (typeOP==Operator.POTENZA){
if (posOP > pos0 && posOP < pos1){
String old = num0 + "^" + num1;
String result = String.valueOf(MathExpression.Potenza(num0, num1));
subExpression = subExpression.replace(old, result);
//System.out.println(result);
}
}
}
MExp2 = new MathExpression(subExpression);
}
n = MExp2.numMultiply();
for(int k = 0; k < n; k++) {
for(int j = 0; j < MExp2.numbers.size()-1; j++){
double num0 = MExp2.numbers.get(j).value();
double num1 = MExp2.numbers.get(j+1).value();
int pos0 = MExp2.numbers.get(j).position();
int pos1 = MExp2.numbers.get(j+1).position();
int posOP = MExp2.operators.get(j).position();
char typeOP = MExp2.operators.get(j).type();
if (typeOP==Operator.MULTIPLY){
if (posOP > pos0 && posOP < pos1){
String old = num0 + "*" + num1;
String result = String.valueOf(MathExpression.Moltiplica(num0, num1));
subExpression = subExpression.replace(old, result);
}
}
}
MExp2 = new MathExpression(subExpression);
}
n = MExp2.numDivide();
for(int k = 0; k < n; k++) {
for(int j = 0; j < MExp2.numbers.size()-1; j++){
double num0 = MExp2.numbers.get(j).value();
double num1 = MExp2.numbers.get(j+1).value();
int pos0 = MExp2.numbers.get(j).position();
int pos1 = MExp2.numbers.get(j+1).position();
int posOP = MExp2.operators.get(j).position();
char typeOP = MExp2.operators.get(j).type();
if (typeOP==Operator.DIVIDE){
if (posOP > pos0 && posOP < pos1){
String old = num0 + "/" + num1;
String result = String.valueOf(MathExpression.Dividi(num0, num1));
subExpression = subExpression.replace(old, result);
//System.out.println(subExpression);
}
}
}
MExp2 = new MathExpression(subExpression);
}
n = MExp2.numSum();
for(int k = 0; k < n; k++) {
for(int j = 0; j < MExp2.numbers.size()-1; j++){
double num0 = MExp2.numbers.get(j).value();
double num1 = MExp2.numbers.get(j+1).value();
int pos0 = MExp2.numbers.get(j).position();
int pos1 = MExp2.numbers.get(j+1).position();
int posOP = MExp2.operators.get(j).position();
char typeOP = MExp2.operators.get(j).type();
if (typeOP==Operator.SUM){
if (posOP > pos0 && posOP < pos1){
String old = num0 + "+" + num1;
String result = String.valueOf(MathExpression.Somma(num0, num1));
subExpression = subExpression.replace(old, result);
//System.out.println(subExpression);
}
}
}
MExp2 = new MathExpression(subExpression);
}
n = MExp2.numSubtract();
for(int k = 0; k < n; k++) {
for(int j = 0; j < MExp2.numbers.size()-1; j++){
double num0 = MExp2.numbers.get(j).value();
double num1 = MExp2.numbers.get(j+1).value();
int pos0 = MExp2.numbers.get(j).position();
int pos1 = MExp2.numbers.get(j+1).position();
int posOP = MExp2.operators.get(j).position();
char typeOP = MExp2.operators.get(j).type();
if (typeOP==Operator.SUBTRACT){
if (posOP > pos0 && posOP < pos1){
String old = num0 + "-" + num1;
String result = String.valueOf(MathExpression.Sottrai(num0, num1));
subExpression = subExpression.replace(old, result);
//System.out.println(subExpression);
}
}
}
MExp2 = new MathExpression(subExpression);
}
String left = pos(expression,0,dominio.inizio()-2);
String rigth = pos(expression,dominio.fine()+2,expression.length()-1);
expression = left+subExpression+rigth;
expression = sanitize(expression);
MExp = new MathExpression(expression);
}
return expression;
}
public static String pos(String s, int begin, int end){
String str = "";
for(int k=begin; k<=end; k++){
str += s.charAt(k);
}
return str;
}
}
class MathExpression {
int o = 0; int b = 0; int n = 0; int f=0;
Dominio dominio;
ArrayList <Number> numbers = new ArrayList<>();
ArrayList <Bracket> brackets = new ArrayList<>();
ArrayList <Operator> operators = new ArrayList<>();
ArrayList <Function> functions = new ArrayList<>();
private int numPotenza;
private int numMultiply;
private int numDivide;
private int numSum;
private int numSubtract;
String expression;
private void getNum(){
for(Operator op : operators) {
switch(op.type()) {
case Operator.POTENZA:
numPotenza++; break;
case Operator.MULTIPLY:
numMultiply++; break;
case Operator.DIVIDE:
numDivide++; break;
case Operator.SUM:
numSum++; break;
case Operator.SUBTRACT:
numSubtract++; break;
}
}
}
public int numPow(){
return numPotenza;
}
public int numMultiply(){
return numMultiply;
}
public int numDivide(){
return numDivide;
}
public int numSum(){
return numSum;
}
public int numSubtract(){
return numSubtract;
}
public void setExpression(String expression){
this.expression = expression;
}
public String getExpression(){
return this.expression;
}
MathExpression(String expression) {
scandisk(expression);
this.getNum();
}
Dominio internalBrackets(){
int j = brackets.size()-1;
char parentesi = brackets.get(j).type();
while (parentesi != Bracket.OPEN) {
j--; parentesi = brackets.get(j).type();
}
if (parentesi == Bracket.OPEN) {
if (brackets.get(j+1).type() == Bracket.CLOSE) {
dominio = new Dominio(brackets.get(j).position()+1,
brackets.get(j+1).position()-1);
}
}
return dominio;
}
void scandisk(String expression){
char c = expression.charAt(0);
int i = 0;
while(i<expression.length() && isMathValue(c)){
String numero = "";
while (isNumber(c) && i<expression.length()) { c = expression.charAt(i); if(isNumber(c)) {
numero += String.valueOf(c); i++; } }
if (numero!="") {
if (numero.indexOf(".")==-1) numero+=".0";
Double valore = Double.parseDouble(String.valueOf(numero));
Number number = new Number(valore, i-1); numbers.add(number); n++;}
//
while (isBracket(c) && i<expression.length()) { c = expression.charAt(i); if(isBracket(c)) {
Bracket bracket = new Bracket(c, i); brackets.add(bracket); i++; b++; } }
if (b > 0) b++;
//
while (isOperator(c) && i<expression.length()) { c = expression.charAt(i); if(isOperator(c)) {
Operator operator = new Operator(c, i); operators.add(operator); i++; o++; } }
if (o > 0) o++;
}
}
public static boolean isNumber(char c){
int ascii = (int)c;
if (ascii>=48 && ascii<=58 || c=='.')
return true; else return false;
}
public static boolean isLetter(char c){
int ascii = (int)c;
if (ascii>=65 && ascii<=91)
return true; else return false;
}
public static boolean isOperator(char c){
if (Operator.isValide(c))
return true; else return false;
}
public static boolean isBracket(char c){
if (c=='(' || c==')')
return true; else return false;
}
public static boolean isMathValue(char c){
if (isOperator(c) || isNumber(c) || isBracket(c))
return true; else return false;
}
public static double Potenza(double a, double b){
return Math.pow(a, b);
}
public static double Moltiplica(double a, double b){
return a * b;
}
public static double Dividi(double a, double b){
return a / b;
}
public static double Somma(double a, double b){
return a + b;
}
public static double Sottrai(double a, double b){
return a - b;
}
}
class Dominio {
private int inizio;
private int fine;
Dominio(int inizio, int fine){
this.inizio = inizio;
this.fine = fine;
}
public int inizio(){
return inizio;
}
public int fine(){
return fine;
}
}
enum Function {
sin, cos, sqrt;
public static String computeAll(String expression){
expression = "("+expression+")";
for (Function f : Function.values()) {
String name = f.toString();
//System.out.println(name);
while (expression.indexOf(name)>=0) {
String argomento = Function.getArg(expression, name);
String argomentoComputed =
ValuteExpression.compute(argomento);
String valueFunction = Function.compute(argomentoComputed, name);
expression = expression.replace(name+"("+argomento+")", valueFunction);
}
}
return expression;
}
public static String getArg(String expression, String name){
String[] NAME = expression.split(name);
int posNAME = expression.indexOf(name);
int begin = NAME[1].indexOf("(");
int end = NAME[1].indexOf(")");
String arg = ValuteExpression.pos(NAME[1], begin+1, end-1);
return arg;
}
public static String compute(String argomento, String name){
double dfncValue = Double.parseDouble(argomento);
switch(name){
case "sin":
dfncValue = Math.sin(dfncValue);
break;
case "cos":
dfncValue = Math.cos(dfncValue);
break;
case "sqrt":
dfncValue = Math.sqrt(dfncValue);
break;
}
String sfncValue = String.valueOf(dfncValue);
return sfncValue;
}
}
class Operator {
public final static char POTENZA = '^';
public final static char MULTIPLY = '*';
public final static char DIVIDE = '/';
public final static char SUM = '+';
public final static char SUBTRACT = '-';
private int position;
private char type;
Operator(char type, int postion){
set(type, postion);
}
public boolean set(char type, int position){
if (isValide(type)) {
this.type = type;
this.position = position;
return true;
}
else return false;
}
public int position(){
return position;
}
public char type(){
return type;
}
public static boolean isValide(char type){
boolean valide = false;
switch(type) {
case POTENZA:
valide = true; break;
case MULTIPLY:
valide = true; break;
case DIVIDE:
valide = true; break;
case SUM:
valide = true; break;
case SUBTRACT:
valide = true; break;
}
return valide;
}
}
class Number {
private int position;
private double value;
private String strvalue;
Number(double value, int position){
set(value, position);
}
Number(String value, int position){
set(value, position);
}
public boolean set(double value, int position){
this.value = value;
this.position = position;
return true;
}
public boolean set(String value, int position){
this.strvalue = strvalue;
this.position = position;
return true;
}
public int position(){
return position;
}
public double value(){
return value;
}
public String strvalue(){
return strvalue;
}
}
class Bracket {
public final static char OPEN = '(';
public final static char CLOSE = ')';
private int position;
private char type;
Bracket(char type, int postion){
set(type, postion);
}
public boolean set(char type, int position){
if (isValide(type)) {
this.type = type;
this.position = position;
return true;
}
else return false;
}
public int position(){
return position;
}
public char type(){
return type;
}
public static boolean isValide(char type){
boolean valide = false;
switch(type) {
case OPEN:
valide = true; break;
case CLOSE:
valide = true; break;
}
return valide;
}
}
About Online Java Compiler
Try our Online Java Compiler (Version OpenJDK 11.0.17) to Edit, Run, and Share your Java Code directly from your browser. This online development environment provides you the latest version OpenJDK 11.0.17.
How to use Online Java Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Java Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Java Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Java Compiler: Save and Share Java Code (Project)
Save Java Project Online
You can save your Java Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Java Project Online
You can use this feature to share your Java Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Java Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Java Compiler
There are several benefits of using the Online Java Compiler to run your Java code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.