在C语言中使用isdigit()有困难
我一直试图在使用Python的经验中增加一些C语言的经验,并从一个基本的添加程序开始。我要做的一件事是检查输入是数字还是字符,如图所示:在C语言中使用isdigit()有困难,c,C,我一直试图在使用Python的经验中增加一些C语言的经验,并从一个基本的添加程序开始。我要做的一件事是检查输入是数字还是字符,如图所示: #include <stdio.h> #include <ctype.h> int main() { int n, sum=0,c,value; printf("Enter the Number of Integers You Want to Add\n"); scanf("%d", &n);
#include <stdio.h>
#include <ctype.h>
int main()
{
int n, sum=0,c,value;
printf("Enter the Number of Integers You Want to Add\n");
scanf("%d", &n);
if(isdigit(n))
{
printf("Enter %d Integers\n", n);
for(c=1; c<=n; c++)
{
scanf("%d", &value);
if(isalpha(value))
{
printf("ENTER INTEGER NOT CHARACTER\n");
break;
}
else
{
sum = sum + value;
}
}
printf("Sum of Entered Integers = %d\n",sum);
}
else
{
printf("ENTER INTEGER NOT CHARACTER\n");
break;
}
return 0;
}
#包括
#包括
int main()
{
int n,和=0,c,值;
printf(“输入要添加的整数数\n”);
scanf(“%d”和“&n”);
if(isdigit(n))
{
printf(“输入%d个整数\n”,n);
对于(c=1;c当您使用scanf
读取一个整数时,您只会得到一个整数。(要读取单个字符,您需要%c
和一个指向字符的指针)
使用isdigit()
时,您需要提供该字符的表示形式(例如,在ASCII中,'0'
字符的表示形式为48,这实际上是它的整数值)
isdigit(0) is false
isdigit(48) is true (for ASCII, ISO8859, UTF-8)
isdigit('0') is true (no matter the character set)
isdigit('0' + n) is true for integers n = 0 ... 9
PS:不测试来自scanf
的返回值是自找麻烦…指向scanf
的%d
标记告诉它将输入解释为数字(更准确地说,它指示参数中的指针指向整数类型)。它只能将整数放入该参数中。如果它无法将输入解释为数字,scanf
将停止扫描输入并立即返回
isdigit()
将其参数作为字符代码进行计算,正如Jens在上文中指出的那样。但是,scanf
已经将字符代码转换为一个纯数字。它们既不起作用,也不起作用。这些库函数的目的是检查表示为int
的给定代码点是否在点的子集内由标准定义为数字字符或字母字符的
您应该检查scanf
调用的结果,而不是假设它们正常工作,并相应地对这些结果执行操作。如果您请求一个整数,并且其中一个已成功扫描,则它会告诉您。如果失败,您的操作过程可能是消耗该行的其余部分(通过换行或EOF)并可能重试:
#include <stdio.h>
int main()
{
int n,value,sum=0;
printf("Enter the Number of Integers You Want to Add\n");
if (scanf("%d", &n) == 1 && n > 0)
{
printf("Enter %d Integers\n", n);
while (n--)
{
if (scanf("%d", &value) == 1)
{
sum = sum + value;
}
else
{
// consume the rest of the line. if not EOF, we
// loop around and try again, otherwise break.
while ((value = fgetc(stdin)) != EOF && value != '\n');
if (value == EOF)
break;
++n;
}
}
printf("Sum of Entered Integers = %d\n", sum);
}
return 0;
}
#包括
int main()
{
int n,值,和=0;
printf(“输入要添加的整数数\n”);
如果(扫描频率(“%d”,&n)==1&&n>0)
{
printf(“输入%d个整数\n”,n);
而(n--)
{
如果(scanf(“%d”,&value)==1)
{
总和=总和+价值;
}
其他的
{
//消耗剩下的线。如果不是EOF,我们
//循环并重试,否则会中断。
而((value=fgetc(stdin))!=EOF&&value!='\n');
如果(值==EOF)
打破
++n;
}
}
printf(“输入的整数之和=%d\n”,总和);
}
返回0;
}
正确完成后,您应该能够输入超出单位数的有效整数(即值>10或<0),这是上面允许的。来自scanf手册页:
On success, the function returns the number of items of the argument list
successfully filled.
在您的程序中,您正试图从stdin中读取一项,因此scanf应返回1。因此,检查该值,您将知道一切正常:
printf("Enter the Number of Integers You Want to Add\n");
while(scanf("%d", &n) != 1) {
printf("That's not a valid integer. Please try again.\n");
}
您不能使用isdigit(),因为您已经在使用scanf将用户输入转换为整数。如果用户没有输入整数,scanf可能已经失败
查看您正在使用的所有C函数的手册页,它们将向您显示函数的期望值以及在不同情况下的返回值
在isdigit()的情况下,输入应为表示ASCII字符的无符号字符。这可能会有点混淆,因为ASCII字符实际上表示为一种整数类型,而字符串是这些字符的数组。与Python等语言不同,Python对您隐藏了所有这些内容。但数字字符串之间存在很大差异(包含数字位数字符的字符数组)和整数本身(处理器实际用于进行数学运算的形式…(简化的解释,但你明白了).您键入的输入是什么?我认为Python对缩进非常敏感…您是否阅读了手册页的Scanfn或者isdigit
或者isalpha
按照您的想法进行操作。确定给定字符的代码点是数字字符还是字母字符。我认为您应该检查分辨率scanf
和通过换行而不是使用那些ctype函数消耗垃圾的结果。数字也是一个字符。你是说字母?