编程介的小学生 2017-03-20 15:29 采纳率: 20.5%
浏览 862
已采纳

PL/Cool

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
Here and are either variables or non-negative integer constants. After execution of the define operator, all occurences of the are replaced with the . 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

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

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

1

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

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-03-21 03:59
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序