The above code computes the value of a mathematical expression supplied at command line by using the concept of converting infix to postfix,then evaluating the postfix expression. Any comments are appreciated.
Mathematical Expression Evaluator
/**
No decimal or alphabetical inputs
Use # instead of ^ operator
Supply input as arguments without spaces
anuvab1911
*/
import java.util.regex.*;
import java.util.*;
class ExpEval{
public static int priority(String k){
switch(k){
case "#":
return 5;
case "/":
return 4;
case "*":
return 3;
case "+":
return 2;
case "-":
return 1;
}
return -1;
}
public static int eval(String a,String b,String op){
switch(op){
case "/":
return (Integer.parseInt(a)/Integer.parseInt(b));
case "*":
return (Integer.parseInt(a)*Integer.parseInt(b));
case "+":
return (Integer.parseInt(a)+Integer.parseInt(b));
case "-":
return (Integer.parseInt(a)-Integer.parseInt(b));
case "#":
return ((int)Math.pow(Integer.parseInt(a),Integer.parseInt(b)));
}
return -1;
}
public static void main(String args[]){
String input=args[0];
StringTokenizer alpha=new StringTokenizer(input,"+-*/#()");
StringTokenizer beta=new StringTokenizer(input,"1234567890");
LinkedList<String> infix=new LinkedList<String>();
while(alpha.hasMoreTokens()){
String operand=alpha.nextToken();
infix.add(operand);
try{
String operator=beta.nextToken();
infix.add(operator);
}
catch(Exception ex){
//System.out.println("Reached end of expression");
break;
}
}
ListIterator<String> gamma=infix.listIterator();
/*while(gamma.hasNext()){
System.out.println(gamma.next());
}*/
//System.out.println("====================");
LinkedList<String> infixOrig=new LinkedList<String>();
//gamma=infix.listIterator();
//System.out.println(infix+"\n"+infixOrig);
//The following splits any dual-operands into two
while(gamma.hasNext()){
String v=gamma.next();
if(v.length()>1){
try{
int num=Integer.parseInt(v);
infixOrig.add(v);
//System.out.println(v);
}
catch(NumberFormatException ex){
//System.out.println(v + " is an operand");
//Split into two operands
String a=v.substring(0,1),b=v.substring(1,2);
System.out.println(a + " " + b);
int curIndex=infix.indexOf(v);
//System.out.println(infixOrig.get(curIndex));
infixOrig.add(a);
infixOrig.add(b);
}
}
else{
infixOrig.add(v);
//System.out.println(v);
}
}
/*System.out.println("===================");
gamma=infixOrig.listIterator();
while(gamma.hasNext()){
System.out.println(gamma.next());
}*/
//Got the complete infix order,now convert to postfix
Stack<String> tempStack=new Stack<String>();
LinkedList<String> postfix=new LinkedList<String>();
tempStack.push("(");
infixOrig.add(")");
gamma=infixOrig.listIterator();
while(!tempStack.isEmpty()){
String element=gamma.next();
try{
Integer.parseInt(element);
postfix.add(element);
}
catch(NumberFormatException ex){
//An operator has been encountered
if(element.equals("("))
tempStack.push("(");
else if(element.equals(")")){
while(!tempStack.peek().equals("("))
postfix.add(tempStack.pop());
tempStack.pop();
}
else{
while(priority((String)tempStack.peek())>priority(element)){
postfix.add(tempStack.pop());
}
tempStack.push(element);
}
}
}
/*System.out.println("========================PF======================");
gamma=postfix.listIterator();
while(gamma.hasNext()){
System.out.println(gamma.next());
}
System.out.println("========================VAL======================");*/
postfix.add(")");
gamma=postfix.listIterator();
while(gamma.hasNext()){
String element=gamma.next();
//System.out.println("Element = " + element);
try{
int a=Integer.parseInt(element);
tempStack.push(Integer.toString(a));
}
catch(NumberFormatException ex){
//An operator has been encountered
if(element.equals(")"))
break;
String a=tempStack.pop();
String b=tempStack.pop();
tempStack.push(Integer.toString(eval(b,a,element)));
}
}
System.out.println("Final Value = " + tempStack.peek());
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.