stl栈实现计算表达式的值

王老师那么可爱(*╹▽╹*),讲了一节课计算表达式,我当然要完成作业啦,虽然网络编程课写这个很奇怪

欢迎大家debug~~~

首先是中缀转后缀表达式:

存储的数据结构是queue<node>,node.flag区分是操作数还是操作符

操作符的优先级由map<char,int>给出,只涉及+-*/(),支持括号嵌套,以及负数

case1: 如果是操作数,把数值读出来后扔进queue

case2: 如果是操作符,要先进操作符栈,被弹出的时候扔进queue

记当前和栈顶的操作符分别为i和st, 入栈和出栈的规则是:如果op[str[i]]>op[st],入栈

否则while(!s.empty()&&op[str[i]]<=op[st]&&op[st]!='(') ,一直弹出进队列  注意'('只能被右括号弹出

后缀表达式求值:

需要开一个操作数栈

case1: 遍历队列,如果遇到操作数,直接加入操作数栈

case2: 如果遇到操作符(只涉及+-*/),先从操作数栈中取第二操作数,再从栈中取第一操作数,计算完再塞进栈

栈中最后剩下的一个数就是答案

#include<iostream>
#include<cstdio>
#include<map>
#include<stack>
#include<queue>
#include<string>
using namespace std;
struct node
{
    int num;
    char op;
    int flag;
};
stack<char>s;       //操作符栈
stack<int>sn;       //操作数栈
queue<node>q;
map<char,int> op;
string str;
int cnt=0;
bool isnum(char ch)
{
    if(ch=='.'||ch>='0'&&ch<='9')
        return true;
    else return false;
}
void change()
{
    int len=str.size();
    int num;
    node tmp;
    for(int i=0;i<len;i++)
    {
        int pre=0;
        tmp.num=0;
        if((str[i]=='-'||str[i]=='+')&&(str[i-1]=='('||!i)||isnum(str[i]))        //输出负数
        {
            tmp.flag=1;
            if(str[i]=='-') pre=-1;
            else tmp.num=str[i]-'0';
            while(i+1<len&&isnum(str[i+1]))
            {
                i++;
                tmp.num=tmp.num*10+str[i]-'0';
            }
            if(pre==-1)
            {
                tmp.num*=-1;
                pre=0;
            }
            q.push(tmp);
        }

        else
        {
            tmp.flag=0;
            if(str[i]==')')
            {
                while(!s.empty()&&s.top()!='(')
                {
                    tmp.op=s.top();
                    q.push(tmp);
                    s.pop();
                }
                s.pop();
            }
            else if(s.empty()||op[str[i]]>op[s.top()])
            {
                s.push(str[i]);  //(在这里会被压入栈中
            }
            else
            {
                    while(!s.empty()&&s.top()!='('&&op[str[i]]<=op[s.top()])                     //不能把(弹出来
                    {
                        tmp.op=s.top();
                        q.push(tmp);
                        s.pop();
                    }
                    s.push(str[i]);
            }

        }
    }

    while(!s.empty())
    {
        tmp.flag=0;
        tmp.op=s.top();
        q.push(tmp);
        s.pop();
    }
}
int cal()
{
    int tmp1,tmp2,tmp;
    node cur;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur.flag==1)
            sn.push(cur.num);
        else
        {
            tmp2=sn.top();
            sn.pop();
            tmp1=sn.top();
            sn.pop();
            if(cur.op=='+') tmp=tmp1+tmp2;
            else if(cur.op=='-')  tmp=tmp1-tmp2;
            else if(cur.op=='*')  tmp=tmp1*tmp2;
            else if(cur.op=='/')  tmp=tmp1/tmp2;
            sn.push(tmp);
        }
    }
    return sn.top();
}
int main()
{
    op['*']=op['/']=1;
    op['+']=op['-']=0;
    op['(']=op[')']=2;
    cin>>str;
    change();
//    while(!q.empty())
//    {
//        node tmp=q.front();
//        q.pop();
//        if(tmp.flag==1) cout<<tmp.num<<endl;
//        else cout<<tmp.op<<endl;
//    }

    cout<<cal()<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iroy33/article/details/88357585