为什么会错?

P1449 后缀表达式

lij123 @ 2023-07-24 06:33:53

#include<bits/stdc++.h>
using namespace std;
int main(){
    char ch;
    int a;
    stack<int> s;
    while(cin>>ch&&ch!='@'){
        if(ch>='0'&&ch<='9'){
            a=a*10+ch-'0';
        }
        if(ch=='.'){
            s.push(a);
            a=0;
        }
        if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
            int x=s.top();
            s.pop();
            int y=s.top();
            s.pop();
            if(ch=='+') s.push(x+y);
            if(ch=='-') s.push(y-x);
            if(ch=='*') s.push(x*y);
            if(ch=='/') s.push(y/x);
        }
    }
    cout<<s.top();
}

by coool @ 2023-08-06 13:05:24

第五行a没初始化吧?把a初始化成0试试?


by coool @ 2023-08-06 13:06:02

@lij123


by lij123 @ 2023-08-06 16:07:15

谢谢,没有问题了。AC记录


by lij123 @ 2024-05-12 11:34:38

@2023gdgz01 改进解除拉黑


|