Evaluate an expression represented by a String. Expression can contain parentheses, you can assume parentheses are well-matched. For simplicity, you can assume only binary operations allowed are +, -, *, and /. Arithmetic Expressions can be written in one of three forms:

In fix Notation: Operators are written between the operands they operate on, e.g. 3 + 4 .

Prefix Notation: Operators are written before the operands, e.g + 3 4

Postfix Notation: Operators are written after operands.

In fix Expressions are harder for Computers to evaluate because of the addional work needed to decide precedence. In fix notation is how expressions are written and recognized by humans and, generally, input to programs. Given that they are harder to evaluate, they are generally converted to one of the two remaining forms. A very well known algorithm for converting an infix notation to a post fix notation is Shunting Yard Algorithm by Edgar Dijkstra. This algorithm takes as input an Infi x Expression and produces a queue that has this expression converted to a post fix notation. Same algorithm can be modified so that it outputs result of evaluation of expression instead of a queue. Trick is using two stacks instead of one, one for operands and one for operators. Algorithm was described succinctly on http://www.cis.upenn.edu/ matuszek/cit594-2002/Assignments/5-expressions.htm, and is re-produced here. (Note that credit for succinctness goes to author of said page)

1. While there are still tokens to be read in,
   1.1 Get the next token.
   1.2 If the token is:
       1.2.1 A number: push it onto the value stack.
       1.2.2 A variable: get its value, and push onto the value stack.
       1.2.3 A left parenthesis: push it onto the operator stack.
       1.2.4 A right parenthesis:
         1 While the thing on top of the operator stack is not a 
           left parenthesis,
             1 Pop the operator from the operator stack.
             2 Pop the value stack twice, getting two operands.
             3 Apply the operator to the operands, in the correct order.
             4 Push the result onto the value stack.
         2 Pop the left parenthesis from the operator stack, and discard it.
       1.2.5 An operator (call it thisOp):
         1 While the operator stack is not empty, and the top thing on the
           operator stack has the same or greater precedence as thisOp,
           1 Pop the operator from the operator stack.
           2 Pop the value stack twice, getting two operands.
           3 Apply the operator to the operands, in the correct order.
           4 Push the result onto the value stack.
         2 Push thisOp onto the operator stack.
2. While the operator stack is not empty,
    1 Pop the operator from the operator stack.
    2 Pop the value stack twice, getting two operands.
    3 Apply the operator to the operands, in the correct order.
    4 Push the result onto the value stack.
3. At this point the operator stack should be empty, and the value
   stack should have only one value in it, which is the final result.

Following is Java implementation of above algorithm.

Java Programming:

