[JAVA] Solving expressions 01-14-2013, 02:54 AM
#1
The following code I'm going to present had been designed by me quite sometime ago. It solves mathematical expression using BODMAS order of operation. I have implemented the code in GUI too, but I'm not sharing that part here for now.
I'm not sure if this is a very good algorithm, even though it took a real long time for me to make it work successfully.
NOTE : I did not check for errors in the above code. So please be careful![Biggrin Biggrin](https://sinister.ly/images/smilies/set/biggrin.png)
Thank you for reading
Code:
import java.io.*;
/** Purpose : To solve a mathematical expression using BODMAS order of operation
* Author : Solixious
* Modified By :
*/
class Solve
{
String s[]={"^","/","*","+","-"}; //used to solve the expression using BODMAS order
public boolean isMisc(String str)
{
return (str.startsWith("sin") || str.startsWith("cos") || str.startsWith("tan") || str.startsWith("log") || str.startsWith("asin") || str.startsWith("acos") || str.startsWith("atan"));
}
public float solveMisc(String str)
{
float result=0.0f;
if(str.startsWith("sin"))
{
result=(float)Math.sin(Math.toRadians(Float.parseFloat(str.substring(3,str.length()))));
}
else if(str.startsWith("cos"))
{
result=(float)Math.sin(Math.toRadians(90.0f-Float.parseFloat(str.substring(3,str.length()))));
}
else if(str.startsWith("tan"))
{
result=(float)(Math.sin(Math.toRadians(Float.parseFloat(str.substring(3,str.length()))))/Math.sin(Math.toRadians(90.0f-Float.parseFloat(str.substring(3,str.length())))));
}
else if(str.startsWith("log"))
{
result=(float)Math.log(Float.parseFloat(str.substring(3,str.length())));
}
else if(str.startsWith("asin"))
{
result=(float)Math.toDegrees(Math.asin(Float.parseFloat(str.substring(4,str.length()))));
}
else if(str.startsWith("acos"))
{
result=(float)Math.toDegrees(Math.acos(Float.parseFloat(str.substring(4,str.length()))));
}
else if(str.startsWith("atan"))
{
result=(float)Math.toDegrees(Math.atan(Float.parseFloat(str.substring(4,str.length()))));
}
return result;
}
public String checkMisc(String str)
{
str=str.replace(" ","");
int lastindex=0;
for(int i=0;i<str.length();i++)
{
String c=String.valueOf(str.charAt(i));
if(c.equals("+") || c.equals("-") || c.equals("*") || c.equals("/") || c.equals("^") || i==str.length()-1)
{
if(isMisc(str.substring(lastindex,i)))
{
if(i==str.length()-1)
{
str=str.replace(str.substring(lastindex,str.length()),String.valueOf(solveMisc(str.substring(lastindex,str.length()))));
}
else
{
str=str.replace(str.substring(lastindex,i),String.valueOf(solveMisc(str.substring(lastindex,i))));
str=checkMisc(str);
}
break;
}
lastindex=i+1;
}
}
return str;
}
public float calc(String str) //used to solve binary string expressions of +, -, *, /, and ^
{
float a,b,result=0.0f;
if(!str.replace("+","").equals(str))
{
a=Float.parseFloat(str.substring(0,str.indexOf("+")));
b=Float.parseFloat(str.substring(str.indexOf("+")+1,str.length()));
result=a+b;
}
else if(!str.replace("-","").equals(str))
{
a=Float.parseFloat(str.substring(0,str.indexOf("-")));
b=Float.parseFloat(str.substring(str.indexOf("-")+1,str.length()));
result=a-b;
}
else if(!str.replace("*","").equals(str))
{
a=Float.parseFloat(str.substring(0,str.indexOf("*")));
b=Float.parseFloat(str.substring(str.indexOf("*")+1,str.length()));
result=a*b;
}
else if(!str.replace("/","").equals(str))
{
a=Float.parseFloat(str.substring(0,str.indexOf("/")));
b=Float.parseFloat(str.substring(str.indexOf("/")+1,str.length()));
result=a/b;
}
else if(!str.replace("^","").equals(str))
{
a=Float.parseFloat(str.substring(0,str.indexOf("^")));
b=Float.parseFloat(str.substring(str.indexOf("^")+1,str.length()));
result=(float)Math.pow(a,b);
}
return result;
}
public float solveExp(String str) //Used to solve complete expressions which do not have any brackets in them
{
float result=0.0f;
int a=0,b=0,c=0;
String sign="";
for(int t=0;t<5;t++) // Will solve expression using BODMAS order of operation
{
sign=s[t];
if(!str.replace(sign,"").equals(str))
{
b=str.indexOf(sign);
while(b!=-1)
{
for(int i=b-1;;i--)
{
if(i==0)
{
a=0;
break;
}
else
{
String ch=String.valueOf(str.charAt(i));
if(ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/") || ch.equals("^"))
{
a=i+1;
break;
}
}
}
for(int i=b+1;;i++)
{
if(i==str.length())
{
c=str.length();
break;
}
else
{
String ch=String.valueOf(str.charAt(i));
if(ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/") || ch.equals("^"))
{
c=i;
break;
}
}
}
str=str.replace(str.substring(a,c),String.valueOf(calc(str.substring(a,c))));
b=str.indexOf(sign,b);
}
}
}
result=Float.parseFloat(str);
return result;
}
public float solveEq(String str)
{
float result=0.0f;
int a=0,b=0,flag=1;
str="("+str+")"; // The given expression is put within brackets
while(flag==1) // All the expressions in all the brackets are solved one by one
{
for(int i=0;i<str.length();i++)
{
String c=String.valueOf(str.charAt(i));
if(c.equals("("))
{
a=i;
}
else if(c.equals(")"))
{
b=i;
if(i==str.length()-1)
{
flag=0;
}
break;
}
else
{
continue;
}
}
str=str.replace(str.substring(a,b+1),String.valueOf(solveExp(checkMisc(str.substring(a+1,b)))));
}
result=Float.parseFloat(str);
return result;
}
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="";
System.out.println("Enter a mathematical expression (e.g. (6.9/3+2^3)/(7.8+8^4) ): ");
str=br.readLine();
str=str.toLowerCase();
Solve s=new Solve();
float result=s.solveEq(str);
System.out.println("Result : "+result);
}
catch(Exception e)
{}
}
}
I'm not sure if this is a very good algorithm, even though it took a real long time for me to make it work successfully.
NOTE : I did not check for errors in the above code. So please be careful
![Biggrin Biggrin](https://sinister.ly/images/smilies/set/biggrin.png)
Thank you for reading
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
Folow me on My YouTube Channel if you're into art.