首页 > 其他分享 >栈

时间:2024-01-17 15:11:13浏览次数:24  
标签: int top else ans stk op

3302. 表达式求值 - AcWing题库

这是一道中缀表达式;有括号提高了这题的难度(个人认为),模拟我感觉不是特别好做;

#include <bits/stdc++.h>
using namespace std;
stack<int>stk;
stack<char>op;
unordered_map<char , int> pr = {{'+' , 1} , {'-' , 1} , {'*' , 2} , {'/' , 2}};

void eval()
{
    //注意取出来的顺序;因为是先进后出,所以第一个取出来的数是在后面的,后取出来的数是在前面的;
    //(1 / 2) 先取出来的是2,后取出来的是1;
    //对除法和减法有影响;
    auto b = stk.top(); stk.pop();
    auto a = stk.top(); stk.pop();
    //字符
    auto c = op.top(); op.pop();
    
    int ans = 0;
    if(c == '+') ans = a + b;
    else if(c == '-') ans = a - b;
    else if(c == '*') ans = a * b;
    else ans = a / b;
    
    stk.push(ans);
}

int main()
{
    string a;
    cin >> a;
    
    for(int i = 0 ; i < a.size() ; i ++)
    {
        auto p = a[i];
        if(isdigit(p))//如果是数字的话
        {
            int x = 0;
            int j = i;
            while(j < a.size() && isdigit(a[j])) x = x * 10 + a[j ++] - '0';
            i = j - 1;
            stk.push(x);
        }
        else if(p == '(') op.push(p);
        else if(p == ')')
        {
            while(op.top() != '(') eval();
            op.pop(); //将左括号 删除;
        }
        else 
        {
            while(op.size() && pr[op.top()] >= pr[p]) eval();
            //如果op里面弹出来的运算符等级 大于等于 当前的运算符等级,就进行计算;直到小于这个运算符等级;
            op.push(p);
        }
    }
    while(op.size()) eval(); //如果里面还有运算符 没进行完的话,在进行一遍;
    cout << stk.top() << endl;
    return 0;
}

P1449 后缀表达式 - 洛谷

对于这一题,没有括号,就是后缀表达式,比较简单;

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
    int m = 0;
    int sum = 0;
    stack<int>stk;
    while(1)
    {
        char c;
        cin >> c;
        
        if(c == '@') break; //如果是@就结束;
        if(c >= '0' && c <= '9')//如果是数字 就进行换算求一下
        {
            c = c -'0';
            m = m * 10 + c;
        }
        if(c == '.')//遇到'.'就把换算的数字放进栈里面,
        {
            stk.push(m);
            m = 0;//记得清零;
        }
        if(c == '+')
        {
            int st1 = stk.top();
            stk.pop();
            int st2 = stk.top();
            stk.pop();
            int ans = st2 + st1;
            stk.push(ans);
            sum = ans;
        }
        else if(c == '-')
        {
            int st1 = stk.top();
            stk.pop();
            int st2 = stk.top();
            stk.pop();
            int ans = st2 - st1;
            stk.push(ans);
            sum = ans;
        }
        else if(c == '*')
        {
            int st1 = stk.top();
            stk.pop();
            int st2 = stk.top();
            stk.pop();
            int ans = st2 * st1;
            stk.push(ans);
            sum = ans;
        }
        else if(c == '/')
        {
            int st1 = stk.top();
            stk.pop();
            int st2 = stk.top();
            stk.pop();
            int ans = st2 / st1;
            stk.push(ans);
            sum = ans;
        }
    }
    cout << sum << endl;
}

标签:,int,top,else,ans,stk,op
From: https://www.cnblogs.com/volapex-lord/p/17970065

相关文章