求助,栈模拟70分
  • 板块P1322 logo语言
  • 楼主wanttoac
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/25 10:09
  • 上次更新2024/3/25 15:40:31
查看原帖
求助,栈模拟70分
1258100
wanttoac楼主2024/3/25 10:09
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int to_num(string a) {
	int res = 0;
	for (int i = 0; i<a.length(); i++) {
		res *= 10;
		res += a[i] - '0';
	}
	return res;
}
int main() {
	string a;
	stack<string>op;
	getline(cin, a);
	for (int i = 0; i < a.length(); i++) {
		if (a[i] == 'R')op.push("*");
		else if (a[i] == 'F')op.push("+");
		else if (a[i] == 'B')op.push("-");
		else if (a[i] >= '0' && a[i] <= '9') {
			string num = "";
			int j;
			for (j = i;j<a.length()&& a[j] >= '0' && a[j] <= '9'; j++)num += a[j];
			op.push(num);
			i = j-1;
		}
	}
	long long res = 0;
	while (!op.empty()) {
		int num = to_num(op.top());
		op.pop();
		string sym = op.top();
		op.pop();
		if (sym == "+")res += num;
		else if (sym == "-")res -= num;
		else if (sym == "*")res *= num;
	}
	cout << res;
	return 0;
}
2024/3/25 10:09
加载中...