[pastacode lang=”java” manual=”%2F*%20A%20Java%20program%20to%20evaluate%20a%20given%20expression%20where%20tokens%20are%20separated%20%0A%20%20%20by%20space.%0A%20%20%20Test%20Cases%3A%0A%20%20%20%20%20%2210%20%2B%202%20*%206%22%20%20%20%20%20%20%20%20%20%20%20%20—%3E%2022%0A%20%20%20%20%20%22100%20*%202%20%2B%2012%22%20%20%20%20%20%20%20%20%20%20—%3E%20212%0A%20%20%20%20%20%22100%20*%20(%202%20%2B%2012%20)%22%20%20%20%20%20%20—%3E%201400%0A%20%20%20%20%20%22100%20*%20(%202%20%2B%2012%20)%20%2F%2014%22%20—%3E%20100%20%20%20%20%0A*%2F%0Aimport%20java.util.Stack%3B%0A%20%0Apublic%20class%20EvaluateString%0A%7B%0A%20%20%20%20public%20static%20int%20evaluate(String%20expression)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20char%5B%5D%20tokens%20%3D%20expression.toCharArray()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20Stack%20for%20numbers%3A%20’values’%0A%20%20%20%20%20%20%20%20Stack%3CInteger%3E%20values%20%3D%20new%20Stack%3CInteger%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Stack%20for%20Operators%3A%20’ops’%0A%20%20%20%20%20%20%20%20Stack%3CCharacter%3E%20ops%20%3D%20new%20Stack%3CCharacter%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20tokens.length%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Current%20token%20is%20a%20whitespace%2C%20skip%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(tokens%5Bi%5D%20%3D%3D%20’%20′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Current%20token%20is%20a%20number%2C%20push%20it%20to%20stack%20for%20numbers%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(tokens%5Bi%5D%20%3E%3D%20’0’%20%26%26%20tokens%5Bi%5D%20%3C%3D%20’9′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20StringBuffer%20sbuf%20%3D%20new%20StringBuffer()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20There%20may%20be%20more%20than%20one%20digits%20in%20number%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(i%20%3C%20tokens.length%20%26%26%20tokens%5Bi%5D%20%3E%3D%20’0’%20%26%26%20tokens%5Bi%5D%20%3C%3D%20’9′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sbuf.append(tokens%5Bi%2B%2B%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20values.push(Integer.parseInt(sbuf.toString()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Current%20token%20is%20an%20opening%20brace%2C%20push%20it%20to%20’ops’%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(tokens%5Bi%5D%20%3D%3D%20′(‘)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ops.push(tokens%5Bi%5D)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Closing%20brace%20encountered%2C%20solve%20entire%20brace%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(tokens%5Bi%5D%20%3D%3D%20′)’)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(ops.peek()%20!%3D%20′(‘)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20values.push(applyOp(ops.pop()%2C%20values.pop()%2C%20values.pop()))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ops.pop()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Current%20token%20is%20an%20operator.%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(tokens%5Bi%5D%20%3D%3D%20’%2B’%20%7C%7C%20tokens%5Bi%5D%20%3D%3D%20’-‘%20%7C%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tokens%5Bi%5D%20%3D%3D%20’*’%20%7C%7C%20tokens%5Bi%5D%20%3D%3D%20’%2F’)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20While%20top%20of%20’ops’%20has%20same%20or%20greater%20precedence%20to%20current%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20token%2C%20which%20is%20an%20operator.%20Apply%20operator%20on%20top%20of%20’ops’%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20to%20top%20two%20elements%20in%20values%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(!ops.empty()%20%26%26%20hasPrecedence(tokens%5Bi%5D%2C%20ops.peek()))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20values.push(applyOp(ops.pop()%2C%20values.pop()%2C%20values.pop()))%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Push%20current%20token%20to%20’ops’.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ops.push(tokens%5Bi%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Entire%20expression%20has%20been%20parsed%20at%20this%20point%2C%20apply%20remaining%0A%20%20%20%20%20%20%20%20%2F%2F%20ops%20to%20remaining%20values%0A%20%20%20%20%20%20%20%20while%20(!ops.empty())%0A%20%20%20%20%20%20%20%20%20%20%20%20values.push(applyOp(ops.pop()%2C%20values.pop()%2C%20values.pop()))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Top%20of%20’values’%20contains%20result%2C%20return%20it%0A%20%20%20%20%20%20%20%20return%20values.pop()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Returns%20true%20if%20’op2’%20has%20higher%20or%20same%20precedence%20as%20’op1’%2C%0A%20%20%20%20%2F%2F%20otherwise%20returns%20false.%0A%20%20%20%20public%20static%20boolean%20hasPrecedence(char%20op1%2C%20char%20op2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(op2%20%3D%3D%20′(‘%20%7C%7C%20op2%20%3D%3D%20′)’)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20if%20((op1%20%3D%3D%20’*’%20%7C%7C%20op1%20%3D%3D%20’%2F’)%20%26%26%20(op2%20%3D%3D%20’%2B’%20%7C%7C%20op2%20%3D%3D%20′-‘))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20method%20to%20apply%20an%20operator%20’op’%20on%20operands%20’a’%20%0A%20%20%20%20%2F%2F%20and%20’b’.%20Return%20the%20result.%0A%20%20%20%20public%20static%20int%20applyOp(char%20op%2C%20int%20b%2C%20int%20a)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20switch%20(op)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20case%20’%2B’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20a%20%2B%20b%3B%0A%20%20%20%20%20%20%20%20case%20′-‘%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20a%20-%20b%3B%0A%20%20%20%20%20%20%20%20case%20’*’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20a%20*%20b%3B%0A%20%20%20%20%20%20%20%20case%20’%2F’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(b%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20UnsupportedOperationException(%22Cannot%20divide%20by%20zero%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20a%20%2F%20b%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%20to%20test%20above%20methods%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(EvaluateString.evaluate(%2210%20%2B%202%20*%206%22))%3B%0A%20%20%20%20%20%20%20%20System.out.println(EvaluateString.evaluate(%22100%20*%202%20%2B%2012%22))%3B%0A%20%20%20%20%20%20%20%20System.out.println(EvaluateString.evaluate(%22100%20*%20(%202%20%2B%2012%20)%22))%3B%0A%20%20%20%20%20%20%20%20System.out.println(EvaluateString.evaluate(%22100%20*%20(%202%20%2B%2012%20)%20%2F%2014%22))%3B%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

22
212
1400
100

Categorized in: