蒟蒻求助后缀表达式qwq
  • 板块学术版
  • 楼主NightStriker
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/7/2 18:48
  • 上次更新2023/10/27 22:03:41
查看原帖
蒟蒻求助后缀表达式qwq
714084
NightStriker楼主2022/7/2 18:48

rt,在bdfs看的不是很懂,做了好几遍不是WA就是RE,用栈做的,有dalao帮忙看一下吗?

code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    stack<int> s;
    int a,b,num,ans = 0;
    string str;
    cin>>str;
    for (long long i = 0; i < str.length(); i++)
    {
        if (str[i] == '+'){
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(b + a); 
        }
        else if (str[i] == '-'){
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(b - a);
        }
        else if (str[i] == '*'){
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(b * a); 
        }
        else if (str[i] == '/'){
            a = s.top();  s.pop();
            b = s.top();  s.pop();
            s.push(b / a);
        }
        else if (str[i] == ' '){
            if (ans != 0)
                s.push(ans);
            ans = 0;
        }
        else{
            num = str[i] - '0';
            ans = ans * 10 + num; 
        } 
    }
    double ansans = s.top();
    printf("%.6lf\n", ansans);
    return 0;
}

在线等,急!

2022/7/2 18:48
加载中...