中缀表达式 和 后缀表达式求值题解

题目1/2 :中缀表达式求值模板题

注意: while(op.size() && grade[op.top()]>=grade[str[i]]) //注意是while

#include<iostream>
using namespace std;
#include<string>
#include<map>
#include<stack>

stack<int> num;
stack<char> op;

map<char,int> grade;

void init()  //优先级 
{
    grade['-']=2;
    grade['+']=2;
    grade['*']=3;
    grade['/']=3;
}

int eval()  //一次计算 
{
    int b=num.top();
    num.pop();
    int a=num.top();
    num.pop();
    
    char opp=op.top();
    op.pop();
    
    int ans;
    
    switch(opp)
    {
        case '+':
        ans=a+b;
        break;
        
        case '-':
        ans=a-b;
        break;
        
        case '*':
        ans=a*b;
        break;
        
        case '/':
        ans=a/b;
        break;
    }
    
    num.push(ans);
    return ans;
    
}


int main()
{
	init();
    string str;
    getline(cin,str);
    

    for(int i=0;i<str.size();i++)
    {
        if(str[i]>='0' && str[i]<='9')
        {
            int ans=0;
            
            while(i<str.size() && str[i]>='0' && str[i]<='9')
            {
                ans*=10;
                ans+=(str[i]-'0');
                i++;
            }
            i--;
            num.push(ans);
//            cout<<"push:"<<ans;
            continue;
        }
        
        if(str[i]=='(')
        {
            op.push('(');
//            cout<<"push:(";
            continue;
        }
        
        if(str[i]==')')
        {
            while(op.top()!='(')
            {
                eval();
                
            }
            op.pop();
            continue;
        }
        
       while(op.size() && grade[op.top()]>=grade[str[i]])  //注意是while 
    		eval();
        
        op.push(str[i]);
    }
    
    while(op.size())
    {
        eval();
    }
    
    cout<<num.top();
    
    return 0;
}

题目2/2 逆波兰表达式求值

class Solution {
public:
    stack<int> num;
    stack<int> op;

    int judge(string ss)
    {
        if(ss=="+")
            return 1;
        if(ss=="-")
            return 2;
        if(ss=="*")
            return 3;
        if(ss=="/")
            return 4;

        return 0;
    }

    int eval()
    {
        int b=num.top();
        num.pop();
        int a=num.top();
        num.pop();

        int opp=op.top();
        op.pop();

        switch(opp)
        {
            case 1:
                return a+b;
            break;
            case 2:
                return a-b;
            break;
            case 3:
                return a*b;
            break;
            case 4:
                return a/b;
            break;
        }
        return 0;
    }

    int str_num(string ss)  //注意可能有负数
    {
        int oo=1;
        if(ss[0]=='-')
        {
            oo=-1;
            ss=ss.substr(1,ss.size()-1);
        }

        int ans=0;

        for(int i=0;i<ss.size();i++)
        {
            ans*=10;
            ans+=(ss[i]-'0');
        }
        return ans*oo;
    }
    int evalRPN(vector<string>& tokens) {

        while(!num.empty())
            num.pop();
        
        while(!op.empty())
            op.pop();

        for(int i=0;i<tokens.size();i++)
        {
            int type=judge(tokens[i]);

            if(!type)
            {
                num.push(str_num(tokens[i]));
                continue;
            }

            op.push(type);
            
            int las=eval();
            num.push(las); //可以push值加在eval运算里面

        }
        return num.top();
  
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值