C语言 C、从单个输入行读取多个数字(scanf?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2539796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C, reading multiple numbers from single input line (scanf?)
提问by migajek
I have written an app in C which expects two lines at input. First input tells how big an array of int will be and the second input contains values separated by space. For example, the following input
我用 C 编写了一个应用程序,它需要输入两行。第一个输入告诉 int 数组有多大,第二个输入包含用空格分隔的值。例如,以下输入
5
1 2 3 4 99
should create an array containing {1,2,3,4,99}
应该创建一个包含 {1,2,3,4,99}
What is the fastest way to do so? My problem is to read multiple numbers without looping through the whole string checking if it's space or a number?
这样做的最快方法是什么?我的问题是读取多个数字而不遍历整个字符串检查它是空格还是数字?
Thanks.
谢谢。
回答by Denilson Sá Maia
int i, size;
int *v;
scanf("%d", &size);
v = malloc(size * sizeof(int));
for(i=0; i < size; i++)
scanf("%d", &v[i]);
Remember to free(v)after you are done!
free(v)完成后记得来哦!
Also, if for some reason you already have the numbers in a string, you can use sscanf()
此外,如果由于某种原因您已经在字符串中有数字,您可以使用 sscanf()
回答by Lunfel
Here is an example taken from http://www.cplusplus.com/reference/cstring/strtok/that I've adapted to our context.
这是一个来自http://www.cplusplus.com/reference/cstring/strtok/的示例,我已经根据我们的上下文进行了调整。
It splits the str chain in sub chains and then I convert each part into an int. I expect that the entry line is numbers seperated by commas, nothing else. Size is the size of your array. You should do scanf("%d", &size); as Denilson stated in his answer. At the end, you have your int array with all values.
它在子链中拆分 str 链,然后我将每个部分转换为 int。我希望输入行是用逗号分隔的数字,没有别的。大小是数组的大小。你应该做 scanf("%d", &size); 正如德尼尔森在他的回答中所说。最后,您将拥有包含所有值的 int 数组。
int main(){
int size = 5, i = 0;
char str[] ="10,20,43,1,576";
int list[size];
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str,",");
list[i] = atoi(pch);
i++;
while (pch != NULL)
{
pch = strtok (NULL, ",");
if(pch != NULL)
list[i] = atoi(pch);
i++;
}
for(i=0;i<size;i++){
printf("%d. %d\n",i+1,list[i]);
}
return 0;
}
回答by M.A.K. Ripon
Here 'N' is the number of array elements of Array 'A'
这里'N'是数组'A'的数组元素数
int N, A[N];
printf("Input no of element in array A: ");
scanf("%d", &N);
printf( "You entered: %d\n", N);
printf("Input array A elements in one line: ");
for(int i=0; i<N; i++){
fscanf(stdin, "%d", &A[i]);
printf( "A[%d] is: %d\n", i, A[i]);
}
回答by Carl Norum
scanf()is kind of a pain in the neck. Check out strtol()for this kind of problem, it will make your life very easy.
scanf()有点脖子疼。看看strtol()这种问题,它会让你的生活很轻松。
回答by wahid_abdul
This code employs a straight forward approach of reading each character through getchar().We go onto reading a number util we find a blank space.The index 'i' of the array gets updated after that.This is repeated until newline('\n') is encountered
这段代码采用了一种直接的方法,通过 getchar() 读取每个字符。我们继续读取一个数字,找到一个空格。之后数组的索引 'i' 被更新。重复这个过程,直到换行('\ n') 遇到
#include<iostream>
main()
{
char ch;
int arr[30] ;
ch =getchar();
int num = 0;
int i=0;
while(ch !='\n')
{
if(ch == ' ')
{
arr[i] = num;
i++;
num = 0;
}
if(((ch -48) >=0) && (ch-48 <=9))
num = (num*10) + (ch - 48);
ch = getchar();
}
arr[i] = num;
for(int j=0;i<=i;j++)
std::cout<<arr[i]<<" ";
}

