不知为何RE

P1449 后缀表达式

lizer @ 2019-07-09 14:53:12

#include <bits/stdc++.h>
using namespace std;
stack<int> num;
char a[100001];
int s1,s2,temp,i;
int turn()
{
    int s=0;
    while(a[i]>='0'&&a[i]<='9')
    {
        s=s*10+a[i]-'0';
        i++;
    }
    return s;   
}
int main()
{
    while(cin>>a[i])
    {
        if(a[i]>='0'&&a[i]<='9')
            {
                temp=turn();
                num.push(temp);
            }
        else if(a[i]!='.')
        {
            switch(a[i])
            {
                case '+':s1=num.top();
                num.pop();
                s2=num.top();
                num.pop();
                s2+=s1;
                num.push(s2);
                break;
                case '-':s1=num.top();
                num.pop();
                s2=num.top();
                num.pop();
                s2-=s1;
                num.push(s2);
                break;
                case '*':s1=num.top();
                num.pop();
                s2=num.top();
                num.pop();
                s2*=s1;
                num.push(s2);
                break;
                case '/':s1=num.top();
                num.pop();
                s2=num.top();
                num.pop();
                s2/=s1;
                num.push(s2);
                break;
                case '@':cout<<num.top();
                return 0;
            }
        }
        i++;
    }
    return 0;
}

by lizer @ 2019-07-09 14:53:53

RT 求助各位dalao qwq


by HZAUqzx @ 2019-07-30 19:01:42

@lizer

while(cin)的时候就读了一个字但是turn()的时候一直让i++他当然要炸啦


by HZAUqzx @ 2019-07-30 19:08:07

@lizer

你要是想内位的话就 把turn()融合到循环里

像这样

( 我嫌STL慢 用数组模拟stack了 )

//luogu1499.getchar()
//后缀表达式
#include <bits/stdc++.h>
int p, s[1001], top;
char cc;
int main(){
    while(true){
        cc = getchar();
        if(cc<='9' && cc>='0') p = p*10 + cc - '0';
        else if(cc == '.') s[++top] = p , p = 0;
        else if(cc == '+') s[--top] += s[top+1];
        else if(cc == '-') s[--top] -= s[top+1];
        else if(cc == '*') s[--top] *= s[top+1];
        else if(cc == '/') s[--top] /= s[top+1];
        else if(cc == '@') printf("%d", s[top]), exit(0);
}   }

by lizer @ 2019-07-30 21:06:23

@1700qiaozixuan 虽然早A了 但还是蟹蟹大佬qwq


|