表达式求值

 使用栈来进行运算,创建两个栈,一个用来储存数字,一个用来储存运算字符,先把一个#字符压入栈作为一个判断结束的标志。

  1 #include <iostream>
  2 #include <stack>
  3 #include <cstring>
  4 
  5 #include <sstream>
  6 using namespace std;
  7 
  8 
  9 
 10 stack<double> opnd;
 11 stack<char> optr;
 12 
 13 template <class Type>
 14 Type stringToNum(const string& str)
 15 {
 16     istringstream iss(str);
 17     Type num;
 18     iss >> num;
 19     return num;
 20 }
 21 
 22 char A[7][7]={
 23     {'>','>','<','<','<','>','>'},
 24     {'>','>','<','<','<','>','>'},
 25     {'>','>','>','>','<','>','>'},
 26     {'>','>','>','>','<','>','>'},
 27     {'<','<','<','<','<','=',' '},
 28     {'>','>','>','>',' ','>','>'},
 29     {'<','<','<','<','<',' ','='}
 30 };
 31 
 32 char B[7]={'+','-','*','/','(',')','#'};
 33 int Search(char c){
 34     int i=0;
 35     while (c!=B[i]) {
 36         i++;
 37         
 38     }
 39     return i;
 40 }
 41 
 42 double operat(double a,char op,double b){
 43     double reslult=0;
 44     switch (op) {
 45         case '+':
 46             reslult=a+b;
 47             break;
 48         case '-':
 49             reslult=a-b;
 50             break;
 51         case '*':
 52             reslult=a*b;
 53             break;
 54         case '/':
 55             reslult=a/b;
 56             break;
 57     }
 58     return reslult;
 59 }
 60 
 61 
 62 char precede(char c1,char c2){
 63     int i,j;
 64     i=Search(c1);
 65     j=Search(c2);
 66     return A[i][j];
 67 }
 68 
 69 int main() {
 70     cin.clear();
 71     double a,b,c;
 72     char s,op;
 73     string s1="";
 74 
 75     optr.push('#');
 76     s=getchar();
 77     
 78     while (s!='#'||optr.top()!='#') {
 79         
 80         if((s<='9'&&s>='0')||s=='.'){
 81             s1=s1+s;
 82             
 83           //  cout << "test数字"<< endl;
 84             
 85             s=getchar();
 86             if(s!='.'&&(s>'9'||s<'0')){
 87                 opnd.push(stringToNum<double>(s1));
 88                 s1="";
 89             }
 90         }
 91         else{
 92             switch (precede(optr.top(),s)) {
 93                 case '<':
 94                     optr.push(s);
 95                     s=getchar();
 96                     
 97                  //   cout << "test < " << endl;
 98         
 99                     break;
100                 case '=':
101                     optr.pop();
102                     s=getchar();
103                     
104                 //    cout << "test =" << endl;
105                     
106                     break;
107                 case '>':
108                     op=optr.top();
109                     optr.pop();
110                     b=opnd.top();
111                     opnd.pop();
112                     a=opnd.top();
113                     opnd.pop();
114                     c=operat(a, op, b);
115                     opnd.push(c);
116                     
117                  //   cout << "test >"  << endl;
118                     break;
119                 default:
120                 //    cout << "无" <<endl;
121                     break;
122             }
123         }
124     }
125     cout << opnd.top()<< endl;
126     return 0;
127 }

这里使用了字符串转换double的方法,判断到一个运算符后就是将之前识别的支付串转换成double入栈。

原文地址:https://www.cnblogs.com/zhouqianwei/p/10614481.html