QWQ_jyc @ 2023-07-05 10:27:59
#include <bits/stdc++.h>
using namespace std;
stack<int>s;
int m,n,e;
char a;
int main(){
while(cin>>a){
if(a=='@') break;
if(a>='0'&&a<='9'){
s.push(a);
}
if(a=='+'){
m=s.top();
s.pop();
n=s.top();
s.pop();
s.push(n+m);
continue;
}else if(a=='-'){
m=s.top();
s.pop();
n=s.top();
s.pop();
s.push(n-m);
continue;
}else if(a=='*'){
m=s.top();
s.pop();
n=s.top();
s.pop();
s.push(m*n);
continue;
}else if(a=='/'){
m=s.top();
s.pop();
n=s.top();
s.pop();
s.push(n/m);
continue;
}else if(a=='.'){
s.push(e);
e=0;
continue;
}else {
e=e*10+a-'0';
continue;
}
}
cout<<s.top();
return 0;
}
by SJZ2010 @ 2023-07-05 10:57:24
@QWQ_jyc
答案和计算过程中的每一个值的绝对值不超过
10^9
要用类似于快读的方法读入数字,不能读一个压一个
by QWQ_jyc @ 2023-07-05 10:57:56
@SJZ2010 我试试
by QWQ_jyc @ 2023-07-05 10:59:56
@SJZ2010 用getchar()吗
by QWQ_jyc @ 2023-07-05 11:06:20
@SJZ2010
#include <bits/stdc++.h>
using namespace std;
stack<int>s;
int n,m;
char a;
int main(){
while(cin>>a;&&a!='@'){
if(a>='0'&&a<='9'){
int num=0;
while(true){
if(a=='.') break;
num=num*10+a-'0';
}
s.push(num);
}else if(a=='+'){
n=s.top();
s.pop();
m=s.top();
s.pop();
s.push(n+m);
continue;
}else if(a=='-'){
n=s.top();
s.pop();
m=s.top();
s.pop();
s.push(m-n);
continue;
}else if(a=='*'){
n=s.top();
s.pop();
m=s.top();
s.pop();
s.push(m*n);
continue;
}else if(a=='/'){
n=s.top();
s.pop();
m=s.top();
s.pop();
s.push(m/n);
continue;
}
}
cout<<s.top();
return 0;
}
by SJZ2010 @ 2023-07-05 11:24:26
@QWQ_jyc
改出来的程序有以下问题
while
括号中有个 ;
快读有问题,会死循环,要改成这样
if(a>='0'&&a<='9'){
int num=0;
while(true){
if(a=='.') break;
num=num*10+a-'0';
a=getchar();
}
s.push(num);
}
by QWQ_jyc @ 2023-07-05 11:25:39
@SJZ2010 xht