首页 > 代码库 > 求前缀表达式的值

求前缀表达式的值

  1 #include <stdlib.h>  2 #include <stdio.h>  3 #include <string.h>  4 #include <cstdio>  5 #include <stack>  6 #include <cmath>  7   8 using namespace std;  9 stack<int> instack; 10 stack<double> mystack; 11  12 void stack(stack<int> mystack) 13 { 14   if(mystack.size()>=0) 15     { 16       int n=mystack.top(); 17       printf("%d",n); 18       mystack.pop(); 19     } 20 } 21  22 int main() 23 { 24   25   int i=0; 26   int dotpos=0; 27   char str[40]; 28   while(gets(str)) 29     { 30       for(i=strlen(str)-1;i>=0;i--) 31     { 32       if(str[i]==.) 33         { 34           dotpos=instack.size(); 35           continue; 36         } 37  38       if(str[i]==+||str[i]==-||str[i]==*||str[i]==/) 39         { 40           if(str[i]==-&& instack.size()!=0) 41         { 42           int icount=0; 43           double itemp=0; 44           int len=instack.size(); 45           while(instack.size()>0) 46             { 47               double inttemp=instack.top(); 48               instack.pop(); 49               itemp=inttemp*pow(10,len-1-icount)+itemp; 50               icount++; 51             } 52           itemp=itemp*-1; 53           if(dotpos!=0) 54             { 55             mystack.push(itemp / pow(10,dotpos)); 56             dotpos=0; 57             } 58           else  59             mystack.push(itemp); 60           continue; 61  62         } 63  64            if(str[i]==+&& instack.size()!=0) 65         { 66           int icount=0; 67           double itemp=0; 68           int len=instack.size(); 69           while(instack.size()>0) 70             { 71               double inttemp=instack.top(); 72               instack.pop(); 73               itemp=inttemp*pow(10,len-1-icount)+itemp; 74               icount++; 75             } 76           if(dotpos!=0) 77             { 78             mystack.push(itemp / pow(10,dotpos)); 79             dotpos=0; 80             } 81           else  82             mystack.push(itemp); 83           continue; 84  85         } 86            if(mystack.size()<2) 87          { 88          printf("ERROR"); 89          return 0; 90              } 91            double a=mystack.top(); 92            mystack.pop(); 93            double b=mystack.top(); 94            mystack.pop(); 95            double r=0; 96            switch(str[i]) 97          { 98          case +: r=a+b; break; 99          case -:r=a-b; break;100          case *:r=a*b;break;101          case /:102               if(b==0)103                 {104                   printf("ERROR");105                   return 0;106                 }107               else r= a / b ;108               break;109          }110 111               mystack.push(r);112               continue;113         }114       if(str[i]== )115         {116                  int icount=0;117           double itemp=0;118           int len=instack.size();119           while(instack.size()>0)120             {121               double inttemp=instack.top();122               instack.pop();123               itemp=inttemp*pow(10,len-1-icount)+itemp;124               icount++;125             }126           if(len!=0)127             {128           if(dotpos!=0)129             {130             mystack.push(itemp / pow(10,dotpos));131             dotpos=0;132             }133           else 134             mystack.push(itemp);135             }136           continue;137         }138          139       if(str[i]>=0&&str[i]<=9)140         {141           instack.push(str[i]-0);142           continue;143 144         }145     }146       printf("%.1lf\n",mystack.top());147     }148 149 150   return 0;151 }

 

求前缀表达式的值