不知道哪里错了,求条

P1449 后缀表达式

shuifan @ 2024-10-04 10:42:40

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

by George_qwe @ 2024-10-04 16:27:39

@shuifan
没有退栈
z.pop();


by George_qwe @ 2024-10-04 16:38:57

bushi
你while死循环了


by yinzixia @ 2024-10-13 13:59:30

数字不一定是一位数


by yinzixia @ 2024-10-13 14:06:09

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

@shuifan


by yinzixia @ 2024-10-13 14:06:59

@shuifan 改了一点,可AC,求关


|