nowcoder真坑人

Posted green-crosswalk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nowcoder真坑人相关的知识,希望对你有一定的参考价值。

一个简单的加减乘除运算代码,真这么简单搞有什么意义啊

我的坑人实现方法,因为strrchr,atoi没有自动补全,就自己实现,结果代码超过120行,没时间走读一遍,导致好几个bug,而且时间紧,代码丑,函数设计也有不合理的地方:

这种if else if 我基本上都不用了,结果它的bug占一半,我还是习惯if if if,不喜欢这种太多else,让代码看起来拗口。

#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(const char* input, char chr)

    const char * pos = input;
    if(NULL == input)
        return NULL;
    while(*pos != ‘\0‘)
    
        if(*pos == chr)
		
			if(chr == ‘-‘)//一个函数的功能被污染了,这个应该拿出去
			
				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;
        
    

  只为了骗骗测试用例,20行代码都够了:

#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);
		
    

  

以上是关于nowcoder真坑人的主要内容,如果未能解决你的问题,请参考以下文章

nowcoder赛前集训营5

NowCoder数列

[nowcoder]最长区间

Nowcoder84D

Nowcoder 提高组练习赛-R1

[nowcoder]因数个数和