题目我感觉就是一个模拟题。就是给你说TinyBasic语言的语法规则,然后让你对这个TinyBasic语言程序就进行解释。只要认真一点,就能过掉了。要注意的就是,TinyBasic语言中表达式的定义有三个,后面只要是提到有表达式的地方,这三个定义都要考虑。一开始就是以为那个IF 那里只会有 判断式 ,忘了其他的两种情况了
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 #define N 30
7 #define M 100
8 #define inf 99999999
9 int mark[N];
10 char str[M][M];
11 int main()
12 {
13 int i,j,t,k;
14 char sbr[M],scr[M],a[M];
15 char ch,sh,th;
16 memset(mark,0,sizeof(mark));
17 i=1;
18 while(gets(str[i]))
19 {
20 if(i>100) break;
21 i++;
22 }
23 j=1;
24 while(j<i)
25 {
26 memset(sbr,0,sizeof(sbr));
27 memset(scr,0,sizeof(scr));
28 memset(a,0,sizeof(a));
29 sscanf(str[j],"%d%s%s",&t,sbr,scr);
30 int flag=0;
31 if(!strcmp(sbr,"STOP")) break;
32 else if(!strcmp(sbr,"LET"))
33 {
34 sscanf(scr,"%c%c%s",&sh,&ch,a);
35 int len=strlen(a);
36 if(a[0]<='9'&&a[0]>='0')
37 {
38 int sum=0;
39 k=0;
40 while(a[k])
41 {
42 sum=sum*10+a[k]-'0';k++;
43 }
44 mark[sh-'A']=sum;
45 }
46 else
47 {
48 if(a[1]=='+')
49 {
50 int sum=mark[a[0]-'A']+mark[a[2]-'A'];
51 mark[sh-'A']=sum;
52 }
53 else if(a[1]=='>')
54 {
55 if(mark[a[0]-'A']>mark[a[2]-'A']) mark[sh-'A']=1;
56 else mark[sh-'A']=0;
57 }
58 }
59 }
60 else if(!strcmp(sbr,"PRINT"))
61 {
62 cout<<scr[0]<<"="<<mark[scr[0]-'A']<<endl;
63 }
64 else if(!strcmp(sbr,"IF"))
65 {
66 int len=strlen(scr);
67 int h=0;
68 for(h=0;h<len;h++)
69 {
70 if(scr[h]=='+'||scr[h]=='>') break;
71 }
72 int kk=0;
73 if(h==len)
74 {
75 if(scr[0]=='0') j++;
76 else kk=1;
77 }
78 else if(!kk)
79 {
80 sscanf(scr,"%c%c%c",&sh,&ch,&th);
81 if(ch=='+')
82 {
83 int sum=mark[sh-'A']+mark[th-'A'];
84 if(!sum) j++;
85 }
86 else if(ch=='>')
87 {
88 if(mark[sh-'A']<=mark[th-'A']) j++;
89 }
90 }
91 }
92 else if(!strcmp(sbr,"GOTO"))
93 {
94 flag=1;
95 int len=strlen(scr);
96 int h=0;
97 for(h=0;scr[h];h++)
98 {
99 if(scr[h]=='+'||scr[h]=='>') break;
100 }
101 if( h==len )
102 {
103 h=0;
104 int sum=0;
105 while(scr[h])
106 {
107 sum=sum*10+scr[h]-'0';
108 h++;
109 }
110 j=sum;
111 }
112 else
113 {
114 sscanf(scr,"%c%c%c",&sh,&ch,&th);
115 if(ch=='+')
116 {
117 j=mark[sh-'A']+mark[th-'A'];
118 }
119 else if(ch=='>')
120 {
121 if(mark[sh-'A']>mark[th-'A']) j=1;
122 else j=0;
123 }
124 }
125 }
126 if(!flag) j++;
127 }
128 return 0;
129 }