zoukankan      html  css  js  c++  java
  • [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
    ", double(b2) / double(a1));
    	return 0;
    }
    

      

  • 相关阅读:
    函数探幽--引用变量
    函数探幽—内联函数
    我遇到的头文件
    继承的特点
    汇编语言中压栈与入栈
    cin.good(),cin.fail(),cin.clear()
    结构体的处理(以c++primer plus 第六章习题4为例)
    uva508
    uva253 CubePainting
    uva1590
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7091485.html
Copyright © 2011-2022 走看看