a2lyaXNhbWUgbWFyaXNh @ 2022-07-18 09:27:24
由于本人患有重度STL依赖症,所以用的是STL中的Stack,并没有手打栈,结果样例会输出一个4位数,帮忙看一下哪错了%%%
by a2lyaXNhbWUgbWFyaXNh @ 2022-07-18 09:27:48
代码:
#include<bits/stdc++.h>
using namespace std;
char s[100];
int tmp,opnmX,opnmY;
stack<int> line;
int main(){
scanf("%s",s);
for(int i=0;i<strlen(s)-1;i++){
if(s[i]>='0'&&s[i]<='9'){
tmp=tmp*10+s[i]-'0';
}else if(s[i]=='.'){
line.push(tmp);
}else{
switch(s[i]){
case '+':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY+opnmX);
break;
case '-':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY-opnmX);
break;
case '*':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY*opnmX);
break;
case '/':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY/opnmX);
break;
}
}
}
printf("%d",line.top());
return 0;
}
by 喵仔牛奶 @ 2022-07-18 09:53:21
#include<bits/stdc++.h>
using namespace std;
char s[100];
int tmp,opnmX,opnmY;
stack<int> line;
int main(){
scanf("%s",s);
for(int i=0;i<strlen(s)-1;i++){
if(s[i]>='0'&&s[i]<='9'){
tmp=tmp*10+s[i]-'0';
}else if(s[i]=='.'){
line.push(tmp),tmp=0;
}else{
switch(s[i]){
case '+':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY+opnmX);
break;
case '-':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY-opnmX);
break;
case '*':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY*opnmX);
break;
case '/':
opnmX=line.top();
line.pop();
opnmY=line.top();
line.pop();
line.push(opnmY/opnmX);
break;
}
}
}
printf("%d",line.top());
return 0;
}
by 喵仔牛奶 @ 2022-07-18 09:53:42
@S__B tmp要清零