Part 10 of 17 in Stacks & Queues  

Evaluate Postfix expression by phantom11

Problem statement

Now that you have seen postfix expressions so closely, you have become an expert evaluating them too. Your class has even gone on to give you a title for this - "The Evaluator" and now they expect you to evaluate their postfix expressions. However, you got worried seeing so many expressions at once. Therefore you decide to write a computer program to do your job.

Input & Output

The first line contains the number of postfix expressions T (≤ 50). Each of the next T lines contains a string S (|S| ≤ 100), the postfix expression you have to evaluate. The string is a valid postfix expression, and contains digits [0-9] and operators {+,-,*,/}. The operators have the following properties:

  • Multiplication(*) and Division(/) have the same precedence. Similarly, Addition(+) and Subtraction(-) have same precedence. But, + and / have higher precedence than + and -.
  • All operators have left to right associativity.
Note that each digit has to be considered as a separate operand. So "24+" has two operands 2 and 4 and one operator +.

For each string output one line, the answer to that expression. Make sure you round your answer to six decimal places.

Sample Input
Sample Output

To try out your code

Sign in

Sign up