Stack Machine Executor 题目链接:
Description
The input contains description of several machines. Each machine is described by two parts: the program and the input section. The program is given by a series of instructions, one per line. Every instruction is given by three uppercase letters and there must not be any other characters. The only exception is the NUM instruction, which has exactly one space after the three letters followed by a non-negative integer number between 0 and 109 . The only allowed instructions are those defined above. Each program is terminated by a line containing the word ‘END’ (and nothing else). The input section starts with an integer N (0 ≤ N ≤ 10000), the number of program executions. The next N lines contain one number each, specifying an input value Vi , 0 ≤ Vi ≤ 109 . The program should be executed once for each of these values independently, every execution starting with the stack containing one number — the input value Vi . There is one empty line at the and of each machine description. The last machine is followed by a line containing the word ‘QUIT’. No program will contain more than 100 000 instructions and no program requires more than 1 000 numbers on the stack in any moment during its execution. Output
For each input value, print one line containing the output value for the corresponding execution, i.e., the one number that will be on the stack after the program executes with the initial stack containing only the input number. If there is a program failure during the execution or if the stack size is incorrect at the end of the run (either empty or there are more numbers than one), print the word ‘ERROR’ instead. Print one empty line after each machine, including the last one. DUP MUL NUM 2 ADD END 3 1 10 50 NUM 1 NUM 1 ADD END 2 42 43 NUM 600000000 ADD END 3 0 600000000 1 QUIT Sample Output
3 102 2502 ERROR ERROR 600000000 ERROR 600000001 Source
2016-HUST-线下组队赛-1 题意:
给出一系列与栈相关的指令序列. 求对每一组指令集给定一个初始值时,是否会出现非法操作,如果不会则输出栈中唯一的数. 题解:
首先明确会ERROR的情况: 操作数不够、除/模零、结果超出限制、结果栈的元素个数不是一. 一开始以为数据规模太大直接模拟可能过不了,而实际上数据非常小... 对各种操作进行模拟即可,注意各种细节的处理. (除和模与内置运算一致,不需要重载) (注意字符串的读入和数据大小) 以下的代码中,先对指令序列出现操作数不够的情况进行了预处理,方便后面的判断. 代码:
#include #include #include #include #include #include #include #include #include #include #include