C 精确多重扫描输入
我想知道基本上如何使用while、if和array等简单命令在一行中精确输入5个数字(特别是整数)。例如: 如果我输入5个由空格分隔的数字, 1 2 3 4 5 程序将打印 1 2 3 4 5 但是,如果我输入小于5或大于5, 1 2 3 4 程序将打印 无效输入。 到目前为止我有C 精确多重扫描输入,c,input,scanf,C,Input,Scanf,我想知道基本上如何使用while、if和array等简单命令在一行中精确输入5个数字(特别是整数)。例如: 如果我输入5个由空格分隔的数字, 1 2 3 4 5 程序将打印 1 2 3 4 5 但是,如果我输入小于5或大于5, 1 2 3 4 程序将打印 无效输入。 到目前为止我有 #include<stdio.h> int main(int argc,char *argv[]){ int array[5], numbers; numbers = 0; w
#include<stdio.h>
int main(int argc,char *argv[]){
int array[5], numbers;
numbers = 0;
while (numbers < 5) {
scanf("%d", &array[numbers]);
numbers = numbers + 1
}
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
return 0;
}
#包括
int main(int argc,char*argv[]){
整数数组[5],数字;
数字=0;
而(数字<5){
scanf(“%d”和数组[number]);
数字=数字+1
}
printf(“%d%d%d%d%d%d\n”,数组[0],数组[1],数组[2],数组[3],数组[4]);
返回0;
}
如果我们用9999(程序未使用的数字)分配所有数组单元格,会怎么样。我们做一个循环来检查每个数组是否已经被更改为一个新的值,如果它仍然是9999,那么它将是无效的。但问题仍然在于,我们如何抓住一行不同数量的输入并继续前进。例如输入2 3
输出2 3 9999 9999
或输入2 3 4
输出2 3 4 9999 9999编辑
要输入5个数字,您可以使用
int a[5];
char x;
scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4]);
while(scanf("%*[^\n]%*c")) {
scanf("%c", &x); // after 5 ints were loaded, get rid of rest of the elements untill new line symbol
}
printf("%d %d %d %d %d\n\n", a[0], a[1], a[2], a[3], a[4]);
它将忽略5个数字之后的所有内容(事实上,它将写入x
,直到出现新行符号),但在这种情况下,无法轻松设置要读取的数字数量
您只需注意,如果行中的int小于5,则它将不起作用。EDIT
要输入5个数字,您可以使用
int a[5];
char x;
scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4]);
while(scanf("%*[^\n]%*c")) {
scanf("%c", &x); // after 5 ints were loaded, get rid of rest of the elements untill new line symbol
}
printf("%d %d %d %d %d\n\n", a[0], a[1], a[2], a[3], a[4]);
它将忽略5个数字之后的所有内容(事实上,它将写入x
,直到出现新行符号),但在这种情况下,无法轻松设置要读取的数字数量
您只需注意,如果行中的int
s小于5,则该命令将不起作用。如果要强制输入在一行上,请先读取输入,然后解析它:
char line[100];
fgets(line, 100, stdin);
char x[100];
int n = sscanf(line, "%d %d %d %d %d %s", array, array+1, array+2, array+3, array+4, x)
if (n != 5)
printf("invalid input\n");
else
printf("read 5 numbers\n");
添加x
以检测是否读取了过多的内容。如果要强制输入在一行上,请先读取输入,然后解析它:
char line[100];
fgets(line, 100, stdin);
char x[100];
int n = sscanf(line, "%d %d %d %d %d %s", array, array+1, array+2, array+3, array+4, x)
if (n != 5)
printf("invalid input\n");
else
printf("read 5 numbers\n");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int intRead(int array[], int size){
char buff[128];
int status=!0;//status == 0, Something happened!
printf("\ninput %d integer:", size);
while(fgets(buff, sizeof(buff), stdin)){
int count=0;
char*p;
for(p=buff;NULL!=(p=strtok(p, " \t\n"));p=NULL){
char *ck;
int i;
i=(int)strtol(p, &ck, 0);
if(*ck){
fprintf(stderr, "invalid input:can't parse of int <<%s>>\n", p);
status=0;
continue;
}
if(count < size){
array[count++]=i;
continue;
}
count = size + 1;//more than
break;
}
if(count < size)
fprintf(stderr, "invalid input: less than %d\n", size);
else if(count == size) return status;
if(count > size)
fprintf(stderr, "invalid input: more than %d\n", size);
printf("\ninput %d integer:", size);
status = !0;
}
return 0;
}
int main(int argc,char *argv[]){
int array[5];
intRead(array, 5);//or: while(!intRead(array, 5));
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
return 0;
}
添加x
以检测是否读取了过多的内容。\include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int intRead(int array[], int size){
char buff[128];
int status=!0;//status == 0, Something happened!
printf("\ninput %d integer:", size);
while(fgets(buff, sizeof(buff), stdin)){
int count=0;
char*p;
for(p=buff;NULL!=(p=strtok(p, " \t\n"));p=NULL){
char *ck;
int i;
i=(int)strtol(p, &ck, 0);
if(*ck){
fprintf(stderr, "invalid input:can't parse of int <<%s>>\n", p);
status=0;
continue;
}
if(count < size){
array[count++]=i;
continue;
}
count = size + 1;//more than
break;
}
if(count < size)
fprintf(stderr, "invalid input: less than %d\n", size);
else if(count == size) return status;
if(count > size)
fprintf(stderr, "invalid input: more than %d\n", size);
printf("\ninput %d integer:", size);
status = !0;
}
return 0;
}
int main(int argc,char *argv[]){
int array[5];
intRead(array, 5);//or: while(!intRead(array, 5));
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
return 0;
}
#包括
#包括
intRead(int数组[],int大小){
字符buff[128];
int status=!0;//status==0,发生了一些事情!
printf(“\n输入%d个整数:”,大小);
while(fgets(buff,sizeof(buff),stdin)){
整数计数=0;
char*p;
对于(p=buff;NULL!=(p=strtok(p,“\t\n”);p=NULL){
char*ck;
int i;
i=(int)strtol(p和ck,0);
如果(*ck){
fprintf(stderr,“无效输入:无法解析int\n”,p);
状态=0;
继续;
}
如果(计数<大小){
数组[count++]=i;
继续;
}
计数=大小+1;//大于
打破
}
如果(计数<大小)
fprintf(stderr,“无效输入:小于%d\n”,大小);
else if(count==size)返回状态;
如果(计数>大小)
fprintf(stderr,“无效输入:超过%d\n”,大小);
printf(“\n输入%d个整数:”,大小);
状态=!0;
}
返回0;
}
int main(int argc,char*argv[]){
int数组[5];
intRead(数组,5);//或:while(!intRead(数组,5));
printf(“%d%d%d%d%d%d\n”,数组[0],数组[1],数组[2],数组[3],数组[4]);
返回0;
}
#包括
#包括
#包括
intRead(int数组[],int大小){
字符buff[128];
int status=!0;//status==0,发生了一些事情!
printf(“\n输入%d个整数:”,大小);
while(fgets(buff,sizeof(buff),stdin)){
整数计数=0;
char*p;
对于(p=buff;NULL!=(p=strtok(p,“\t\n”);p=NULL){
char*ck;
int i;
i=(int)strtol(p和ck,0);
如果(*ck){
fprintf(stderr,“无效输入:无法解析int\n”,p);
状态=0;
继续;
}
如果(计数<大小){
数组[count++]=i;
继续;
}
计数=大小+1;//大于
打破
}
如果(计数<大小)
fprintf(stderr,“无效输入:小于%d\n”,大小);
else if(count==size)返回状态;
如果(计数>大小)
fprintf(stderr,“无效输入:超过%d\n”,大小);
printf(“\n输入%d个整数:”,大小);
状态=!0;
}
返回0;
}
int main(int argc,char*argv[]){
int数组[5];
intRead(数组,5);//或:while(!intRead(数组,5));
printf(“%d%d%d%d%d%d\n”,数组[0],数组[1],数组[2],数组[3],数组[4]);
返回0;
}
关闭;问题是,x
可能会失败,因为该行是12345x
…这真的很棘手。可能是%n
而不是%d
,然后您可以检查报告的偏移量后是否只有空格?@JonathanLeffler很好。我将其更改为%s
:忽略尾随空格,因为模式中的单个空格可能与字符串中的多个空格相匹配。对于scanf()
或fscanf()
,这将是有问题的(因为在第五个数字之后需要非空字符)。有了sscanf()
,就没关系了;字符串的结尾是'EOF',它必须停在那里。这对于OP和您一样重要。sscanf(行,“%d%d%d%d%d%c”、…、&x)
也可以工作,然后x
可以是char x
。小调:100快到了。由于64位整数更常见,机器可以使用该大小的int
,只需要100个字节;问题是,x
可能会失败,因为该行是12345x
…这真的很棘手。可能是%n
而不是%d
,然后您可以检查报告的偏移量后是否只有空格?@JonathanLeffler很好。我将其更改为%s
:尾随空格ar