SDUT 小型Basic编译器问题
http://acm.sdut.edu.cn/web/showproblem.php?pid=2099&cid=1088
才开始掉下了中间可能有多个空格的处理,WA了好几次。。。还有这里对表达式的理解一定要透彻,表达式有三种1:常数;2:A+B形式;3:A>B的形式。。。
View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 107
using namespace std;
struct node
{
int num;//存编号
char op[10];//存操作符
char exp[10];//存表达式
int expl;//存表达式的长度
}tagp[maxn];
int en[30];
int main()
{
char st[maxn];
int i,j,len,jj;
int k = 1;
memset(tagp,0,sizeof(tagp));
memset(en,0,sizeof(en));
while (gets(st) != NULL)
{
len = strlen(st);
i = 0;
while (st[i] == ' ') i++;//处理空格
for (; st[i] != ' ' && i < len; ++i)
{
tagp[k].num = tagp[k].num*10 + st[i] - '0';
}
while (st[i] == ' ') i++;//处理空格
int l = 0;
for (j = i; st[j] != ' ' && j < len; ++j)
tagp[k].op[l++] = st[j];
while (st[j] == ' ') j++;
l = 0;
for (jj = j; jj < len; ++jj)
{
tagp[k].exp[l++] = st[jj];
tagp[k].expl++;
}
k++;
}
int s = 1;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
while (s)
{
if (strcmp(tagp[s].op,"STOP") == 0) break;
if (strcmp(tagp[s].op,"LET") == 0)
{
pos1 = tagp[s].exp[0] - 'A';
if (tagp[s].expl > 3)//表达式长度大于3可能常数可能是其他两种
{
if (tagp[s].exp[3] >= '0' && tagp[s].exp[3] <= '9')//是常数
{
int sum = 0;
for (i = 2; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
en[pos1] = sum;
}
else//是A>B或A+B
{
pos2 = tagp[s].exp[2] - 'A';
pos3 = tagp[s].exp[4] - 'A';
if (tagp[s].exp[3] == '+')
en[pos1] = en[pos2] + en[pos3];
else if (tagp[s].exp[3] == '>')
{
if (en[pos2] > en[pos3])
en[pos1] = 1;
else
en[pos1] = 0;
}
}
}
else//小于3肯定是常数
{
int sum = 0;
for (i = 2; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
en[pos1] = sum;
}
s++;
}
else if (strcmp(tagp[s].op,"PRINT") == 0)
{
pos1 = tagp[s].exp[0] - 'A';
printf("%s=%d\n",tagp[s].exp,en[pos1]);
s++;
}
else if (strcmp(tagp[s].op,"IF") == 0)
{
if (tagp[s].expl == 3)//三种情况都有
{
if (tagp[s].exp[1] >= '0' && tagp[s].exp[1] <= '9')//常数
{
int sum = 0;
for (i = 0; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
if (sum > 0) s++;
else s += 2;
}
else
{
pos2 = tagp[s].exp[0] - 'A';
pos3 = tagp[s].exp[2] - 'A';
if (tagp[s].exp[1] == '+')//A+B
{
int mark = en[pos2] + en[pos3];
if (mark > 0) s++;
else s += 2;
}
else if (tagp[s].exp[1] == '>')//A>B
{
pos2 = tagp[s].exp[0] - 'A';
pos3 = tagp[s].exp[2] - 'A';
if (en[pos2] > en[pos3])
s++;
else
s += 2;
}
}
}
else//常数
{
int sum = 0;
for (i = 0; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
if (sum > 0)
s++;
else
s += 2;
}
}
//同上
else if (strcmp(tagp[s].op,"GOTO") == 0)
{
if (tagp[s].expl == 3)
{
if (tagp[s].exp[1] >= '0' && tagp[s].exp[1] <= '9')
{
int sum = 0;
for (i = 0; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
if (sum > 0) s = sum;
else break;
}
else
{
pos2 = tagp[s].exp[0] - 'A';
pos3 = tagp[s].exp[2] - 'A';
if (tagp[s].exp[1] == '+')
{
int mark = en[pos2] + en[pos3];
if (mark > 0) s = mark;
else break;
}
else if (tagp[s].exp[1] == '>')
{
pos2 = tagp[s].exp[0] - 'A';
pos3 = tagp[s].exp[2] - 'A';
if (en[pos2] > en[pos3])
s = 1;
else
break;
}
}
}
else
{
int sum = 0;
for (i = 0; i < tagp[s].expl; ++i)
{
sum = sum*10 + (tagp[s].exp[i] - '0');
}
if (sum > 0)
s = sum;
else
break;
}
}
}
return 0;
}