全T求调

P1449 后缀表达式

__yiLIUyi__ @ 2024-07-25 14:50:44

#include<bits/stdc++.h>
using namespace std;
stack<int>n;
char c;
string str;
int num,i,top,k;
int main(){
    cin>>str;
    for(c=str[k];c!='@';k++){
        if('9'>=c&&c>='0'){
            int a=c-'0';
            num=num*10+a;
            i=1;
        }
        else if(c=='.'){
            n.push(num);
            num=i=0;
            top++;
        }
        else{
            int a=n.top();
            n.pop();
            int b=n.top();
            n.pop();
            if(c=='+')n.push(b+a);
            if(c=='-')n.push(b-a);
            if(c=='*')n.push(b*a);
            if(c=='/')n.push(b/a);
        }
    }cout<<n.top();
}

by Bismuth_ @ 2024-07-25 15:01:33

@yiLIUyi 每个回合c要实时更新


by __yiLIUyi__ @ 2024-07-25 15:03:21

@yzx_mexxi for循环开头更新了啊


by Bismuth_ @ 2024-07-25 15:04:13

@yiLIUyi 是要随时更新,k++一次就更新一次


by __yiLIUyi__ @ 2024-07-25 15:06:09

@yzx_mexxi 现在没有输出了:)


by Bismuth_ @ 2024-07-25 15:08:19

@yiLIUyi ?代码发出来,我本地调了能过


by __yiLIUyi__ @ 2024-07-25 15:14:01

@yzx_mexxi

#include<bits/stdc++.h>
using namespace std;
stack<int>n;
char c;
string str;
int num,i,top,k;
int main(){
    cin>>str;
    for(;c!='@';k++,c=str[k]){
        if('9'>=c&&c>='0'){
            int a=c-'0';
            num=num*10+a;
            i=1;
        }
        else if(c=='.'){
            n.push(num);
            num=i=0;
        }
        else{
            int a=n.top();
            n.pop();
            int b=n.top();
            n.pop();
            if(c=='+')n.push(b+a);
            if(c=='-')n.push(b-a);
            if(c=='*')n.push(b*a);
            if(c=='/')n.push(b/a);
        }
    }cout<<n.top();
    return 0;
}

by Bismuth_ @ 2024-07-25 15:14:46

@yiLIUyi e,第一次循环c没有值


by __yiLIUyi__ @ 2024-07-25 15:16:03

@yzx_mexxi 哇谢谢dalao


by Bismuth_ @ 2024-07-25 15:16:43

@yiLIUyi 红联好评


by __yiLIUyi__ @ 2024-07-25 15:17:32

@yzx_mexxi 是我头图吗


| 下一页