如何扫描值并忽略字符
How to scan in values and ignore the characters
我是 C 的新手,我在查看一些问题时思考了一个问题,我们需要使用用户输入扫描值。例子
1 2 3 45 6 7. 所以我们自动将这些值扫描到二维数组中。
困扰我的一件事是如果用户输入
1 2 3 2 3 Josh,我们怎么能忽略 Josh 而只将值扫描到数组中。
我查看了使用 getchar 并使用了一个标志变量,但我无法弄清楚区分整数和字符的难题。
/* 这是我试过的 */
#include <stdio.h>
int main(int argc, char *argv[]) {
int a;
int b;
int A[10];
while (((a = getchar()) != '\n') && (b = 0)) {
if (!(a >= "A" && a <= "Z")) {
scanf("%d", A[b]);
}
b++;
}
}
}
您的代码完全错误。我建议删除它。
您可以使用 scanf
和 %d
来读入数字。如果它 returns 0,则有一些无效输入。因此,扫描并丢弃 %s
并重复此过程:
int num = -1;
while(num != 0)
{
printf("Enter a number, enter 0 to exit:");
if(scanf("%d", &num) == 0) /* If scanf failed */
{
printf("Invalid input found!");
scanf("%*s"); /* Get rid of the invalid input (a word) */
}
}
我认为实现您想要的效果的一个好方法是使用 scanf 格式 "%s"
,它会将所有内容读取为字符串,有效地 拆分 根据空格输入。来自手册:
s
Matches a sequence of non-white-space characters; the next
pointer must be a pointer to character array that is long
enough to hold the input sequence and the terminating null
byte ('[=26=]'), which is added automatically. The input string
stops at white space or at the maximum field width, whichever
occurs first.
要将字符串转换为整数,可以使用atoi。来自手册:
The atoi() function converts the initial portion of the string
pointed to by nptr to int.
因此,如果它将字符串的初始部分转换为整数,我们就可以使用它来识别什么是数字,什么不是。
您可以为 atoi.
构建一个简单的 "word detector"
使用 ctype.h
中的函数 isalpha 你可以:
int isword(char *buffer)
{
return isalpha(*buffer);
}
并重写您的阅读程序:
#include <stdio.h>
#include <ctype.h>
int isword(char *buffer)
{
return isalpha(*buffer);
}
int main(void)
{
char input[200];
int num;
while (1) {
scanf("%s", input);
if (!strcmp(input, "exit")) break;
if (isword(input)) continue;
num = atoi(input);
printf("Got number: %d\n", num);
}
return 0;
}
你应该记住 isword
这个名字是错误的。该函数不会检测 buffer
实际上是否是一个单词。它只测试 第一个字符 ,如果这是一个字符,则 return 为真。原因是我们的基本函数 itoa 的工作方式。如果缓冲区的第一个字符不是数字,它将 return 为零——这不是你想要的。所以,如果你有其他需求,可以把这个函数当成base.
这也是我写一个单独的函数而不是的原因:
if (!isalpha(input[0]))
num = itoa(input);
else
continue;
输出(你的输入):
$ ./draft
1 2 3 2 3 Josh
Got number: 1
Got number: 2
Got number: 3
Got number: 2
Got number: 3
exit
$
关于赋值和&&
while (((a = getchar()) != '\n') && (b = 0))
正如我在评论中所说,这个循环永远不会工作,因为你正在做一个 logical conjunction(AND) 并且赋值总是 return zero
。这意味着循环条件将始终评估为 false.
在 C 中,assignments return 赋值。所以,如果你这样做
int a = (b = 10);
a
现在将保持值 10
。同样的,当你做
something && (b = 0)
你做得很好
something && 0
总是 计算结果为假(如果你还记得 AND 真理 table):
p q p && q
---------------
0 0 0
0 1 0
1 0 0
1 1 1
我是 C 的新手,我在查看一些问题时思考了一个问题,我们需要使用用户输入扫描值。例子 1 2 3 45 6 7. 所以我们自动将这些值扫描到二维数组中。 困扰我的一件事是如果用户输入 1 2 3 2 3 Josh,我们怎么能忽略 Josh 而只将值扫描到数组中。 我查看了使用 getchar 并使用了一个标志变量,但我无法弄清楚区分整数和字符的难题。 /* 这是我试过的 */
#include <stdio.h>
int main(int argc, char *argv[]) {
int a;
int b;
int A[10];
while (((a = getchar()) != '\n') && (b = 0)) {
if (!(a >= "A" && a <= "Z")) {
scanf("%d", A[b]);
}
b++;
}
}
}
您的代码完全错误。我建议删除它。
您可以使用 scanf
和 %d
来读入数字。如果它 returns 0,则有一些无效输入。因此,扫描并丢弃 %s
并重复此过程:
int num = -1;
while(num != 0)
{
printf("Enter a number, enter 0 to exit:");
if(scanf("%d", &num) == 0) /* If scanf failed */
{
printf("Invalid input found!");
scanf("%*s"); /* Get rid of the invalid input (a word) */
}
}
我认为实现您想要的效果的一个好方法是使用 scanf 格式 "%s"
,它会将所有内容读取为字符串,有效地 拆分 根据空格输入。来自手册:
s
Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('[=26=]'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
要将字符串转换为整数,可以使用atoi。来自手册:
The atoi() function converts the initial portion of the string pointed to by nptr to int.
因此,如果它将字符串的初始部分转换为整数,我们就可以使用它来识别什么是数字,什么不是。
您可以为 atoi.
构建一个简单的 "word detector"使用 ctype.h
中的函数 isalpha 你可以:
int isword(char *buffer)
{
return isalpha(*buffer);
}
并重写您的阅读程序:
#include <stdio.h>
#include <ctype.h>
int isword(char *buffer)
{
return isalpha(*buffer);
}
int main(void)
{
char input[200];
int num;
while (1) {
scanf("%s", input);
if (!strcmp(input, "exit")) break;
if (isword(input)) continue;
num = atoi(input);
printf("Got number: %d\n", num);
}
return 0;
}
你应该记住 isword
这个名字是错误的。该函数不会检测 buffer
实际上是否是一个单词。它只测试 第一个字符 ,如果这是一个字符,则 return 为真。原因是我们的基本函数 itoa 的工作方式。如果缓冲区的第一个字符不是数字,它将 return 为零——这不是你想要的。所以,如果你有其他需求,可以把这个函数当成base.
这也是我写一个单独的函数而不是的原因:
if (!isalpha(input[0]))
num = itoa(input);
else
continue;
输出(你的输入):
$ ./draft
1 2 3 2 3 Josh
Got number: 1
Got number: 2
Got number: 3
Got number: 2
Got number: 3
exit
$
关于赋值和&&
while (((a = getchar()) != '\n') && (b = 0))
正如我在评论中所说,这个循环永远不会工作,因为你正在做一个 logical conjunction(AND) 并且赋值总是 return zero
。这意味着循环条件将始终评估为 false.
在 C 中,assignments return 赋值。所以,如果你这样做
int a = (b = 10);
a
现在将保持值 10
。同样的,当你做
something && (b = 0)
你做得很好
something && 0
总是 计算结果为假(如果你还记得 AND 真理 table):
p q p && q
---------------
0 0 0
0 1 0
1 0 0
1 1 1