码迷,mamicode.com
首页 > 其他好文 > 详细

小型Basic编译器问题

时间:2014-06-08 20:26:01      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   color   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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;
}

 

小型Basic编译器问题,布布扣,bubuko.com

小型Basic编译器问题

标签:c   class   blog   code   a   color   

原文地址:http://www.cnblogs.com/liun1994/p/3775944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!