C++ 求后缀表达式的值

2014-11-24 02:39:55 · 作者: · 浏览: 1
#include   
#include   
#include   
#include   
  
using namespace std;  
  
double eva lpostfix()  
{  
    stack s;  
    string token;  
    double a,b,result;  
    cin>>token;  
    while(token[0]!='=')  
    {  
        result=atof(token.c_str());  
        if(result!=0.0)  
            s.push(result);  
        else if(token=="0.0")  
            s.push(result);  
        else  
            switch(token[0])  
        {  
        case '+':  
            a=s.top();  
            s.pop();  
            b=s.top();  
            s.pop();  
            s.push(a+b);  
            break;  
        case '-':  
            a=s.top();   
            s.pop();   
            b = s.top();  
            s.pop();   
            s.push(a-b);   
            break;  
        case '*':  
            a=s.top();   
            s.pop();   
            b = s.top();  
            s.pop();   
            s.push(a*b);   
            break;  
        case '/':  
            a=s.top();   
            s.pop();   
            b = s.top();  
            s.pop();   
            s.push(a/b);   
            break;  
        case '^':  
            a=s.top();   
            s.pop();   
            b = s.top();  
            s.pop();   
            s.push(exp(a*log(b)));   
            break;  
        }  
        cin>
>token; } return s.top(); } int main() { eva lpostfix(); return 0; }