Honestly, I was looking for an answer too. I wanted to make a calculator app so I was trying to turn a string directly into an operation but after looking on the internet for a few hours I found nothing so I ended up making the program myself and since I'm a beginner it wasn't easy. So here s my program it doesn't work with parenthesis or commas . You can only use the four operators "+ - * /"
but besides that, everything seems to be working.
It s pretty simple just start with the division by taking both numbers and the string that represent the operand then cut it into three string then do the operation with both numbers after that replace the operation with the result until there s no division left . After that do the same for multiplication , subtraction, and finally addition
here s a description of my functions
orderOp : will call the other function
getOp : get the string of the operation so if you gave it "2+4*5/6" you'll get 5/6
cutString: cut the string into three part so if you gave it 5/6 you'll get
a string array with ["5","/","6"] inside
Op: takes the the array of cutString and depending on the arr[1] so in this case "/" it does the math with
class Operateur{
private static final DecimalFormat decfor = new DecimalFormat("0.00");
public static String Op(String s){
String[] equation = cutString(s);
double sol = 0;
switch (equation[1]){
case "/":
sol = Double.parseDouble(equation[0])/Double.parseDouble(equation[2]);
break;
case "*":
sol = Double.parseDouble(equation[0])*Double.parseDouble(equation[2]);
break;
case "+":
sol = Double.parseDouble(equation[0])+Double.parseDouble(equation[2]);
break;
case "-":
sol = Double.parseDouble(equation[0])-Double.parseDouble(equation[2]);
break;
}
return sol+"";
}
public static String[] cutString(String s){
String[] arr = new String[0];
if(s.contains("+")){
arr = s.split("((?=[//+])|(?<=[//+]))");
}
if(s.contains("-")){
arr = s.split("((?=-)|(?<=-))");
}
if(s.contains("*")){
arr = s.split("((?=[//*])|(?<=[//*]))");
}
if(s.contains("/")){
arr = s.split("((?=[///])|(?<=[///]))");
}
return arr;
}
public static void orderOp(String equation){
while(equation.contains("/")){
equation = equation.replace(getOp(equation),(decfor.format(Double.parseDouble(Op(getOp(equation))))).replace(',', '.'));
}
System.out.println("Division :" +equation);
while(equation.contains("*")){
equation = equation.replace(getOp(equation),decfor.format(Double.parseDouble(Op(getOp(equation)))).replace(',', '.'));
}
System.out.println("Multiplication:" +equation);
while(equation.contains("+")){
equation = equation.replace(getOp(equation),Op(getOp(equation)));
}
System.out.println("addition:" +equation);
while(equation.contains("-")&& (equation.replaceAll("[^.]", "").length()>1)){
equation = equation.replace(getOp(equation),Op(getOp(equation)).replace(',', '.'));
equation = RemoveNegative(equation);
System.out.println(equation);
}
System.out.println("soustraction:" +equation);
}
public static String getOp(String s){
String r ="";
if(s.contains("/")){
int slash = s.indexOf("/");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&(s.charAt(last) != '-')&(s.charAt(last) != '*')&(s.charAt(last) != '/')&&(s.charAt(last) != '/')){//&(last >= s.length())
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("*")){
int slash = s.indexOf("*");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("+")){
int slash = s.indexOf("+");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("-")){
int slash = s.indexOf("-");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
return r;
}
public static String RemoveNegative(String s){
s = s.replace("-+","-");
s = s.replace("+-","-");
if(s.charAt(0) == '-'){
s = s.replaceFirst("-","");
s = s.replace("-","+");
while(s.contains("+")){
s = s.replace(getOp(s),Op(getOp(s)));
}
s = "-"+s;
}
return s;
}
}