Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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_Arrays - Fatal编程技术网

C 将由逗号分隔的数字组成的用户输入放入数组?

C 将由逗号分隔的数字组成的用户输入放入数组?,c,arrays,C,Arrays,我从用户那里获取了10个数字(用户在提示下输入,数字之间用逗号分隔,如:245645,-2434245)。如何将这些元素放入数组中?如下图所示,我使用了scanf,但它并不像我希望的那样工作。如有任何建议,将不胜感激 //User will pass ten numbers, separated by commas. This is to be put into an array. #include <stdio.h> int main () { int A[10];

我从用户那里获取了10个数字(用户在提示下输入,数字之间用逗号分隔,如:245645,-2434245)。如何将这些元素放入数组中?如下图所示,我使用了
scanf
,但它并不像我希望的那样工作。如有任何建议,将不胜感激

//User will pass ten numbers, separated by commas. This is to be put into an array.

#include <stdio.h>

int main () 
{
    int A[10]; // array to contain in users input.
    printf("Enter your numbers: ");
    scanf("%d", &A[10]);

    return 0;
}
//用户将传递十个数字,用逗号分隔。这将被放入一个数组中。
#包括
int main()
{
int A[10];//要包含在用户输入中的数组。
printf(“输入您的号码:”);
scanf(“%d”、&A[10]);
返回0;
}

您还必须在
scanf
中使用逗号:

for(int i=0; i<10; i++) {        /* for each element in A */
  if(0==scanf("%d,", &A[i])) {    /* read a number from stdin into A[i] and then consume a commma (if any) */
    break;                       /* if no digit was read, user entered less than 10 numbers separated by commas */
    /* alternatively error handling if fewer than 10 is illegal */
  }
}

for(inti=0;i我不会为你写全部内容。
但我绝对能帮上忙。
其中一种方法是:

  • 获取一个包含10个逗号分隔数字的字符串:
    fgets()
    可能是
  • 验证字符串,修剪空白,使生活更轻松
  • 从字符串中选择一个数字:
    strtol()
    可能是什么
  • 搜索字符串中的
    ,“
    字符,并将指针设置为
    ”之后的下一个索引,“
    strchr()
    可能是吗
  • 重复第3步和第4步共10次(从这里开始,实际上是9次)
  • 打印数字
  • 下面的代码可以完成一半的工作,剩下的部分就是从用户那里获取字符串并进行验证。 预先声明和初始化字符串的目的是更加强调对初学者来说复杂的数据的实际解析(没有冒犯)

    在我们看下面的代码之前,让我们先看一些东西

    • 您可能需要查看该函数的手册页
    • 您可能希望查看函数的手册页,该手册页未在下面的代码中使用,但您可能最终使用它来实现步骤1
    我已经承认,这可能不是实现它的最佳方式,我很高兴地同意,通过添加各种错误检查,下面的代码可以在许多方面做得更好,但我将这留给您去探索和实现

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(void) 
    {
        int i = 0, j = 0;
        char *Str = "1,11,21,1211,111221,312211,1234,4321,123,789";
        char *ptr;
        long ret;
        long array[10] = { [0 ... 9] = 0 };
    
        // So, lets assume step 1 and 2 are taken care of.
    
        // Iterate though the data for 10 times
        for(i = 0; i < 10; i++)
        {
            ret = strtol(Str, &ptr, 10);
            // Check if its a number
            if(ret != 0)
            {
                // Its is a number!
                // Put it in the array
                array[j] = ret;
                // Increment the index so that next number will not over-write the existing one
                j++;
                // Find the next ',' in the string  
                Str = strchr(Str, ',');
                if(Str == NULL)
                {
                    // Handle this condition that there are no more commas in the string! 
                    break;
                }
                // Assuming that the ',' is found, increment the pointer to index next to ','
                Str++;
            }
        }
    
        // Print the array
        for(i = 0; i < j; i++)
            printf("%ld\n", array[i]);
    }
    
    希望我已经让你开始,祝你好运

    1
    11
    21
    1211
    111221
    312211
    1234
    4321
    123
    789