nobody_here @ 2024-08-01 21:37:05
#include<bits/stdc++.h>
using namespace std;
int main(){
stack<char> a;
char w;
int n=0,m=0,y=0;
cin>>w;
do{
if(w=='/' || w=='*' || w=='-' || w=='+'){
n=a.top()-'0';
a.pop();
m=a.top()-'0';
a.pop();
if(w=='/'){
y=n/m;
a.push(y+'0');
}else if(w=='*'){
y=n*m;
a.push(y+'0');
}else if(w=='-'){
y=n-m;
a.push(y+'0');
}else{
y=n+m;
a.push(y+'0');
}
}else if(w=='.'){
/*37*/
}else{
a.push(w);
}
cin>>w;
}while(w=='@');
cout<<a.top()-'0';
return 0;
}
实在写不出了,dalao们救救我
by zzhhyy1234 @ 2024-08-04 11:14:56
上面那条的c改成w
by zzhhyy1234 @ 2024-08-04 11:18:07
再删去原程序所有-'0'和+'0'(不包括上面加的)
by zzhhyy1234 @ 2024-08-04 11:18:27
就ok了!
by zzhhyy1234 @ 2024-08-04 11:19:46
贴最终代码(亲测AC)
#include<bits/stdc++.h>
using namespace std;
int main(){
stack<int> a;
char w;
int n=0,m=0,y=0,x=0,flag=0;
cin>>w;
do{
if(w=='/' || w=='*' || w=='-' || w=='+'){
m=a.top();
a.pop();
n=a.top();
a.pop();
if(w=='/'){
y=n/m;
a.push(y);
}else if(w=='*'){
y=n*m;
a.push(y);
}else if(w=='-'){
y=n-m;
a.push(y);
}else{
y=n+m;
a.push(y);
}
}else if(w=='.'){
a.push(x);
x=0;
flag=0;
}else{
if(flag==0){
flag=1;
x=w-'0';
}
else{
x*=10;
x+=w-'0';
}
}
cin>>w;
}while(w!='@');
cout<<a.top();
return 0;
}
求关
by zzhhyy1234 @ 2024-08-04 11:21:40
还有我自己的代码,可以参考
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<int> a;
int x;
int flag=0;
char c;
while(scanf("%c",&c)!='@'){
if(c=='@') break;
if(isdigit(c)){
if(flag==0){
flag=1;
x=c-'0';
}
else{
x*=10;
x+=c-'0';
}
}
else if(c=='.'){
a.push(x);
x=0;
flag=0;
}
else{
int z=a.top();
a.pop();
int y=a.top();
a.pop();
switch(c){
case '+': a.push(y+z); break;
case '-': a.push(y-z); break;
case '*': a.push(y*z); break;
case '/': a.push(y/z); break;
}
}
}
cout<<a.top();
return 0;
}
by nobody_here @ 2024-08-07 07:09:02
大佬牛逼,小弟膜拜 @zzhhyy1234
by PrOI_Luo_Yi_cheng @ 2024-08-13 08:56:24
#include <bits/stdc++.h>
using namespace std;
string s;
stack<int> a;
int main(){
cin >> s;
int cur = 0;
for (int i = 0; i < s.size() - 1; i++){
if (s[i] >= '0' && s[i] <= '9')
cur = cur * 10 + (s[i] - '0');
else if (s[i] == '.'){
a.push(cur);
cur = 0;
}
else{
int left = a.top();
a.pop();
int right = a.top();
a.pop();
if (s[i] == '+')
a.push(left + right);
if (s[i] == '-')
a.push(right - left);
if (s[i] == '*')
a.push(left * right);
if (s[i] == '/')
a.push(right / left);
}
}
cout << a.top();
return 0;
}