死循环了

P1449 后缀表达式

HardBlackPencil @ 2023-09-03 13:19:54

#include <bits/stdc++.h>
#include <stack>
using namespace std;

string s;
stack<int> zhan;

int main()
{
    cin >> s;
    char s1 = '.', s2 = '+', s3 = '-', s4 = '*', s5 = '/';
    int number = 0;
    char f;
    while ((f = getchar()) != '@')
    {
        if (f == s1)
        {
            zhan.push(number);
            number = 0;
        }
        else if (f == s2)
        {
            int b = zhan.top();
            zhan.pop();
            int a = zhan.top();
            zhan.pop();
            number = a + b;
            zhan.push(number);
            number = 0;
        }
        else if (f == s3)
        {
            int b = zhan.top();
            zhan.pop();
            int a = zhan.top();
            zhan.pop();
            number = a - b;
            zhan.push(number);
            number = 0;
        }
        else if (f == s4)
        {
            int b = zhan.top();
            zhan.pop();
            int a = zhan.top();
            zhan.pop();
            number = a * b;
            zhan.push(number);
            number = 0;
        }
        else if (f == s5)
        {
            int b = zhan.top();
            zhan.pop();
            int a = zhan.top();
            zhan.pop();
            number = a / b;
            zhan.push(number);
            number = 0;
        }
        else
            number = number * 10 + (int)f;
    }
    cout << zhan.top();
    return 0;
}

大佬们帮我看看这道题咋就死循环了o(╥﹏╥)o


by Genshineer @ 2023-09-03 13:27:20

@HardBlackPencil 你cin>>s;之后又去f=getchar()干什么???


by Genshineer @ 2023-09-03 13:27:38

cin>>s;删了


by Genshineer @ 2023-09-03 13:29:36

再把number = number * 10 + (int)f;改成number = number * 10 + (int)f-48;


by xiayuanhong @ 2023-09-03 13:38:10

啊对对对


by HardBlackPencil @ 2023-09-03 14:08:19

@long_long_integer谢谢,而cin>>s是个历史遗留问题


by HardBlackPencil @ 2023-09-03 14:10:20

@long_long_integer (int)(f-48)吧?


by HardBlackPencil @ 2023-09-03 14:13:18

thanx, 大佬们


by Genshineer @ 2023-09-03 18:19:02

@HardBlackPencil 加不加括号是一样的(


|