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

PROGRAMMING:A simple calculator

Luz5年前 (2021-05-10)题库616
![ cal.jpg](~/7a19b25d-2a56-49dc-bbc4-698c8b1db505.jpg)
This problem requires you to design a simple calculator using stack execution for beginners of data structure. As shown in the figure above, the calculator consists of two stacks, one stack $$s_ 1 $$stores numbers, another stack $$s_ 2 $$store operator. There is an equal sign key at the bottom of the calculator. Each time you press this key, the calculator will perform the following operations:
1. From $$s_ 1 $, pop up two numbers in the order of $$n_ 1 $$and $$n_ 2$$;
1. From $$s_ 2. Pop up an operator op in $$;
1. Perform calculation $$n_ 2$$ op $$n_ 1$$;
1. Press the result back to $$s_ 1$$。
Until both stacks are empty, the calculation ends and the final result is displayed on the screen.
###Input format:
Enter the first line to give the positive integer $$n $$($$1 < n / Le 10 ^ 3 $$) as $$s_ The number of digits in 1 $$.
The second line gives $$n $$integers with absolute values not exceeding 100; The third line gives $$n-1 $$operators - only the four operations' + ',' - ',' * ',' / 'are considered here. Numbers and symbols in a row are separated by spaces.
###Output format:
Push the input numbers and operators onto the stack in the given order respectively $$s_ 1 $$and $$s_ 2 $$, the final result of the calculation will be output. Note that all calculations take only the integral part of the result. The absolute value of the middle and final results of the calculation should not exceed $$10 ^ 9 $.
If there is an illegal operation with zero denominator when performing division, output in one line: ` error: X / 0 ', where ` X' is the numerator at that time. Then end the program.
###Input sample 1:
```in
five
40 5 8 3 2
/ * - +
```
###Output sample 1:
```out
two
```
###Input example 2:
```in
five
2 5 8 4 4
* / - +
```
###Output example 2:
```out
ERROR: 5/0
```







answer:If there is no answer, please comment