表达式求值 逆波兰

 

输入一段表达式 1+2*(21*2+(3/2))马上得到结果

 

 

  1 #include <iostream>
  2 #include <stack>
  3 using namespace std;
  4 
  5 //当前字符跟栈定字符进行比较
  6 char  priorcompare( stack<char> &opstr, stack<int> &opnum,char inputstr)
  7 {
  8       
  9    /* 
 10      优先级  1.'>'       栈顶字符优先级大于等于输入字符   出栈计算
 11              2. '<'      栈顶字符优先级小于输入字符       入栈
 12              3. '='      ( )括号匹配
 13     */
 14 
 15     //char flag_priority = 0;        
 16         
 17    if(opstr.top()=='+'||opstr.top()=='-')
 18    {
 19           if(inputstr=='*'||inputstr=='/'||inputstr=='(')
 20           {
 21                   //flag_priority = '<';     
 22               return  '<';
 23           }
 24           else
 25           {
 26                     ///flag_priority = '>';
 27               return '>';
 28           }
 29    }
 30 
 31    if (opstr.top()=='*'||opstr.top()=='/') 
 32    {
 33 
 34         if(inputstr=='(')
 35         {
 36           return '<';
 37         }
 38         else
 39         { 
 40             return '>';
 41         }
 42    }
 43  
 44 
 45    if(opstr.top()=='(')
 46    {
 47 
 48         if(inputstr =='\n')
 49         {  
 50             cout<<"error"<<__LINE__<<endl;
 51             exit(!0);
 52         }
 53         
 54         if(inputstr==')')
 55         {
 56           return '=';
 57         }
 58 
 59         else
 60         {
 61 
 62             return '<';
 63         }
 64 
 65    }
 66 
 67  
 68    if(opstr.top()==')')
 69    {
 70      if(inputstr=='(')
 71      {
 72          cout<<"error"<<__LINE__<<endl;
 73          exit(!0);
 74      }
 75      else
 76      {
 77       return '>';
 78      }
 79    }
 80 
 81    if(opstr.top()=='\n')
 82    {
 83        if(inputstr=='\n')
 84        {
 85            return '=';
 86        }
 87 
 88        if(inputstr==')')
 89        {
 90            cout<<"error"<<__LINE__<<endl;
 91            exit(!0);
 92        }
 93        else
 94        {
 95             return '(';
 96        }
 97    }
 98 }
 99 
100 
101 void calc(stack<char> &opstr, stack<int> &opnum)
102 {
103     int result = 0;
104     int n=  opnum.top();
105 
106     opnum.pop();
107 
108     switch(opstr.top())
109     {
110     case '*': result = n*opnum.top();  opnum.pop();opstr.pop();opnum.push(result); break;
111     case '+': result = n+opnum.top();  opnum.pop();opstr.pop();opnum.push(result);break;
112     case '/': result = n/opnum.top();  opnum.pop();opstr.pop();opnum.push(result); break;
113     case '-': result = n-opnum.top();  opnum.pop();opstr.pop();opnum.push(result); break;
114     /*case '(': break;  
115     case ')':break;
116     case '\n':break;*/
117     default:break;
118     }
119 }
120 
121 int main(int argc,  char* argv[])
122 { 
123     char  str = 0;
124     int n = 0;
125     int result = 0;
126     stack<int>  opnum;
127     stack<char>  opstr;
128 
129     opstr.push('\n');           //存入一个回车字符
130 
131     while(str!='\n'||opstr.top()!='\n')
132     {
133         if(str>'0'&&str<'9')
134         {
135             opnum.push(str-48);
136             str = getchar();
137         }
138         
139         else
140         { 
141             if  (str=='+'||str=='-'||str=='*'||str=='/'||str=='\n'||str=='('||str==')')       //字符为四则运算符
142             {
143                 switch( priorcompare(opstr,opnum,str))
144                 {
145                    case '=': opstr.pop();  str = getchar();break;                                                  //遇到')'右刮号出栈
146                    case '>':  calc(opstr,opnum);  break;                                                    //大于等于,出栈      
147                    case '<': opstr.push(str);break;  str = getchar();   //小于,入栈
148                 }
149             }
150         }
151     }
152     
153     
154     cout<<opnum.top()<<endl;
155     return 0;
156 }

 

 

posted @ 2012-07-06 22:12  pipicfan  阅读(191)  评论(0)    收藏  举报