思路
用栈来模拟操作,遇到符号就取栈顶的两个元素,进行操作。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
char ch[2010];
int stack[2010], p;
int main(){
scanf("%s", ch+1);
int len = strlen(ch+1), num = 0;
for(int i = 1; i <= len; i ++){
char c = ch[i];
if(c == '@'){
printf("%d", stack[p]);
return 0;
}
if(c <= '9' && c >= '0') num = num * 10 + c - '0';
else if(c == '.') stack[++p] = num, num = 0;
else if(c == '+'){
int t1 = stack[p--];
int t2 = stack[p--];
stack[++p] = t1+t2;
}
else if(c == '-'){
int t1 = stack[p--];
int t2 = stack[p--];
stack[++p] = t2-t1;
}
else if(c == '*'){
int t1 = stack[p--];
int t2 = stack[p--];
stack[++p] = t1*t2;
}
else if(c == '/'){
int t1 = stack[p--];
int t2 = stack[p--];
stack[++p] = t2/t1;
}
}
return 0;
}