面条编程
发布时间: 2018年3月13日 23:07 时间限制: 1000ms 内存限制: 128M
面条编程语言仅由3种语句组成:PRINT、GOTO、END
定义如下:
PRINT [string] : 程序输出[string],一个不含空格的字符串
GOTO [number] : 程序跳转到行号为[number]的语句执行
END : 程序结束
编写一个该面条编程语言的解释器,输入程序,输出程序运行结果,若程序不能正常结束(未执行到END语句),输出ERROR
一段面条编程程序,由上述语句组合而成,行数不多于100000
程序运行结果,即程序执行过程中PRINT语句输出的内容或ERROR
PRINT 1 GOTO 4 GOTO 2 PRINT ? END
1 ?
PRINT 1 GOTO 4 PRINT 3 PRINT 4
ERROR
主要错误(没考虑到)的是END语句读入后没有跟后续,应该先分清讨论语句内容,然后再决定输入
关于string+连接反而没有问题,不需要用到动态数组
#include <iostream>
using namespace std;
struct code
{
int how,where;
string what;
bool used;
} codes[100002];
int main()
{
bool errorif=false;
int i=0;
char n[5];
while(cin>>n)
{
if(n[0]=='P')
{
codes[i].how=0;
cin>>codes[i].what;
}
if(n[0]=='G')
{
codes[i].how=1;
cin>>codes[i].where;
}
if(n[0]=='E')
{
codes[i].how=2;
}
i++;
codes[i].used=false;
}
string shuchu;
bool hasend=false;
int j=0;
while(j>=0)
{
if(codes[j].how==2){
hasend=true;
break;
}
if(codes[j].used==true||j>=i){
errorif=true;
break;
}
codes[j].used=true;
if(codes[j].how==0){
shuchu+=codes[j].what+"\n";
}
if(codes[j].how==1){
j=(int)codes[j].where-2;
}
j++;
}
if(errorif){
cout<<"ERROR"<<endl;
}else if(!hasend){
cout<<"ERROR"<<endl;
}else{
cout<<shuchu;
}
return 0;
}