fets全错了

P1449 后缀表达式

jianxing04 @ 2024-01-24 00:35:15

为什么用fgets全错了,改成用c++98的gets就对了

#include<bits/stdc++.h>
using namespace std;
int s[309];
char str[309];
void F(char a[])
{
    int top=0,i=0,tem=0;
    while(i<strlen(a)-2)
    {
        switch(a[i])
        {
            case '+':s[--top]+=s[top+1];break;
            case '-':s[--top]-=s[top+1];break;
            case '*':s[--top]*=s[top+1];break;
            case '/':s[--top]/=s[top+1];break;
            default :
                    tem=0;
                    while(str[i]!='.')
                    {
                        tem=tem*10+str[i]-'0';
                        i++;
                    }
                    s[++top]=tem;
        }
        i++;
    }
    cout<<s[1];
}
int main()
{  
    fgets(str,309,stdin);
    F(str);
    return 0;
}

by xiaoyuhao0503 @ 2024-02-01 12:37:21

会不会是换行符造成的?


by wpc_120318 @ 2024-02-03 14:29:47

@jianxing04 给你看一下我的代码```

include <bits/stdc++.h>

using namespace std;

stack<int> st;

string str;

void deal(char op) {

int a = st.top();

st.pop();

int b = st.top();

st.pop();

if (op == '+')

{

    st.push(b + a);

} 

else if (op == '-')
{
    st.push(b - a);    
}
else if (op == '*')
{
    st.push(b * a);
}
else 
{
    st.push(b / a);
}

} int main() {

cin >> str;

int num = 0;

for (int i = 0; i < str.size(); i++) {
    if(str[i] >= '0' && str[i] <= '9')
    {
        num = num * 10 + (str[i] - '0');
    }
    else if(str[i] == '.')
    {
        st.push(num);
        num = 0;
    }
    else if(str[i] != '@')
    {
        deal(str[i]);
    }
}
cout << st.top() << endl;
return 0;

}


 @[jianxing04](/user/1037870)

|