import java.util.*; // for Stack public class Evaluate // class to evaluate an arithmetic expression consisting of int values, +, -, *, / and parens { // we should probably implement Excetion handling for wrong characters, that will be left to you to do public static void main(String[] args) { Scanner s=new Scanner(System.in); // for input String expr=s.nextLine(); // get the expression from the user char c, c2; // c = next character in expression and c2 = top of operator stack needed to determine operator precedence int value=0; // temporary computed value Stack operands=new Stack(); // two Stacks, one stores the int values as Integers Stack operators=new Stack(); // one stores the operators as Characters for(int i=0;i='0'&&c<='9') // if digit, push the digit as an Integer onto the operands Stack { int x=(int)c-48; // convert ASCII character to int value operands.push(new Integer(x)); } else if(c=='+'||c=='-') // + and - have lowest operator precedence, apply all other operations on operator stack first except for ( { while(!operators.empty()&&operators.peek()!='(') // remove all items off operator stack until it is empty or we reach a ( { c2=(char)operators.pop(); // get that operator value=process(c2,operands); // compute the value of top of operand stack -op- second of operand stack operands.push(new Integer(value)); // and push this temp computation back onto the stack } operators.push(new Character(c)); // once emptied out, we can push the new operator (+, -) onto the operator stack } else if(c=='*'||c=='/') { // if * or /, higher precedence while(operators.peek()=='*'||operators.peek()=='/') // we apply any * or / already on the stack first { c2=(char)operators.pop(); // pop off the * or / value=process(c2,operands); // apply the * or / to the top two of the operand stack operands.push(new Integer(value)); // and push the result back onto the stack } operators.push(new Character(c)); // now push our new * or / } else if(c=='(') operators.push(new Character(c)); // if ( then just push onto stack to start a higher level of precedence else if(c==')') { // but if ), then we have to pop off everything up to the corresponding ( while(operators.peek()!='(') // pop all operators off the stack until we reach ( { c2=(char)operators.pop(); // get the next operator value=process(c2,operands); // and apply it to the top two on the operand stack operands.push(new Integer(value)); // and push the result back onto the operand stack } operators.pop(); // pop off the ( } } System.out.println(expr + " = " + value); // when done, output the result } public static int process(char c, Stack s) // here, we get the operator and apply it to the top two Integers on the stack s { int x2=s.pop().intValue(); // notice that we apply the operator in opposite order of the top two on the stack (not important for + and * but is important for - and /) int x1=s.pop().intValue(); switch(c) { // and return the resulting int value to be converted back to an Integer and pushed onto s case '+': return x1+x2; case '-': return x1-x2; case '*': return x1*x2; case '/': return x1/x2; default: return 0; } } }