Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C计数输入的数字,以逗号分隔_C_Count_Numbers_Comma - Fatal编程技术网

C计数输入的数字,以逗号分隔

C计数输入的数字,以逗号分隔,c,count,numbers,comma,C,Count,Numbers,Comma,我有一个代码来检查这个数字是奇数还是偶数。我使用char输入并用逗号分隔每个数字。一切都很好,但我需要计算输入了多少个数字,其中有多少是偶数。 因为逗号,我撞到了墙上。我试图搜索谷歌,但我的英语不是很好,我找不到这样的功能。也许我应该循环数字输入,直到用户只需按enter键开始检查偶数和奇数。到目前为止,我的代码是: char str[256]; fgets (str, 256, stdin); char *pt; pt = strtok (str,","); whil

我有一个代码来检查这个数字是奇数还是偶数。我使用char输入并用逗号分隔每个数字。一切都很好,但我需要计算输入了多少个数字,其中有多少是偶数。 因为逗号,我撞到了墙上。我试图搜索谷歌,但我的英语不是很好,我找不到这样的功能。也许我应该循环数字输入,直到用户只需按enter键开始检查偶数和奇数。到目前为止,我的代码是:

char str[256];
 fgets (str, 256, stdin);
    char *pt;
    pt = strtok (str,",");
    while (pt != NULL) {
        int a = atoi(pt);

        if (a%2 == 0)
        {
            printf("Number is even\n");

        }
        else
        {
            printf("Number is odd!\n\n");
        }
        printf("%d\n", a);
        pt = strtok (NULL, ",");
    }

若我们使用变量+,这意味着变量的值增加1

char str[256];
fgets (str, 256, stdin);
char *pt;
int odd_count = 0,even_count = 0;
pt = strtok (str,",");
while (pt != NULL) {
    int a = atoi(pt);

    if (a%2 == 0)
    {
        printf("Number is even\n");
        even_count++;
    }
    else
    {
        printf("Number is odd!\n\n");
        odd_count++;
    }
    printf("%d\n", a);
    pt = strtok (NULL, ",");
}
printf("Count of even numbers in the sequence is %d",even_count);
printf("Count of odd numbers in the sequence is %d",odd_count);
printf("Total numbers in the sequence is are %d",even_count + odd_count);

若我们使用变量+,这意味着变量的值增加1

char str[256];
fgets (str, 256, stdin);
char *pt;
int odd_count = 0,even_count = 0;
pt = strtok (str,",");
while (pt != NULL) {
    int a = atoi(pt);

    if (a%2 == 0)
    {
        printf("Number is even\n");
        even_count++;
    }
    else
    {
        printf("Number is odd!\n\n");
        odd_count++;
    }
    printf("%d\n", a);
    pt = strtok (NULL, ",");
}
printf("Count of even numbers in the sequence is %d",even_count);
printf("Count of odd numbers in the sequence is %d",odd_count);
printf("Total numbers in the sequence is are %d",even_count + odd_count);

如注释中所述,当您读入每个数字时,计算读入的值的总数。然后,当检查偶数时,增加另一个计数器:

int countTotal = 0, countEven = 0;
while (pt != NULL) {
    int a = atoi(pt);

    countTotal++;
    if (a%2 == 0)
    {
        printf("Number is even\n");
        countEven++;
    }
    else
    {
        printf("Number is odd!\n\n");
    }
    printf("%d\n", a);
    pt = strtok (NULL, ",");
}

如注释中所述,当您读入每个数字时,计算读入的值的总数。然后,当检查偶数时,增加另一个计数器:

int countTotal = 0, countEven = 0;
while (pt != NULL) {
    int a = atoi(pt);

    countTotal++;
    if (a%2 == 0)
    {
        printf("Number is even\n");
        countEven++;
    }
    else
    {
        printf("Number is odd!\n\n");
    }
    printf("%d\n", a);
    pt = strtok (NULL, ",");
}

您已经检测到偶数/奇数。为什么不能让计数器
total\u count
num\u even\u count
并在适当的位置增加它们?不清楚你的困难是什么,我不明白。你仍然有一个数字循环。为什么不在while循环中使用计数器来知道值的数目呢?只是++它在“偶数”和“奇数”中允许轻松计算赔率和偶数(总数应为偶数+奇数)。@kaylum我正在学习C,不知道num\u偶数。我正在使用我迄今所学的知识。我试试看。我对如何使用逗号感到非常困惑。在调用
strtok(NULL,“,”)之后是否应该增加
pt
?TDWTF?没有神奇的num\u偶数计数,@kaylum只是编了一个似乎适合计数的变量名……你已经在检测偶数/奇数了。为什么不能让计数器
total\u count
num\u even\u count
并在适当的位置增加它们?不清楚你的困难是什么,我不明白。你仍然有一个数字循环。为什么不在while循环中使用计数器来知道值的数目呢?只是++它在“偶数”和“奇数”中允许轻松计算赔率和偶数(总数应为偶数+奇数)。@kaylum我正在学习C,不知道num\u偶数。我正在使用我迄今所学的知识。我试试看。我对如何使用逗号感到非常困惑。在调用
strtok(NULL,“,”)之后是否应该增加
pt
?TDWTF?没有神奇的num_甚至计数,@kaylum只是编了一个似乎适合计数的变量名。。。