nowcoder really deceptive

A simple arithmetic calculation code, really that simple to engage in what is the point ah

My deceptive implementation, because strrchr, atoi no auto-complete, to achieve their own, the result of more than 120 lines of code, no commuting time again, leading to several bug, but time is tight, the code ugly, function design also has unreasonable :

This basically if else if I do not, and the results of it accounted for half of the bug, I used to if if if, not like this too much else, so that code looks a mouthful.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef enum 
{ 
    SYM_ADD = 0, 
    SYM_DEL, 
    SYM_MUL, 
    SYM_DIV, 
    SYM_INVALID 
} ENUM_Calc_SYM; 
char * findChr (INPUT const char *, char CHR) 
{ 
    const char * POS = INPUT; 
    IF (INPUT == NULL) 
        return NULL; 
    the while (* POS = '\ 0'!) 
    { 
        IF (POS * == CHR) 
		{ IF (CHR == '-') // function is a function contaminated, this should take out 
			{ 
				IF ((POS! = INPUT) && (* (-POS. 1)> = '0') && (* (-POS. 1) <= '. 9') ) 
					return (char *) POS;
			}
			else
				return (char *)pos;
			
			{
			}
		}
        pos++;
    }
    return NULL;
}

int str2int(const char* input, int *a)
{
    int validFlag = 0;
    int flag = 1;
    int sum = 0;
    const char *pos = input;
    if(NULL == input || NULL == a)
        return -1;
    while(*pos != '\0')
    {
        if(*pos == '-')
            flag = -1;
        else if(*pos >= '0' && *pos <= '9')
        {
            validFlag = 1;
            sum = sum*10 + *pos - '0';
        }else
        {
            break;
        }
		pos++;
    }
    if(validFlag == 0)
        return -1;
    sum = flag*sum;
    *a = sum;
    return 0;
}
void parseCmd(char * input, int *a, int* b, int* flag)
{
    char * pos = NULL;
    if(NULL == input)
        return;
    if(NULL == a || NULL == b || NULL == flag)
        return;
    *flag = SYM_INVALID;
    if((pos=findChr(input, '+')) != NULL)
    {
        *flag = SYM_ADD;
    }
    else if((pos=findChr(input, '-')) != NULL)
    {
        *flag = SYM_DEL;
    }
    else if((pos=findChr(input, '*')) != NULL)
    {
        *flag = SYM_MUL;
    }
    else if((pos=findChr(input, '/')) != NULL)
    {
        *flag = SYM_DIV;
    }
    if(NULL == pos)
       return;
       
    *pos = '\0';
    pos++;
    if(0 != str2int(input, a) || 0!= str2int(pos, b))
    {
       *flag = SYM_INVALID;
    }
    return;
}
void main()
{
    char input[100]={0};
    int a,b;
    int flag;
    while(scanf("%s", input) == 1)
    {
        flag = SYM_INVALID;
        parseCmd(input, &a, &b, &flag);
        memset(input, 0, sizeof(input));
        if(flag == SYM_INVALID)
            break;
        switch(flag)
        {
            case SYM_ADD:
                printf("%d\n", a+b);
                break;
            case SYM_DEL:
                printf("%d\n", a-b);
                break;
            case SYM_MUL:
                printf("%d\n", a*b);
                break;
            case SYM_DIV:
                if(b == 0)
                    break;
                printf("%f\n", (float)a/b);
                break;
            default:
                break;
        }
    }
}

  Just to fool the test, 20 lines of code are enough:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    char input[100]={0};
    int a,b;
    int flag;
    while(scanf("%s", input) == 1)
    {
		if(sscanf(input, "%d+%d", &a,&b)==2)
		{
			printf("%d\n",a+b);
		}
		if(sscanf(input, "%d-%d", &a,&b)==2)
		{
			printf("%d\n",a-b);
		}
		if(sscanf(input, "%d*%d", &a,&b)==2)
		{
			printf("%d\n",a*b);
		}
		if(sscanf(input, "%d/%d", &a,&b)==2)
		{
			printf("%d\n",a/b);
		}
    }
}

  

Guess you like

Origin www.cnblogs.com/green-crosswalk/p/11257263.html