码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-06-28 21:58:08      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ref   www   efi   tps   div   targe   传送门   main   pre   

传送门

 

超级大模拟。。

 

代码

#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] 计算器的改良(模拟)

标签:ref   www   efi   tps   div   targe   传送门   main   pre   

原文地址:http://www.cnblogs.com/zhenghaotian/p/7091485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!