| 比赛 | 
    板子大赛 | 
    评测结果 | 
    AAAAAAAAEE | 
    | 题目名称 | 
    表达式求值 | 
    最终得分 | 
    80 | 
    | 用户昵称 | 
    对立猫猫对立 | 
    运行时间 | 
    0.517 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    3.38 MiB  | 
    | 提交时间 | 
    2025-01-22 09:30:19 | 
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
char s[1000005];
stack<long long int> s1;
stack<char> s2;
int num = 0;
int val(char c)
{
	if(c == '+')
	{
		return 1;
	}
	else if(c == '-')
	{
		return 1;
	}
	else if(c == '*')
	{
		return 2;
	}
	else if(c == '/')
	{
		return 2;
	}
	else if(c == '^')
	{
		return 3;
	}
	else if(c == '(')
	{
		return 0;
	}
	else if(c == ')')
	{
		return 0;
	}
}
void cal()
{
	int b = s1.top();
	s1.pop();
	int a = s1.top();
	s1.pop();
	char op = s2.top();
	s2.pop();
	if(op == '+')
	{
		s1.push(a+b%10000);
	}
	else if(op == '*')
	{
		s1.push(a*b%10000);
	}
}
int main()
{
	freopen("expr2013.in","r",stdin);
	freopen("expr2013.out","w",stdout);
	cin >> s;
	int n = strlen(s);
	s[n] = '(';
	s[n+1] = ')';
	for(int i = 0;i < n+2;i++)
	{
		if(s[i] >= '0' && s[i] <= '9')
		{
			num = num * 10 + (s[i] - '0');
		}
		else if(s[i] == '+')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && val(s2.top()) >= val('+'))
			{
				cal();
			}
			s2.push('+');
		}
		else if(s[i] == '-')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && val(s2.top()) >= val('-'))
			{
				cal();
			}
			s2.push('-');
		}
		else if(s[i] == '*')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && val(s2.top()) >= val('*'))
			{
				cal();
			}
			s2.push('*');
		}
		else if(s[i] == '/')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && val(s2.top()) >= val('/'))
			{
				cal();
			}
			s2.push('/');
		}
		else if(s[i] == '^')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && val(s2.top()) >= val('^'))
			{
				cal();
			}
			s2.push('^');
		}
		else if(s[i] == '(')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			s2.push('(');
		}
		else if(s[i] == ')')
		{
			if(num)
			{
				s1.push(num);
				num = 0;
			}
			while(!s2.empty() && s2.top() != '(')
			{
				cal();
			}
			s2.pop();
		}
	}
	while(!s2.empty())
	{
		cal();
	}
	cout << s1.top() % 10000 <<endl;
	return 0;
}