#K68697. Prefix Expression Evaluation
Prefix Expression Evaluation
Prefix Expression Evaluation
You are given one or more prefix expressions. A prefix expression is an arithmetic expression in which every operator precedes its operands. The operators allowed in this problem are addition (+), subtraction (-), multiplication (*), and integer division (/). Note that integer division is defined as \( a // b \) (i.e. the quotient after dividing, discarding any remainder).
For each prefix expression, evaluate the result.
Example:
- The prefix expression
+ 3 4
evaluates to \(3 + 4 = 7\). - The prefix expression
- * 10 20 30
is evaluated as \(10*20 = 200\) then \(200 - 30 = 170\).
You need to read multiple expressions and output the result of each one on a separate line.
inputFormat
The first line of input contains a single integer \(T\) indicating the number of prefix expressions. Each of the following \(T\) lines contains a valid prefix expression as a string. Operands are integers and tokens are separated by a space.
Example:
3 + 3 4 - * 10 20 30 / 100 5
outputFormat
For each input prefix expression, output its evaluated integer result on a separate line.
Example:
7 170 20## sample
3
+ 3 4
- * 10 20 30
/ 100 5
7
170
20
</p>