Show Me The Operators
InterviewTime limit1sMemory limit128 MB
Evaluate each space-separated expression in two left-to-right sweeps and print the operators in execution order with the final value.
- Level
Easy2 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
A programming instructor wants his students to see the order in which the binary arithmetic operators +, -, *, / run inside an expression. Given an arithmetic expression, print the operators in the order they are executed under the rules below, then the final value of the expression.
Precedence rules:
- Multiplication and division have higher precedence than addition and subtraction.
- Multiplication and division have the same precedence. Between two of them, the one that appears first from the left of the expression is executed first.
- Addition and subtraction have the same precedence. Between two of them, the one that appears first from the left of the expression is executed first.
So one expression is evaluated in two sweeps from left to right. The first sweep executes every multiplication and division, and the second sweep executes every addition and subtraction.
Division is integer division and drops the fractional part, so 7 / 2 is 3. In the first sweep the left side of an operator is always at least 0 and the right side is always a positive operand, so the rounding direction never changes the result and no division by zero happens. In the second sweep an intermediate value may go negative: 3 - 5 is -2.
Input
The first line contains the number of test cases . Each of the next lines contains one arithmetic expression. An expression is a sequence of positive integer operands joined by the binary operators +, -, *, /, with one space between every operand and operator. An expression contains at most 10 operators.
Output
Print one line for each test case. The line starts with Case #x:, where x is the case number counted from 1. After that prefix, print the operators in execution order and then the final value of the expression, all separated by single spaces.