校赛面条编程

面条编程

发布时间: 2018年3月13日 23:07   时间限制: 1000ms   内存限制: 128M

描述

面条编程语言仅由3种语句组成:PRINT、GOTO、END

定义如下:

PRINT [string]   :  程序输出[string],一个不含空格的字符串

GOTO [number]   :  程序跳转到行号为[number]的语句执行

END   :  程序结束

编写一个该面条编程语言的解释器,输入程序,输出程序运行结果,若程序不能正常结束(未执行到END语句),输出ERROR

输入

一段面条编程程序,由上述语句组合而成,行数不多于100000

输出

程序运行结果,即程序执行过程中PRINT语句输出的内容或ERROR

样例输入1
PRINT 1
GOTO 4
GOTO 2
PRINT ?
END
样例输出1
1
?
样例输入2
PRINT 1
GOTO 4
PRINT 3
PRINT 4
样例输出2
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值