The simple architecture of the nanoprocessor Perfect Function (briefly PF) includes an input register and three integer registers , and . is connected to the standard input, and the result from the calculation is expected in . A non-negative integer and one of the characters and can be read in from the standard input.
The elementary operations, which PF is able to perform, are shown in the table below.
The following notation is used:
- - any of the four registers;
- - any of the three integer registers , or
- - a non-negative integer;
- - any of the characters allowed, or a non-negative integer.
(You can notice that for the binary operations, which change a register, the changing register is the second parameter.)
Code of the operation | Action |
---|---|
ReadInt | A non-negative integer is read in from the standard input. An error occurs, if after cleaning up the delimiters, something else comes from the standard input. |
ReadChar | One of the characters +, -, * or = is read in from the standard input. An error occurs, if after cleaning up the delimiters, there is some other character or a number on the standard input. |
If Is | The beginning of a block to be executed if the content of Reg is equal to the constant const. An attempt to compare a character and a number leads to an error. |
EndIf | End of the If-block |
Store | The content of R becomes equal to the non-negative integer . |
Copy | The content of is copied into R. An error occurs if there is a character in , and not a number. |
Copy | The content of is copied into . |
Add | The content of Reg is added to the content of R. If there is a character in Reg, an error occurs. |
Sub | The content of Reg is subtracted from the content of R. If there is a character in Reg, an error occurs. |
Mul | The content of Reg is multiplied by the content of R. If there is a character in Reg, an error occurs. |
Repeat | Beginning of a cycle block |
Loop | End of the cycle block |
Stop | End of the calculation process. The result is the content of . |
In the beginning of the calculation all registers contain the number 0.
Task
Write a program for the processor PF to correctly calculate an arithmetic expression, containing non-negative integers and the arithmetic operations +
, -
and *
. The result should be obtained in register . By "correct", we mean to follow the accepted standard priority of operations: multiplication first, then addition and subtraction from left to right. Thus, the input 2 + 2 * 2 = should leave in result 6, not 8.
The input for your program is correct and will look as follows:
Numbers, alternatively followed by a character, are coming from the standard input to the register. This sequence begins with a number. All the numbers are non-negative integers. The character following each of them is one amongst +
, -
, \
and =
. Any non-empty set of spaces, tabs or new line symbols can serve as a delimiter between the members of the sequence. The end of the sequence is marked by reading the character =
. The characters +
, -
and *
denote the standard numerical operations: addition, subtraction, and multiplication, respectively.
Output
The problem is of "output-only" type. Send to the testing system a text file PF.txt, containing the program for PF-processor that you have coded, which solves the described problem. Each line of the file PF.txt should be a correct operation for the processor PF.
Constraints
The file PF.txt should not exceed 255 program lines and 64 KB in size. The calculated result of any subsequence of the given one, starting and ending at a number, is a positive or negative integer, or zero, but contains no more than 18 decimal digits.
Evaluation
Each test is evaluated separately. Only operations +
and -
or only operation *
will occur in 30% of the tests.
Interpreter
You are provided with an interpreter of programs for the nanoprocessor PF - the file PFI.cpp. It reads from the folder, where it is compiled, the PF.txt file, which represents a program for the PF-processor, and waits for data from the standard input. If trace mode is switched on (#define TRACE 1), the instructions are executed step by step, after each instruction the registers' content is shown on the standard output, and <ENTER> key is expected. On error, the process terminates with a corresponding message sent to the standard error device. Of course, you can redesign this interpreter as you see comfortable.
Example
Input
12 – 0 * 12345678 * 5 + 3 * 6 – 43 =
Output: The content of register A should be
-13
For faster orientation, we show you below a sample program for PF (the identation is also sample), which for a correctly input sequence of 4 members: a non-negative integer , a sign or , a non-negative integer and the sign calculates the expression :
ReadInt
Copy IN B
Store 1 A
Copy A C
If B is 0
ReadChar
If IN is +
ReadInt
Add IN A
ReadChar
Stop
EndIf
ReadInt
Sub IN A
ReadChar
Stop
EndIf
Add A A
Repeat
Sub C B
If B is 0
ReadChar
If IN is +
ReadInt
Add IN A
ReadChar
Stop
EndIf
ReadInt
Sub IN A
ReadChar
Stop
EndIf
Add C C
Mul C A
Store 1 C
Loop