PL/Cool

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

      The new IMB compiler "Unvisual Age for PL/Cool" is planned for release next month. Your task in this problem is to write the interpreter for the PL/Cool language so that the compiler programmers could test their product.

      Program on PL/Cool can contain expressions. Each expression can contain integer constants and variables. The following operations are allowed: + (add), - (substract), * (multiply), / (divide), % (modulo), and ^ (raise). All operations have their usual meaning and priority ([^] > [*] = [/] = [%] > [+] = [-]), all operations except ^ are evaluated from left to right, ^ is evaluated from right to left. Parenthesis can be used to change the order of evaluation, as usually. Unary minus and plus are allowed, their priority in this case is the highest.

      Divide operation acts like integer division: first both operands are replaced with their absolute values, next integer division is performed, remainder is dropped, and finally the sign of the quotient is set equal to the sign of the true quotient of the operands. Taking modulo is perfomed the same way, except that the quotient is dropped and remainder is kept. For example, (80 + 4 * 75)/(56 - 2^2^3) = -1.

      Program may contain variables. Variable name starts with a letter, which may be followed by letters and digits. Variable names are case insensitive. Maximal name length is 10 characters.

      PL/Cool has two operators: print and define. Program is the sequence of the operators, one on a line.

Operator print has the following syntax: print

Operator define has the following syntax:

define <operand1> <operand2>

      Here <operand1> and <operand2> are either variables or non-negative integer constants. After execution of the define operator, all occurences of the <operand1> are replaced with the <operand2>. Define operator can be used even to change the value of the integer constants, for example after

define 2 4

the value of 2 + 2 is 8.

      Note that substitution is performed recursively, until some undefined constant is met, for example the following sequence of operators prints 8 and 10:

define 2 4
define 3 2
print 3+3
define 4 5
print 3+3

      An attempt to redefine some variable or constant is ignored. An attempt to make a definition that leads to circular dependence is also ignored. For example, the following sequence prints 4 and 4, because the last two definitions are ignored.

define 2 4
define 3 2
define 2 5
print 3
define 4 2
print 3

      If some identifier is used in the expression that is not yet defined to be substituted by some integer constant, its value is set to zero. For example, the following operator prints 3:

print x + 3

Input

      Input file contains the program on PL/Cool. All numbers in the expressions, including numbers that occur during expression evaluation, do not exceed 109 by their absolute value. The total number of define operators does not exceed 30000, the total number of print operators does not exceed 2000, the length of each line does not exceed 200 characters.

      In case of division denominator is never zero, zero is not raised to zero power and power exponent is always non-negative.

Output

      Output file must contain the output of all print operators in the order they are executed, one on a line.

Sample Input

print (80+4*75)/(56-2^2^3)
print 30/-1
print 31%-5
print 0^3
print 2+2
define 2 3
print 2+2
define 3 x
print 2+2
define x 2
print 2+2
define 2 5
print 2+2
define x 7
print 2+2

Sample Output

-1
-30
-1
0
4
6
0
0
0
14

Source

Andrew Stankevich Contest 3

Manager

Information
Solved Number5
Submit Number34
Problem Tags
dsu
No tag edit access
温馨提示:AC后可以编辑标签哦. ^-^
Login
LoginCancel