#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<string>
#include<stdio.h>
using namespace std;
/*
后缀表达式计算:是数时压入,是运算符时依次弹出右,左操作数,运算完压回去。
最后得到的即是 空格分隔的数字注意
*/
double opera(char op,double a,double b){
if(op=='+') return a+b;
if(op=='-') return a-b;
if(op=='*') return a*b;
if(op=='/') return a/b;
}
int main()
{
//freopen("E:\\learn\\inputs.txt","r",stdin);
string a;
getline(cin,a);
stack<double> s;
for(int i=0;i<a.length();){
if(a[i]>='0' && a[i]<='9'){
s.push(a[i]-'0');
}
else{
int num2=s.top();
s.pop();
int num1=s.top();
s.pop();
int c=opera(a[i],num1,num2);
s.push(c);
}
}
cout<<s.top();
return 0;
}
后缀表达式用栈求值
最新推荐文章于 2023-09-12 20:13:33 发布