chen721

· · 个人记录

U445194 栈进阶--3 后缀表达式求值

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
stack<int>st;
signed main()
{
    string c;
    while(cin>>c){
        if(c=="@"){
            cout<<st.top();//输出值
            return 0;
        }
        if(c=="*"){
            int y=st.top();//倒过来
            st.pop();
            int x=st.top();
            st.pop();
            st.push(x*y);
        }
        else if(c=="/"){
            int y=st.top();
            st.pop();
            int x=st.top();
            st.pop();
            st.push(x/y);
        }
        else if(c=="+"){
            int y=st.top();
            st.pop();
            int x=st.top();
            st.pop();
            st.push(x+y);
        }
        else if(c=="-"){
            int y=st.top();
            st.pop();
            int x=st.top();
            st.pop();
            st.push(x-y);
        }
        else st.push(stoi(c));//转数字
    }
    return 0;
}

U445233 栈进阶--5 表达式求值

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
stack<int>st;
signed main()
{   
    char s;
    int a,num=0;
    cin>>a;
    st.push(a%10000);
    while(cin>>s>>a){
        if(s=='+')st.push(a%10000);
        else{
            int x=st.top();
            st.pop();
            st.push((x%10000)*(a%10000));//乘优先级高
        }
    }
    while(st.size()){
        int x=st.top();
        num+=x;
        num%=10000;
        st.pop();
    }
    cout<<num%10000;
    return 0;
}

U445242 栈进阶--6 中缀转后缀表达式

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
stack<char>st;
map<char,int>mp;
signed main()
{   
    string s;
    cin>>s;
    mp['(']=0,mp['+']=1,mp['-']=1,mp['*']=2,mp['/']=2;//优先级
    fr(i,0,s.size()-1){
        if(s[i]=='('){
            st.push(s[i]);
        }
        else if(s[i]==')'){
            while(st.top()!='('){
                cout<<st.top();
                st.pop();
            }st.pop();//左括号P1739 表达式括号匹配

        }
        else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){
            while(st.size()&&mp[s[i]]<=mp[st.top()]){//特判
                cout<<st.top();
                st.pop();
            }
            st.push(s[i]);
        }
        else{
            cout<<s[i];
        }
    }
    while(!st.empty()){
        cout<<st.top();
        st.pop();
    }
    return 0;
}

P1739 表达式括号匹配

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
stack<int>st;
signed main()
{
    char c;
    while(cin>>c){
        if(c=='@'){
            if(st.size()==0)cout<<"YES";
            else cout<<"NO";
            return 0;
        }
        if(c=='(')st.push(1);
        if(c==')'){
            if(st.size())
            st.pop();
            else{
                cout<<"NO";
            return 0;
            }
        }
    }
    return 0;
}

P1981 [NOIP 2013 普及组] 表达式求值

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
stack<int>st;
signed main()
{   
    char s;
    int a,num=0;
    cin>>a;
    st.push(a%10000);
    while(cin>>s>>a){
        if(s=='+')st.push(a%10000);
        else{
            int x=st.top();
            st.pop();
            st.push((x%10000)*(a%10000));
        }
    }
    while(st.size()){
        int x=st.top();
        num+=x;
        num%=10000;
        st.pop();
    }
    cout<<num%10000;
    return 0;
}