题目链接: OpenJudge - 1684:Dynamic Declaration Language (DDL)
题目描述:
评价: 一道单纯的大模拟题,算法难度不大,需要非常细心,锻炼我们的耐心和能力!值得一做
分析:
总结一下题目的条件,这种语言,有2种错误:
1. 变量重复定义
2. 变量使用未声明
这题有一个需要注意的点就是: goto 语句是可以向前跳转的,因此我们需要先把所有的输入存下来,然后再模拟程序的运行过程。(我一开始没考虑到这个,卡了非常久,一直调不出来)
然后别的地方其实没啥难度,只要细心处理就好.
参考代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N=110;
int t,n;
int cur_line=0,last_line;
int end_flag=0;
int num_pro;
string s;
string s_ori;
map<string,int> mp;
map<string,int> used;
string guan[10]={"dcl","inc","goto","dec","end"};
struct node{
string ops;
string x;
int value;
};
node arr[N];
string change(string g){
for(int i=0;i<g.size();i++){
if(g[i]>='A'&&g[i]<='Z'){
g[i]-=('A'-'a');
}
}
return g;
}
int check_num(string g){
int flag=1;
for(int i=0;i<g.size();i++){
if(g[i]>='0'&&g[i]<='9'){
continue;
}
else return 0;
}
return 1;
}
int change_num(string g){
int num=0;
for(int i=0;i<g.size();i++){
num=num*10+(g[i]-'0');
}
return num;
}
int main(void){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
num_pro++;
printf("%d\n",num_pro);
last_line=0;
cur_line=0;
end_flag=0;
mp.clear();
used.clear();
for(int i=1;i<=n;i++){
cin >> s;
s_ori=s;
s=change(s);
if(s==guan[0]){ //dcl
string id;
cin >> id;
arr[i]=node{"dcl",id,0};
}
else if(s==guan[1]){ // inc x
string id;
cin >> id;
arr[i]=node{"inc",id,0};
}
else if(s==guan[2]){ // goto i or goto id i
string line_num;
string id;
cin >> id;
if(check_num(id)){ // goto i
int num=change_num(id);
arr[i]=node{"goto","-",num};
}
else{ // goto id i
int num;
cin >> num;
arr[i]=node{"goto",id,num};
}
}
else if(s==guan[3]){ // dec x
string id;
cin >> id;
arr[i]=node{"dec",id,0};
}
else if(s==guan[4]){ //end
arr[i]=node{"end","-",0};
}
else{ // id = ic; where ic is constant
string id;
id=s_ori;
string op;
cin >> op;
int num;
cin >> num;
arr[i]=node{id,id,num};
}
}
cur_line=1;
while(cur_line<=n){
s=arr[cur_line].ops;
if(s==guan[0]){ //dcl
string id;
id=arr[cur_line].x;
if(mp.count(id)&&used[id]==0){
printf("%d %d\n",cur_line,1);
cur_line++;
continue;
}
cur_line++;
mp[id]=0;
used[id]=0; // indicate that the x has not been used.
}
else if(s==guan[1]){ // inc x
string id;
id=arr[cur_line].x;
if(!mp.count(id)){
printf("%d %d\n",cur_line,2);
cur_line++;
continue;
}
cur_line++;
used[id]=1;// id has been used
mp[id]++;
}
else if(s==guan[2]){ // goto i or goto id i
string line_num;
string id;
id=arr[cur_line].x;
if(id=="-"){ // goto i
int num=arr[cur_line].value;
cur_line=num;
continue;
}
else{ // goto id i
int num;
num=arr[cur_line].value;
if(!mp.count(id)){
printf("%d %d\n",cur_line,2);
cur_line++;
continue;
}
used[id]=1;
if(mp[id]>0){
cur_line=num;
continue;
}
cur_line++;
}
}
else if(s==guan[3]){ // dec x
string id;
id=arr[cur_line].x;
if(!mp.count(id)){
printf("%d %d\n",cur_line,2);
cur_line++;
continue;
}
cur_line++;
used[id]=1;// id has been used
mp[id]--;
}
else if(s==guan[4]){ //end
break;
}
else{ // id = ic; where ic is constant
string id;
id=arr[cur_line].x;
int num;
num=arr[cur_line].value;
if(!mp.count(id)){
printf("%d %d\n",cur_line,2);
cur_line++;
continue;
}
mp[id]=num;
used[id]=1;
cur_line++;
}
}
}
return 0;
}