Question: rust Problem 2 . Stack Calculator There is a calculator that is based on a slightly modified stack, called a stack calculator. The stack calculator
rust
Problem Stack Calculator
There is a calculator that is based on a slightly modified stack, called a stack calculator. The stack calculator can only store numbers and perform the following operations. For convenience, the topmost number in the stack is referred to as the first number, the next as the second number, and so on in order.
NUM X: Push X onto the top of the stack X
POP: Remove the first number from the stack.
INV: Invert the sign of the first number eg
DUP: Duplicate the first number and push the copy onto the top of the stack.
SWP: Swap the first and second numbers on the stack.
ADD: Add the first and second numbers.
SUB: Subtract the first number from the second number second first
MUL: Multiply the first and second numbers.
DIV: Divide the second number by the first number and store the quotient. The second number is the dividend, and the first number is the divisor.
MOD: Divide the second number by the first number and store the remainder. The second number is the dividend, and the first number is the divisor.
In the case of binary operators, consider the first number to be the right operand, the second number the left one. Before performing the operation, both numbers are removed from the stack, and the result is stored back in the stack.
When there are insufficient numbers to perform an operation, when dividing by zero for DIV and MOD or when the absolute value of the result exceeds it results in a program error. If a program error occurs, the execution of the current program is halted, and no further commands are executed. To avoid ambiguity in negative division, the calculations are performed as follows: When there is a negative operand in the division, the absolute value of the number is taken before performing the division. Then, the
sign of the quotient and remainder is determined as follows: if only one of the operands is negative, the sign of the quotient is negative. In all other cases, the sign of the quotient is positive. The sign of the remainder is the same as the dividend. For example, DIVMOD and MOD
Input
The input consists of descriptions for multiple calculators. Each calculators description is divided into a program section and an input section.
The program section consists of commands, with each command on a separate line. Each command is a threeletter uppercase alphabetic string as specified in the problem description, and no other characters are provided. In the case of the command NUM, a number follows the command, which is an integer greater than or equal to and less than or equal to The NUM and the number are separated by a space. Each program ends with the command END.
The input section begins with the number of program executions, N N The next N lines each contain one input value Vi Vi Each input value is to be executed once by the program, and these executions are all independent. Each time the program is executed, the only value in the stack is the input value Vi
Descriptions for each machine are separated by a blank line. The appearance of QUIT indicates that there are no further machine descriptions. There will not be more than commands, and the stack will not store more than numbers during execution.
Output
For each input value, after executing the corresponding program, the output value should be printed. The output value is the number stored in the stack.
If a program error occurs, or if there is not exactly one number in the stack after all executions are completed, "ERROR" should be printed.
After printing all output values for each machine, a blank line should be printed.
Example
Input
DUP
MUL
NUM
ADD
END
NUM
NUM
ADD
END
NUM
ADD
END
QUIT
Output
ERROR
ERROR
ERROR
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
