yuanwanzhao @ 2024-04-10 22:27:41
拿3.5.2.-*7.+@举例
正常结果:16
蒟蒻我的答案:28
(大脑爆炸)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
stack <int> a;
string s;
int main(){
int cn=0,i=0;
bool b=0;
cin>>s;
while(b!=1){
if(s[i]>='0'&&s[i]<='9'){a.push(s[i]-48);cout<<s[i]<<" ";}
else if(s[i]=='+'){int n=a.top(); a.pop(); cn+=a.top()+n; n=a.top()+n; a.pop(); a.push(n);}
else if(s[i]=='-'){int n=a.top(); a.pop(); cn+=a.top()-n; n=a.top()-n; a.pop(); a.push(n);}
else if(s[i]=='*'){int n=a.top(); a.pop(); cn+=a.top()*n; n=a.top()*n; a.pop(); a.push(n);}
else if(s[i]=='/'){int n=a.top(); a.pop(); cn+=a.top()/n; n=a.top()/n; a.pop(); a.push(n);}
else if(s[i]=='.'){}
else{b=1;}
i++;
cout<<i<<" : "<<cn<<"\n";
}
cout<<" ";
cout<<cn;
return 0;
}
by 红黑树 @ 2024-04-10 22:48:32
@yuanwanzhao
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
stack <int> a;
string s;
int main(){
int cn=0,i=0;
bool b=0;
cin>>s;
while(b!=1){
if(s[i]>='0'&&s[i]<='9'){a.push(s[i]-48);cout<<s[i]<<" ";}
else if(s[i]=='+'){int n=a.top(); a.pop(); cn=a.top()+n; n=a.top()+n; a.pop(); a.push(n);}
else if(s[i]=='-'){int n=a.top(); a.pop(); cn=a.top()-n; n=a.top()-n; a.pop(); a.push(n);}
else if(s[i]=='*'){int n=a.top(); a.pop(); cn=a.top()*n; n=a.top()*n; a.pop(); a.push(n);}
else if(s[i]=='/'){int n=a.top(); a.pop(); cn=a.top()/n; n=a.top()/n; a.pop(); a.push(n);}
else if(s[i]=='.'){}
else{b=1;}
i++;
cout<<i<<" : "<<cn<<"\n";
}
cout<<" ";
cout<<cn;
return 0;
}