[luoguP1981] 表达式求值(U•ェ•*U)

传送门

弄两个栈,一个存数,一个存运算符,然后乱搞。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000001

int n, top_d, top_f;
int stack_d[N];
char s[N], stack_f[N];
bool b[N];

int main()
{
	int i, j, x;
	scanf("%s", s + 1);
	n = strlen(s + 1);
	i = 1;
	x = 0;
	while(isdigit(s[i]))
	{
		x = ((x << 1) + (x << 3) + s[i] - '0') % 10000;
		i++;
	}
	stack_d[++top_d] = x;
	stack_f[++top_f] = s[i];
	i++;
	while(i <= n)
	{
		x = 0;
		while(isdigit(s[i]))
		{
			x = ((x << 1) + (x << 3) + s[i] - '0') % 10000;
			i++;
		}
		stack_d[++top_d] = x;
		if(stack_f[top_f] == '*')
		{
			stack_d[top_f] = (stack_d[top_f] * stack_d[top_f + 1]) % 10000;
			top_f--;
			top_d--;
		}
		stack_f[++top_f] = s[i];
		i++;
	}	
	for(i = 1; i <= top_d; i++) stack_d[i] = (stack_d[i] + stack_d[i - 1]) % 10000;
	printf("%d
", stack_d[top_d]);
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhenghaotian/p/7080540.html