HUNTER×HUNTER#10896

HUNTER×HUNTER

1.7 seconds
256 megabytes
Very hard

The simple architecture of the nanoprocessor Perfect Function (briefly PF) includes an input register IN and three integer registers A, B and C. IN is connected to the standard input, and the result from the calculation is expected in A. A non-negative integer and one of the characters +, -, * and = can be read in 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:

(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 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 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 Reg Is const 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 number R The content of R becomes equal to the non-negative integer number.

Copy IN R The content of IN is copied into R. An error occurs if there is a character in IN, and not a number.

Copy R_1\rangle R_2\rangle The content of R_1 is copied into R_2.

Add Reg R The content of Reg is added to the content of R. If there is a character in Reg, an error occurs.

Sub Reg R The content of Reg is subtracted from the content of R. If there is a character in Reg, an error occurs.

Mul Reg R 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 A.

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 A. 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 A 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 IN 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.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

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 .

For faster orientation, we show you below a sample program for PF (the indentation 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 2^{\pm y}:

ReadInt

Stop

If B is 0

ReadChar

Copy IN B

EndIf

ReadChar

Stop

Store 1 A

ReadInt

If IN is +

EndIf

Copy A C

Sub IN A

ReadInt

Add C C

If B is 0

ReadChar

Add IN A

Mul C A

ReadChar

Stop

ReadChar

Store 1 C

If IN is +

EndIf

Stop

Loop

ReadInt

Add A A

EndIf

Add IN A

Repeat

ReadInt

ReadChar

Sub C B

Sub IN A

Scoring

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.

Each test is evaluated separately. Only operations + and - or only operation * will occur in 30% of the tests.