-->
当前位置:首页 > 题库

编程题:Simple Calculator

Luz4年前 (2022-09-07)题库402

![cal.jpg](~/f925cab7-06c4-410f-8116-907478d9ddde.jpg)


You are supposed to implement a simple calculator with a stack. As shown by the above figure, the calculator consists of two stacks: $$S_1$$ is for the numbers, and $$S_2$$ for the operators. At the bottom there is a "=" key. Whenever this key is pressed, the calculator will do the following operations:

- 1. Pop two numbers $$n_1$$ and $$n_2$$, in order, from $$S_1$$;
- 2. Pop an operator op from $$S_2$$;
- 3. Calculate $$n_2$$ op $$n_1$$;
- 4. Push the result back to $$S_1$$.

Keep doing until both stacks become empty, and then print the final result on the screen.

### Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer $$N$$ ($$1 < N \le 10^3$$), which is the amount of numbers in $$S_1$$.

Then $$N$$ integers are given in the next line. It is guaranteed that the absolute value of each number is no more than 100. The next line contains $$N-1$$ operators -- here we only consider +, -, *, and /. All the numbers or the characters in a line are separated by spaces.

### Output Specification:

First push the input numbers and operators into $$S_1$$ and $$S_2$$ in the given order, and then output the final result. Notice that only the integer part of the result will be maintained for every calculation. It is guaranteed that the absolute value of every number obtained during the calculations is no more than $$10^9$$.

If there is an illegal operation as to divide by zero, output in a line ERROR: X/0, where X is the numerator, and then end the process.

### Sample Input 1:
in
5
40 5 8 3 2
/ * - +


### Sample Output 1:
out
2


### Sample Input 2:
in
5
2 5 8 4 4
* / - +


### Sample Output 2:
out
ERROR: 5/0









答案:若无答案欢迎评论