4.2栈 给你一串字符串,用逆波兰表示的 计算结果
如1 2 + 3 4 - * = -3
1.atoi可以返回字符串的数值 如字符型12返回12。定义在#include;
2.处理两位数的时候,就不能用getchar 只能用字符串,然后字符串第一个是算法符号就处理
#include
#include
#include
#include
#include
using namespace std;
int main(){ stack q; char s[10000];int a,b;
while((scanf("%s",s))!=EOF){
if(s[0]=='+'){ a=q.top();q.pop();b=q.top();q.pop();
q.push(a+b);
}
else if(s[0]=='-') {a=q.top();q.pop();b=q.top();q.pop();
q.push(b-a);
}
else if(s[0]=='*') { a=q.top();q.pop();b=q.top();q.pop();
q.push(a*b);
}
else { q.push(atoi(s));
}
} cout
关注
打赏