Text parsing into Runtime Commands !
Hello All ..
I want to make a mini Matlab for mobile phones .. or you could name it a smart calculator ..
The base of such a project is the String command parsing ..
Assume the user is allowed to type:
x = 5
By text tokenizing by "=" .. I can get the variable name and value ..
if the variable name does not exist >> create it .. and update its value by '5' ..
This looks to me to be ok ..
But if the user enters:
y = x+3
it should do the same .. but here the right-hand side is an expression .. not a single value .. how can I evaluate it ..?
more tokenizing ?I dont know ..
ok .. trying to be more simple.. I i was able to replace all variables by their values .. so the previous expression became
y = 5 + 3
How can I evaluate the "5+3" string .. and get their numeric value=8 ?
Can any1 help ?
Thanks in advance !
Regards, Tarek
Attached is a java file that could compute regular arithmetic expression
n1[operator]n2[operator]n2[operator]... to test it in your command line enter
Calc 1+2+3+4+5-2 it takes the first line parameter as its function parameter.
Dont forget to compile first before running :)
Goodluck
import java.util.ArrayList;
public class Calc {
private float Compute(String str){
String token = "";
float result = 0;
ArrayList al = new ArrayList();
for (int x = 0; x < str.length();x++){
if ((str.charAt(x)!='+') &&
(str.charAt(x)!='-') &&
(str.charAt(x)!='*') &&
(str.charAt(x)!='/'))
{
token = token + str.charAt(x);
}else{
al.add(token);
al.add(str.substring(x,x+1));
token = "";
}
}
al.add(token);
// operate first on the first 2 operands
if ("+".equals(al.get(1))){
result = Float.valueOf(al.get(0).toString()).floatValue() +
Float.valueOf(al.get(2).toString()).floatValue();
}else
if ("+".equals(al.get(1))){
result = Float.valueOf(al.get(0).toString()).floatValue() +
Float.valueOf(al.get(2).toString()).floatValue();
}
else
if ("-".equals(al.get(1))){
result = Float.valueOf(al.get(0).toString()).floatValue() -
Float.valueOf(al.get(2).toString()).floatValue();
}
if ("*".equals(al.get(1))){
result = Float.valueOf(al.get(0).toString()).floatValue() *
Float.valueOf(al.get(2).toString()).floatValue();}
if ("/".equals(al.get(1))){
result = Float.valueOf(al.get(0).toString()).floatValue() /
Float.valueOf(al.get(2).toString()).floatValue();}
//proceed if multiple operation;
if (al.size()>3){
int x = 3;
while (x<al.size()){
if ("+".equals(al.get(x)))
result = result + Float.valueOf(al.get(x+1).toString()).floatValue();else
if ("-".equals(al.get(x)))
result = result - Float.valueOf(al.get(x+1).toString()).floatValue();else
if ("*".equals(al.get(x)))
result = result * Float.valueOf(al.get(x+1).toString()).floatValue();else
if ("/".equals(al.get(x)))
result = result / Float.valueOf(al.get(x+1).toString()).floatValue();
x = x + 2;
}
}
return result;
}
public Calc(String str){
System.out.println(str+ "=" + String.valueOf(Compute(str)));
}
public static void main(String[] args){
if (args.length==0){
System.out.println("Usage :Calc ><expression> in your command prompt");
System.out.println("<expression>=regular expression like 1*2+3/1");
}else
new Calc(args[0]);
}
}