# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include <stdlib.h>
# include <map>
//使用map解决一对一 的关系
using namespace std;
map<char,long> mapdata;
//数据存储
typedef struct node
{
long num;
char ch1[20];
char ch2[20];
}LINK;
LINK arry[101];
//存储已定义的变量
struct data
{
char st[100];
int top;
}p;
int main()
{
long a,i=0,length,temp;
char str1[20],str2[20];
char ch;
long k,t;
char temp1[20];
//数据输入
while(scanf("%ld",&a) != EOF)
{
arry[i].num=a;
scanf("%s",&str1);
strcpy(arry[i].ch1,str1);
if(strcmp(str1,"STOP") != 0)
{
scanf("%s",&str2);
strcpy(arry[i].ch2,str2);
}
i++;
}
//inital
length=i;
p.top=0;
//对每一条语句顺序执行
for(i=0;i<=length;)
{
temp=0; //标记变量有没有定义
//LET语句
if(strcmp(arry[i].ch1,"LET") == 0)
{
ch=arry[i].ch2[0]; //取得变量
for(k=0;k<p.top;k++)
{
if(p.st[k]==ch)
{
temp=1; //变量已定义
if(isdigit(arry[i].ch2[2]) || arry[i].ch2[2]==‘-‘)//如果第单个是数字,则说明是常量。
{
for(t=0;t<strlen(arry[i].ch2)-2;t++)
temp1[t]=arry[i].ch2[t+2];
temp1[t]=‘\0‘;
mapdata[arry[i].ch2[0]]=atoi(temp1);
}
else
{
if(arry[i].ch2[3] == ‘+‘)
{
mapdata[arry[i].ch2[0]]=mapdata[arry[i].ch2[2]]+mapdata[arry[i].ch2[4]];
}
if(arry[i].ch2[3] == ‘>‘)
{
if(mapdata[arry[i].ch2[2]] > mapdata[arry[i].ch2[4]])
mapdata[arry[i].ch2[0]]=1;
else
mapdata[arry[i].ch2[0]]=0;
}
break;
}
}
}
if(temp == 0) //变量未定义
{
if(isdigit(arry[i].ch2[2]) || arry[i].ch2[2]==‘-‘)//如果第单个是数字,则说明是常量。
{
p.st[p.top++]=arry[i].ch2[0];
for(t=0;t<strlen(arry[i].ch2)-2;t++)
temp1[t]=arry[i].ch2[t+2];
temp1[t]=‘\0‘;
mapdata[arry[i].ch2[0]]=atoi(temp1);
}
else
{
p.st[p.top++]=arry[i].ch2[0];
if(arry[i].ch2[3] == ‘+‘)
{
mapdata[arry[i].ch2[0]]=mapdata[arry[i].ch2[2]]+mapdata[arry[i].ch2[4]];
}
else if(arry[i].ch2[3] == ‘>‘)
{
if(mapdata[arry[i].ch2[2]] > mapdata[arry[i].ch2[4]])
mapdata[arry[i].ch2[0]]=1;
else
mapdata[arry[i].ch2[0]]=0;
}
}
}
i++;
}
//PRINT语句
if(strcmp(arry[i].ch1,"PRINT") == 0)
{
printf("%c=%d\n",arry[i].ch2[0],mapdata[arry[i].ch2[0]]);
i++;
}
//IF语句
if(strcmp(arry[i].ch1,"IF") == 0)
{
if(mapdata[arry[i].ch2[0]] <= mapdata[arry[i].ch2[2]])
i++;
i++;
}
//GOTO语句
if(strcmp(arry[i].ch1,"GOTO") == 0)
{
int b;
b=atol(arry[i].ch2);
i=b-1;
}
//STOP语句
if(strcmp(arry[i].ch1,"STOP") == 0)
break;
}
/*
//测试是否输入成功
for(i=0;i<=length;i++)
{
printf("%d %s %s\n",arry[i].num,arry[i].ch1,arry[i].ch2);
}
*/
return 0;
}
原文:http://www.cnblogs.com/liun1994/p/7041185.html