ARITH2 - Simple Arithmetics II


While browsing aimlessly, Peter stumbled upon an old riddle he used to solve on his calculator when he was still young. It was the kind of a riddle where you punch in a bunch of numbers and operators into a simple pocket calculator and then turn it upside down to get the answer:

These come in many different sizes but they are always exactly one foot long. Answer: 103 * 103 * 5.

What are made of ice to keep people warm? Answer: 50 * 40 * 250 + 791.

After a few minutes he found a large amount of such riddles and full of excitement he went to solve them. He turned his computer screen upside down...

... only to find out that he does not have a reasonable calculator program installed on his computer.

Problem specification

You are given multiple sequences of button presses of a simple pocket calculator that consist of digits and arithmetic operators. For each such sequence find the number it would produce on a pocket calculator's display.

Note that the pocket calculator evaluates the operators in the order in which they are given. (i.e., there is no operator precedence.) Assume that the display of the calculator is large enough to show the result, and that its memory is sufficient to store all intermediate results.

Input specification

The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case represents one sequence of button presses for a pocket calculator. The sequence consists of non-negative integers and arithmetic operators and ends with an equal sign. It may also contain spaces to improve readability.

The operator / represents integer division, rounded down. You may assume that no test case contains division by zero and that in all test cases all intermediate results are non-negative.

Tip: long long int in C/C++, long in Java or int64 in Pascal is enough for this problem.

Output specification

For each sequence from the input file output the number that would be displayed on the calculator.

Example

Input:
4

1 + 1 * 2 =

29 / 5 =

103 * 103 * 5 =

50 * 40 * 250 + 791 =

Output:
4
5
53045
500791

Hint

The first test case shows that there is no operator precedence.

The second one shows that integer division always rounds down.

The last two outputs are the answers to the two riddles in the problem statement: "shoes" (53045 upside down), and "igloos"(500791 upside down).


hide comments
bhawna_123: 2017-07-06 12:58:28

got accepted even with int

sandeep_4141: 2017-06-11 17:20:43

easy stuff without string

soodan: 2017-04-08 07:31:14

guys this question can be done without taking input as a string , but as a challenge you can take input as a string and then solve it : -)

sunny: 2017-03-20 11:48:54

AC in one go :-).

nilabja16180: 2017-02-16 13:15:00

AC in one GO!

narutohokage_1: 2017-02-15 21:32:19

The Question is meant to be don't use string just take input directly with scanf.




wasted so much time on string but , as a challenge you can try to do with string also.

for solving this problem with string ------------
* every test case begin with empty line
* may have some space in the input or not
such as:-
12 + 3 -1= output: 14; or 12+3-1= output:14
* there may be a new line in input
such as
12+
3
-
1
=
output:14;
* division is integer division;
* got AC in both using long long or long long int

Last edit: 2017-02-15 21:32:39
splashishkumar: 2017-02-08 16:15:13

WOW...no string use ...AC in one go.. simple.... do calculation with input...

mukul arora: 2017-02-06 10:51:23

those who stuck with String and vector.
Don't use it. Think straightforward without worrying about spaces.

sahil_iiit: 2017-02-05 20:23:19

lolipop type.
very basic implementation.
no use of string. Just use three variable :- two for integer and one for char.

vengatesh15: 2017-01-22 17:58:54

nice one no need of string silly mistake cost me 1 WA


Added by:Fudan University Problem Setters
Date:2009-05-31
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO NODEJS PERL6
Resource:IPSC 2009