【应用】浮点数四则运算器 Part2:转化模块的编写

根据在 【应用】浮点数四则运算器 Part1:输入模块的编写 中的规划,有如下思路:

需要计算的部分实际上是flag=0的部分,其余部分都有其他对应的操作。

当flag=0时,

1.扫描all全部数据,找到其中字母元x,检查是否有对应值。

若没有:返回error code 3: undefined 'x',x是字母元的名称。继续输入下一组数据

若全部有对应值,将其转化为其对应值。

这样就得到了完全又数字和操作符组成的表达式,同时完成了期望3.能够识别没有赋初值的字母元。

这里用到的是map的函数find()。

number.find();

空返回number.end(),不空返回这个值。

2.识别错误类型:是否缺少或多余运算符。

缺少或多余运算符的特征有以下几种:

1)数字的下一个是数字或括号'('。数字的上一个是括号')'。

2)操作符的下一个是操作符或空串。操作符的上一个是空串。

可以将数字编为1,括号'('编为2,括号')'编为3,操作符编为4。

若当前位置编码为1,当上一个为3时输出error code 4: operation after ')' needed

若当前位置编码为1,当下一个为2时输出error code 5: operation before '(' needed

若当前位置编码为1,当下一个为1时输出error code 6: operation after 'x' needed,x为当前位置的数据

若当前位置编码为4,当下一个为4时输出error code 7: double operation 'x' 'y',x为当前操作符,y为下一个

若当前位置编码为4,当下一个不存在时输出error code 8: extra end operation 'x',x为当前操作符

若当前位置编码为4,当上一个不存在时输出error code 9: extra start operation 'x',x为当前操作符

继续输入下一组数据

3.若没有错误,则证明是正确的格式。那么就开始运算。

1)首先是表达式的转化,将中缀表达式转化为后缀表达式。

2)利用后缀表达式的计算函数计算结果。但需注意:

当调用'/'是需检测除数是否为0。

若为0,则输出error code 10:'0' under '/'

为了实现这一功能,使用递归的后缀表达式计算函数难以操作,需要考虑新的操作方法。

已知完整的后缀表达式存放在一个栈中,可以创建一个新栈,数据时存入,操作符时操作后压入栈顶,为'/'时检测栈顶是否为0。

最后输出栈顶元素。正常情况下输出后栈应该只有一个元素。若栈中有多个元素:

extra code 11:BUG SHOWED!DAMN!!!

由于错误判断有很多,决定定义一个全局变量Error用来保存是否出现了问题。

在这里发现了新的问题。之前的文章中转化为后缀表达式方式无法兼容识别负数,于是增加了一些内容用于识别负数。

以上是计算思路。

下面是计算函数的代码实现:

1.首先是用数据串替换字母串。

 1 int Error=0;
 2 
 3 void givenum(){
 4     Error=0;
 5     for (int i=0;i<=x;i++){
 6         if ((all[i][0]>='0'&&all[i][0]<='9')||
 7             all[i]=="("||
 8             all[i]==")"||
 9             all[i][0]=='+'||
10             all[i][0]=='-'||
11             all[i]=="*"||
12             all[i]=="/"){
13             continue;
14         }
15         if (cnt.find(all[i])!=cnt.end()){
16             all[i]=cnt[all[x]];
17         }
18         else{
19             printf("out>>error code 3: undefined '%s'\n",all[i].c_str());
20             Error=1;
21             break;
22         }
23     }
24 }

2.检查表达式合法性。

 1 void check(){
 2     Error=0;
 3     int code[x+10];
 4     for (int i=0;i<=x;i++){
 5         if (all[i]=="+"||
 6             all[i]=="-"||
 7             all[i]=="*"||
 8             all[i]=="/"){
 9             code[i]=4;
10             continue;
11         }
12         if (all[i][0]=='('){
13             code[i]=2;
14             continue;
15         }
16         if (all[i][0]==')'){
17             code[i]=3;
18             continue;
19         }
20         code[i]=1;
21     }
22     if (code[0]==4){
23         printf("out>>error code 9: extra start operation '%s'\n",all[0].c_str());
24         Error=1;
25     }
26     for (int i=0;i<=x;i++){
27         if (i>0&&
28             code[i]==1&&
29             code[i-1]==3){
30             printf("out>>error code 4: operation after ')' needed\n");
31             Error=1;
32         }
33         if (i<x&&
34             code[i]==1&&
35             code[i+1]==2){
36             printf("out>>error code 5: operation before '(' needed\n");
37             Error=1;
38         }
39         if (i<x&&
40             code[i]==1&&
41             code[i+1]==1){
42             printf("out>>error code 6: operation after '%s' needed\n",all[i].c_str());
43             Error=1;
44         }
45         if (i<x&&
46             code[i]==4&&
47             code[i+1]==4){
48             printf("out>>error code 7: double operation '%s' '%s'\n",all[i].c_str(),all[i+1].c_str());
49             Error=1;
50         }
51     }
52     if (code[x]==4){
53         printf("out>>error code 8: extra end operation '%s'\n",all[x].c_str());
54         Error=1;
55     }
56 }

