栈进阶总结

· · 个人记录

U445194 后缀表达式求值

题意:

给出一个后缀表达式,求该表达式的值(如有除号,按 int 除法计算)。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
string x;
stack<int> s;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    while(cin>>x){
        if(x[0]=='@'){
            cout<<s.top();
            return 0;
        }                                //边界判断
        if(x[0]>='0'&&x[0]<='9'){
            int a=0;
            for(int i=0;i<x.size();i++){
                a=a*10+x[i]-'0';
            }
            s.push(a);
        }                                  //string和int的转换
        else{
            if(x[0]=='+'){
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                s.push(a+b);
            }
            if(x[0]=='-'){
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                s.push(b-a);
            }
            if(x[0]=='*'){
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                s.push(a*b);
            }
            if(x[0]=='/'){
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                s.push(b/a);
            }                    //四种运算
        }
    }
    return 0;
}

易错点:

在减法和除法中,要注意 b 和 a 的位置,不能写反。

U445233 表达式求值

题意:

给出一个只有加法和乘法的算术表达式,求该表达式的值。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int ans,a;
char c;
stack<int> s;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>a;
    s.push(a);
    while(cin>>c>>a){
        if(c=='+'){
            s.push(a);
        }                                       //如果是加法就直接入栈
        else{
            int x=s.top()*(a%10000)%10000;
            s.pop();
            s.push(x);
        }                                       //如果是乘法就先和栈顶计算后再入栈
    }
    while(!s.empty()){
        ans+=s.top();
        ans%=10000;
        s.pop();
    }                                            //因为没有乘法,只有加法,所以直接求和
    cout<<ans;
    return 0;
}

易错点:

记得答案要 %10000。

U445242

题意:

给出一个中缀表达式,求其对应的后缀表达式。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
stack<char> c;
signed main(){
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='('){
            c.push('(');
        }                                        //如果是左括号就直接入栈
        else if(s[i]==')'){
            while(!c.empty()&&c.top()!='('){
                cout<<c.top();
                c.pop();
            }
            c.pop();
        }                                        //如果是右括号就出栈,直到遇到左括号
        else if(s[i]=='+'||s[i]=='-'){
            //if(s[i]=='+' && c.size())cout<<c.top()<<   "--"<<endl;
            while(!c.empty()&&(c.top()=='+'||c.top()=='-'||c.top()=='*'||c.top()=='/')){
                cout<<c.top();
                c.pop();
            }
            c.push(s[i]);
        }                                        //加减法运算
        else if(s[i]=='*'||s[i]=='/'){
            while(!c.empty()&&(c.top()=='*'||c.top()=='/')){
                cout<<c.top();
                c.pop();
            }
            c.push(s[i]);
        }                                        //乘除法运算
        else{
            cout<<s[i];
        }                                         //否则是数字或字母就直接输出
    }
    while(!c.empty()){
        if(c.top()!='(')cout<<c.top();
        c.pop();
    }                                             //如果栈里还有元素,输出所有非左括号的元素
    return 0;
}

易错点:

在读到加减乘除法时,栈里可能有多个优先级大于或等于它的元素,要逐一输出,而且,每次要访问栈顶元素或出栈时,要先判断栈是否为空,不然会运行时错误。