全RE,救命!

P1449 后缀表达式

Y_QWQ_Y @ 2023-07-08 10:55:04

啊啊啊,我实在逝不明白了,上代码。

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

我真的是想骂人想哭 呜呜呜


by caolvchong_ @ 2023-07-08 11:10:45

看着好像没问题,你对照一下我的吧

#include<bits/stdc++.h>
using namespace std;
int main(){
    stack<long long>st;
    long long tmp=0,x,y;
    char ch;
    while((ch=getchar())!='@'){
        if(ch>='0'&&ch<='9'){
            tmp=tmp*10+ch-'0';
        }
        else if(ch=='.'){
            st.push(tmp);
            tmp=0;
        }
        else{
            x=st.top();
            st.pop();
            y=st.top();
            st.pop();
            if(ch=='+') st.push(y+x);
            else if(ch=='-') st.push(y-x);
            else if(ch=='*') st.push(y*x);
            else if(ch=='/') st.push(y/x);
        }
    }
    cout<<st.top()<<endl;
    return 0;
}

by caolvchong_ @ 2023-07-08 11:11:40

@Wyz2012dingding 有可能是没开long long


by Y_QWQ_Y @ 2023-07-08 11:17:35

ok谢谢


by Y_QWQ_Y @ 2023-07-08 11:18:53

但逝 long long 也全r了


by Ryanwu @ 2023-07-09 15:00:33

上代码

#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int main() {
    stack<int> s;
    string str;
    getline(cin,str);
    int len=str.length();
    for(int i=0;i<len;i++) {
        if(str[i]>='0' && str[i]<='9') {
            int num=0;
            while(str[i]>='0' && str[i]<='9') {
                num=num*10+str[i]-'0';
                i++;
            }
            s.push(num);
        } else {
            if(str[i]=='@') break;
            if(str[i]=='.') continue;
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            if(str[i]=='+') s.push(b+a);
            if(str[i]=='-') s.push(b-a);
            if(str[i]=='*') s.push(b*a);
            if(str[i]=='/') s.push(b/a);
        }
    }
    cout<<s.top()<<endl;
    return 0;
}

是不是你没考虑到多位数的情况???


by Ryanwu @ 2023-07-09 15:01:22

AC


by Y_QWQ_Y @ 2023-07-12 10:59:36

考虑了啊,是不是要用getline?


by Y_QWQ_Y @ 2023-07-12 11:23:00

ok,经过我严密的调试,终于AC了,奥利给,并且,谢谢上面的巨佬指点

#include<bits/stdc++.h>
using namespace std;
stack<int>s;
string c;
int main(){
    cin>>c;
    int sum=0;
    for(int i=0;c[i]!='@';i++){
        //都没人发现我原本在这里把sum归零了吗?
        if(c[i]>='0'&&c[i]<='9'){
            sum=sum*10+c[i]-'0';
            continue;
        }
        if(c[i]=='.'){
            s.push(sum);
            sum=0;
            continue;
        }
        int a,b;
        a=s.top();
        s.pop();
        b=s.top();
        s.pop();
        if(c[i]=='+')s.push(b+a);
        if(c[i]=='-')s.push(b-a);
        if(c[i]=='*')s.push(b*a);
        if(c[i]=='/')s.push(b/a);
    }
    cout<<s.top();
    return 0;
}

by b_bread @ 2023-08-26 10:29:40

减和除一定要b-a,b/a


|