机试-表达式求值

  1 #include <iostream>
  2 #include <stack>
  3 #include <stdio.h>
  4 
  5 using namespace std;
  6 
  7 char str[220];  //表达式字符串
  8 int mat[][5]=
  9 {
 10     1,0,0,0,0,
 11     1,0,0,0,0,
 12     1,0,0,0,0,
 13     1,1,1,0,0,
 14     1,1,1,0,0,
 15 };  //优先级矩阵
 16 
 17 stack<int> op; //保存运算符标号,+-*/依次为1,2,3,4
 18 stack<double> in;//运算数
 19 
 20 void getOp(bool &reto,int &retn,int &i)
 21 {//获得表达式的下一个元素函数,若reto为true,表示该元素为一个运算符,其编号保存在retn中;
 22     //否则,若reto为false,表示该元素为一个运算数,其值保存在引用变量retn中。
 23     //引用变量i表示遍历的字符串下标
 24     if(i==0&&op.empty()==true)
 25     {
 26         reto=true;
 27         retn=0;
 28         return;
 29     }
 30     if(str[i]==0)
 31     {
 32         reto=true;
 33         retn=0;
 34         return;
 35     }
 36     if(str[i]>='0'&&str[i]<='9')
 37     {
 38         reto=false;
 39     }
 40     else
 41     {
 42         reto=true;
 43         if(str[i]=='+')
 44         {
 45             retn=1;
 46         }
 47         else if(str[i]=='-')
 48         {
 49             retn=2;
 50         }
 51         else if(str[i]=='*')
 52         {
 53             retn=3;
 54         }
 55         else if(str[i]=='/')
 56         {
 57             retn=4;
 58         }
 59         i+=2;
 60         return;
 61     }
 62     retn=0;//返回结果为数字
 63     for(;str[i]!=' '&&str[i]!=0;i++)
 64     {
 65         retn*=10;
 66         retn+=str[i]-'0';
 67     }//计算该数字的数字值
 68     if(str[i]==' ')
 69         i++;
 70     return;
 71 }
 72 
 73 int main()
 74 {
 75     while(gets(str))
 76     {
 77         if(str[0]=='0'&&str[1]==0)
 78             break;//输入只有一个0,退出
 79         bool retop;
 80         int retnum;
 81         int idx=0;
 82         while(!op.empty())
 83             op.pop();
 84         while(!in.empty())
 85             in.pop(); //清空数字栈和运算符栈
 86         while(true)
 87         {
 88             getOp(retop,retnum,idx);
 89             if(retop==false)
 90             {
 91                 in.push((double)retnum);
 92             }
 93             else
 94             {
 95                 double tmp;
 96                 if(op.empty()==true||mat[retnum][op.top()]==1)
 97                 {//若运算符栈为空或当前遍历到的运算符优先级大于栈顶运算符,
 98                     //将该运算符压入运算符堆栈
 99                     op.push(retnum);
100                 }
101                 else
102                 {
103                     while(mat[retnum][op.top()]==0)
104                     {
105                         int ret=op.top();
106                         op.pop();
107                         double b=in.top();
108                         in.pop();
109                         double a=in.top();
110                         in.pop();
111 
112                         if(ret==1)
113                             tmp=a+b;
114                         else if(ret==2)
115                             tmp=a-b;
116                         else if(ret==3)
117                             tmp=a*b;
118                         else
119                             tmp=a/b;
120                         in.push(tmp);
121                     }
122                     op.push(retnum);
123                 }
124             }
125 
126             if(op.size()==2&&op.top()==0)
127                 break;//若运算符栈只有两个元素,且其栈顶元素为标记运算符,则表示该表达式求值结束
128 
129         }
130         printf("%.2f
",in.top());
131     }
132     return 0;
133 }
原文地址:https://www.cnblogs.com/Xbert/p/5109975.html