1333: Cat Language
题目
题目描述
猫元前173年,人类从地球上消失。
猫元元年,智慧的猫猫建立起了文明。
猫元116年,猫猫点亮编程科技树。
为了推动程序设计的发展,猫猫极客们决定开发 Cat Language(CL) 。
CL分为三部分。
第一部分是变量的声明,在程序的头部。CL中只有整数int
变量,因为猫猫们认为万物皆能由整数表现。变量的声明包括变量的总数n和n个变量的名称。名称只能由大小写字母组成,且不需要做保留字的检查。猫猫讨厌乱码,所以所有的变量声明后都会被初始化为0。
第二部分是程序体。程序体部分包括程序总行数m和m行语句(0-base对每行语句进行标号)。
语句的具体描述如下。在接下来的描述中,var
代表此处是一个变量名称,const
代表此处是一个常量(int
范围内),exp
代表此处可以是var
或const
。猫猫不想计算复合表达式,所以CL中不会出现复合表达式。同时猫猫不喜欢负数,所以在指定const
时不会使用负数(但运行时的变量可以为负值)。
- 无参数
-
End
- 程序结束的标识符。需要注意的是,程序中可以有任意数量的
End
语句 - 隐式地在第m行含有一个
End
语句
- 程序结束的标识符。需要注意的是,程序中可以有任意数量的
-
单参数
Input var
- 等待输入一个整数并赋值给
var
- 等待输入一个整数并赋值给
Print exp
- 输出
exp
代表的值并换行
- 输出
Jump const
- 将程序执行指针
pointer
跳转到line[const]
- 将程序执行指针
- 双参数
Let var exp
var = exp
- 三参数
Add var exp1 exp2
var = exp1 + exp2
Sub var exp1 exp2
var = exp1 - exp2
Multi var exp1 exp2
var = exp1 * exp2
Div var exp1 exp2
var = exp1 / exp2
- 整数除法,保证
exp2
不为0
Mod var exp1 exp2
var = exp1 mod exp2
- 取模运算,保证
exp2
不为0
Jumpe const exp1 exp2
if exp1==exp2 then move the pointer to line[const]
Jumplt const exp1 exp2
if exp1<exp2 then move the pointer to line[const]
第三部分是运行时指令。
指令如下:
Run
:从程序头部开始执行整个程序。首先将执行指针pointer
指向第0行语句,开始执行。如果遇到跳转,则将执行指针移动到跳转后的位置,否则就移到下一行语句。Run
语句接下来紧跟着程序中需要的输入,每个输入间以空格隔开。
Clear
:重置所有变量为0。
Quit
:退出运行。
作为一名化身为极客猫猫的原人类程序员,你承担起了猫语言CL的开发任务。请实现一个猫语言解释器Cat Language Interpreter
。
输入格式
见描述。变量名称不长于10个字符。const
不会是负数。跳转时保证行号合法。变量名区分大小写。
0<n<=100
0<m<=100
输出格式
见描述,即每个Print语句的输出,每个输出占一行。
样例输入
Sample1
Cat Language
3 sum i n
7
Input n
Let i 1
Let sum 0
Add sum sum i
Add i i 1
Jumplt 3 i n
Print sum
Run
10
Run
100
Run
101
Quit
Sample2
Cat Language
4 k x ans tmp
10
Input x
Input k
Let ans 0
Mod tmp k 2
Div k k 2
Jumpe 7 tmp 0
Add ans ans x
Add x x x
Jumplt 3 0 k
Print ans
Run
7 9
Run
93 714
Quit
样例输出
Sample1
45
4950
5050
Sample2
63
66402
数据范围
保证给出的CL代码合法且不会出现死循环。
建议使用类的继承来实现,为Basic interpreter
大作业作准备。可以补全以下代码。
```c++
include
include
include
using namespace std;
class Base;
int n,m;
string var[1005];
map
class Base{ public: static int pointer; virtual void execute()=0; };
int Base::pointer=0;
class EndStatement:public Base{ public: void execute(){ / code here / } };
class InputStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
class PrintStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
class JumpStatement:public Base{ private: / code here / public: void execute(){ / code here / } }; class LetStatement:public Base{ private: / code here / public: void execute(){ / code here / } }; class AddStatement:public Base{ private: / code here / public: void execute(){ / code here / } }; class SubStatement:public Base{ private: / code here / public: void execute(){ / code here / } }; class MultiStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
class DivStatement:public Base{ private: / code here / public: void execute(){ / code here / } }; class ModStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
class JumpeStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
class JumpltStatement:public Base{ private: / code here / public: void execute(){ / code here / } };
void InputCode(){ / code here / } void Clear(){ state.clear(); for (int i = 0; i < n; i++) { state[var[i]]=0; } } void Quit(){ for (int i = 0; i < m; i++) { delete statements[i]; } }
void RunTheProgram(){
int &p=Base::pointer;
p=0;
while (p
void InputCommands(){ Clear(); string com; while (cin>>com) { if (com=="Quit") { Quit(); return; } if (com=="Clear") Clear(); if (com=="Run") RunTheProgram(); } }
int main(){ InputCode(); InputCommands(); return 0; } ```
Oops! 本题目还没有解答!
助教老师们编题的速度,已经超过了解题的速度!
OJ翻了一新,但本解答集还大多用的是2017-2019级,甚至更早的同学们贡献的答案。
如果你已经AC了,可以的话,请您参考添加页面,与大家一起分享你的题解!