3.转化表达式。

 1 stack<string> fx1;
 2 stack<string> fx2;
 3 
 4 int level(string buffer){
 5     switch (buffer[0]){
 6         case '+': return 1;
 7         case '-': return 1;
 8         case '*': return 2;
 9         case '/': return 2;
10         default:  return 0;
11     }
12 }
13 
14 void change(){
15     while (fx1.empty()!=1){
16         fx1.pop();
17     }
18     while (fx2.empty()!=1){
19         fx2.pop();
20     }
21     int m=0;
22     while (m<=x){
23         if (all[m]=="+"||
24             all[m]=="-"||
25             all[m]=="*"||
26             all[m]=="/"){
27             if (fx1.empty()==1||
28                 fx1.top()=="("){
29                 fx1.push(all[m]);
30                 m++;
31                 continue;
32             }
33             else{
34                 if (level(fx1.top())<=level(all[m])){
35                     fx1.push(all[m]);
36                     m++;
37                     continue;
38                 }
39                 else{
40                     while (fx1.empty()!=1&&
41                            fx1.top()!="("&&
42                            level(fx1.top())>level(all[m])){
43                         fx2.push(fx1.top());
44                         fx1.pop();
45                     }
46                     fx1.push(all[m]);
47                     m++;
48                     continue;
49                 }
50             }
51         }
52         if (all[m][0]=='('||
53             all[m][0]==')'){
54             if (all[m][0]=='('){
55                 fx1.push(all[m]);
56                 m++;
57                 continue;
58             }
59             if (all[m][0]==')'){
60                 while (fx1.empty()!=1&&
61                        fx1.top()!="("){
62                     fx2.push(fx1.top());
63                     fx1.pop();
64                 }
65                 fx1.pop();
66                 m++;
67                 continue;
68             }
69         }
70         fx2.push(all[m]);
71         m++;
72     }
73     while (fx1.empty()!=1){
74         fx2.push(fx1.top());
75         fx1.pop();
76     }
77     while (fx2.empty()!=1){
78          fx1.push(fx2.top());
79          fx2.pop();
80     }
81 }

测试这三段代码用的主函数是:

 1 int main(){
 2     while (1){
 3         Getdata();
 4         if (flag==1){
 5             printf("out>>error code 1:extra ')'\n");
 6             continue;
 7         }
 8         if (flag==2){
 9             printf("out>>error code 2:extra '('\n");
10             continue;
11         }
12         if (flag==3){
13             printf("out>>%s\n",cnt[all[x-1]].c_str());
14             continue;
15         }
16         if (flag==4){
17             break;
18         }
19         if (flag==0){
20             printf("out>>");
21             for (int i=0;i<x;i++){
22                 printf("%s ",all[i].c_str());
23             }
24             printf("%s\n",all[x].c_str());
25             givenum();
26             if (Error==1){
27                 continue;
28             }
29             check();
30             if (Error==1){
31                 continue;
32             }
33             change();
34             while (fx1.empty()!=1){
35                 printf("%s",fx1.top().c_str());
36                 printf(" ");
37                 fx1.pop();
38             }
39             printf("\n");
40             continue;
41         }
42     }
43     return 0;
44 }

运行结果与预期相同。

到这里已经达成的目的是将表达式转化为完整无误的后缀表达式。

输出时要注意是否有除数为0的情况出现。

在Part 3中记录计算输出结果的模块。

猜你喜欢

转载自www.cnblogs.com/Ze-Black/p/10062262.html
今日推荐