文章

171

粉丝

0

获赞

710

访问

27.2k

头像
计算表达式 题解:

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

string str;
map<char,int> m={{'+',1},{'-',1},{'*',2},{'/',2}};

int work()
{
    stack<char> s;
    stack<int> num;
    string n="";
    for(int i=0;i<str.size();i++)
    {
        if(str[i]>='0'&&str[i]<='9')
        {
            n+=str[i];
            if(i==str.size()-1)
            num.push(stoi(n));
        }
        else
        {
            num.push(stoi(n));
            n="";
            if(s.empty())
            s.push(str[i]);
            else if(m[str[i]]>m[s.top()])
            s.push(str[i]);
            else{
                while(!s.empty()&&m[str[i]]<=m[s.top()])
                {
                    int b=num.top();
                    num.pop();
                    int a=num.top();
                    num.pop();
                    int c=0;
                    char op=s.top();
            ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发