逆波兰式

来源:互联网 发布:犀牛软件最新版本 编辑:程序博客网 时间:2024/05/17 01:21
#include<iostream>
#include<stack>
using namespace std;
int main(void){
stack <int> sta;
char s[50];
int i , num, a, b, c, sum;
cin >> s;
i = 0;
while (s[i] != '\0'){
 num = s[i] - '0';
 
 if (num >= 0 && num <=  9){
  sta.push(num);
  
 }
 if (s[i] == '+'){
  a = sta.top();
  sta.pop();
  b = sta.top();
  sta.pop();
  c = b + a;
  sta.push(c);
  
 }
 if (s[i] == '-'){
  a = sta.top();
  sta.pop();
  b = sta.top();
  sta.pop();
  c = b - a;
  sta.push(c);
  
 }
 if (s[i] == '*'){
  a = sta.top();
  sta.pop();
  b = sta.top();
  sta.pop();
  c = b * a;
  sta.push(c);
  
 }
 if (s[i] == '/'){
  a = sta.top();
  sta.pop();
  b = sta.top();
  sta.pop();
  c = b / a;
  sta.push(c);
  
 }
 i++;
}
sum = sta.top();
cout << sum << endl;
cin.get();
system("pause");
return 0;
}
原创粉丝点击