首页 > 代码库 > [luoguP1022] 计算器的改良(模拟)

[luoguP1022] 计算器的改良(模拟)

传送门

 

超级大模拟。。

 

代码

#include <cstdio>#include <cstring>#include <iostream>#define isword(x) ((x) >= ‘a‘ && (x) <= ‘z‘)int n, a1, b1, a2, b2, f, x;char s[1001], c;int main(){	int i;	scanf("%s", s + 1);	n = strlen(s + 1);	i = f = 1;	while(i <= n)	{		if(!isdigit(s[i]))		{			if(isword(s[i]))			{				c = s[i];				i++;				continue;			}			if(isdigit(s[i - 1]) && i != 1) b1 += x * f;			if(!isdigit(s[i - 1]) && i != 1)			{				if(!isdigit(s[i - 2])) a1 += f;				else a1 += x * f;			}			if(s[i] == ‘+‘) f = 1;			if(s[i] == ‘-‘) f = -1;			if(s[i] == ‘=‘) break;			i++;			}		x = 0;		while(isdigit(s[i])) x = (x << 1) + (x << 3) + s[i++] - ‘0‘;	}	i++;	f = 1;	while(i <= n)	{		if(!isdigit(s[i]))		{			if(isword(s[i]))			{				c = s[i];				i++;				continue;			}			if(isdigit(s[i - 1]) && s[i - 1] != ‘=‘) b2 += x * f;			if(!isdigit(s[i - 1]) && s[i - 1] != ‘=‘)			{				if(!isdigit(s[i - 2])) a2 += f;				else a2 += x * f;			}			if(s[i] == ‘+‘) f = 1;			if(s[i] == ‘-‘) f = -1;			i++;		}		x = 0;		while(isdigit(s[i])) x = (x << 1) + (x << 3) + s[i++] - ‘0‘;	}	if(isdigit(s[i - 1])) b2 += x * f;	if(!isdigit(s[i - 1]))	{		if(!isdigit(s[i - 2])) a2 += f;		else a2 += x * f;	}	a1 -= a2;	b2 -= b1;	printf("%c=", c);	if(b2 == 0) puts("0.000");	else printf("%.3lf\n", double(b2) / double(a1));	return 0;}

  

[luoguP1022] 计算器的改良(模拟)