求助
查看原帖
求助
505484
冬笙夏洛_楼主2021/5/21 13:08
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	stack<ll>num;
	stack<char>ch;

	ll n;
	char c=0;
	//先输入一个数
	cin >> n;
	num.push(n);

	while (c != '\n')
	{
		//此后输入 一个字符 + 一个数字
		c = getchar();
		if (c == '\n')
			break;
		cin >> n;
		ch.push(c);
		num.push(n);
		//先把表达式中乘除法的先算了,然后就剩下加减法了
		if (c == '*' || c == '/')
		{
			ch.pop();
			ll x = num.top(); num.pop();
			ll y = num.top(); num.pop();
			
			c == '*' ? num.push(x * y) : num.push(y / x);
		
		}
	}
	//栈中只有加减法的运算了
	while (!ch.empty())
	{
		c = ch.top(); ch.pop();
		ll x = num.top(); num.pop();
		ll y = num.top(); num.pop();

		c == '+' ? num.push(x + y) : num.push(y - x);

	}
	cout << num.top()%10000 << endl;


	return 0;
}
2021/5/21 13:08
加载中...