7-21 求前缀表达式的值 (25分)

参考:https://blog.csdn.net/qq_36254699/article/details/78824351

前缀表达式求值方法:

先将表达式入栈a,将栈a元素逐个出栈,如果是数字,直接入栈b,如果是操作符x,则b出栈2次,用t1接收第一个数,t2接收第二个数,再将t2 x t1的计算

结果压入栈b。

最后留在栈b的结果就是计算的结果。

  1 #include <iostream>
  2 #include <string>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <iomanip>
  6 using namespace std;
  7 class stk
  8 {
  9 public:
 10     stk() :r(-1) {}
 11     ~stk() {}
 12     void push(double k)
 13     {
 14         s[++r] = k;
 15     }
 16     double top()
 17     {
 18         if (r != -1)
 19             return s[r--];
 20     }
 21 public:
 22     double s[100];
 23     int r;
 24 };
 25 int main()
 26 {
 27     stk digit;
 28     char exp[110];
 29     cin.getline(exp, 100);
 30     int Max;
 31     for (Max = 0; exp[Max] != ''; Max++);
 32     for (int i = Max - 1; i >= 0; i--)
 33     {
 34         double sum = 0;
 35 
 36         if (exp[i] >= '0' && exp[i] <= '9')
 37         {
 38             double mult = 1;
 39             for (int j = i; j >= 0; j--)
 40             {
 41                 if (exp[j] == ' ')
 42                 {
 43                     digit.push(sum);
 44                     i = j;
 45                     break;
 46                 }
 47                 else
 48                 {
 49                     if ((exp[j] >= '0' && exp[j] <= '9') || exp[j] == '.')
 50                     {
 51                         if (exp[j] >= '0' && exp[j] <= '9')
 52                         {
 53                             sum += (exp[j] - '0') * mult;
 54                             mult *= 10;
 55                             if (j == 0)
 56                             {
 57                                 digit.push(sum);
 58                                 i = j;
 59                                 break;
 60                             }
 61                         }
 62                         else
 63                         {
 64                             sum = sum / mult;
 65                             mult = 1;
 66                         }
 67                     }
 68                     else if ((exp[j] == '+' || exp[j] == '-') && j == 0)
 69                     {
 70                         if (exp[j] == '-')sum = -sum;
 71                         digit.push(sum);
 72                         i = j;
 73                         break;
 74                     }
 75                     else if (exp[j] == '-')
 76                     {
 77                         sum = -sum;
 78                     }
 79                     else continue;
 80 
 81                 }
 82             }
 83         }
 84         else
 85         {
 86             if (exp[i] == ' ')
 87             {
 88                 continue;
 89             }
 90             else
 91             {
 92                 double t1, t2;
 93                 t1 = digit.top();
 94                 t2 = digit.top();
 95                 if (exp[i] == '+')
 96                 {
 97                     sum = t1 + t2;
 98                 }
 99                 else if (exp[i] == '-')
100                 {
101                     sum = t1 - t2;
102                 }
103                 else if (exp[i] == '*')
104                 {
105                     sum = t1 * t2;
106                 }
107                 else if (exp[i] == '/')
108                 {
109                     if (t2 != 0)
110                     {
111                         sum = t1 / t2;
112                     }
113                     else
114                     {
115                         cout << "ERROR";
116                         exit(0);
117                     }
118                 }
119                 digit.push(sum);
120             }
121         }
122     }
123     cout << fixed << setprecision(1) << digit.top() << endl;
124     return 0;
125 }
原文地址:https://www.cnblogs.com/2020R/p/12423293.html