后缀表达式求值
  • 板块学术版
  • 楼主Ctjer
  • 当前回复7
  • 已保存回复7
  • 发布时间2021/6/12 14:12
  • 上次更新2023/11/4 21:59:08
查看原帖
后缀表达式求值
464170
Ctjer楼主2021/6/12 14:12
#include<bits/stdc++.h>
using namespace std;
string s;
stack<int> a;
int main()
{
	
	while(cin >> s&&s!="@"){
		if(s=="+"){
			int f=a.top();
			a.pop();
			int l=a.top();
			a.pop();
			a.push(f+l);
		}
		else if(s=="-"){
			int f=a.top();
			a.pop();
			int l=a.top();
			a.pop();
			a.push(l-f);
		}
		else if(s=="*"){
			int f=a.top();
			a.pop();
			int l=a.top();
			a.pop();
			a.push(f*l);
		}
		else if(s=="/"){
			int f=a.top();
			a.pop();
			int l=a.top();
			a.pop();
			a.push(l/f);
		}
		else{
			int k=0;
			int p=s.size()-1;
			while(p>=0){
				k=k*10+int(s[p]-'0');
				p--;
			}
			a.push(k);
		}
	}
	cout << a.top() << endl;
	return 0;
}

和P1449很像,只不过操作数末尾和操作符末尾都是空格

2021/6/12 14:12
加载中...