热门搜索 :
考研考公

isdigit(int ch) 具体用法

发布网友 发布时间:2022-04-19 23:38

我来回答

5个回答

热心网友 时间:2023-09-09 03:16

isdigit()函数用于判断输入的字符是不是一个数字,它接受的是一个字符的ASCII码,也就是说,它的作用是判断传入的ASCII码是不是大于等于48且小于等于57。

ctype.h中声明的这些类似的函数都是针对字符型变量基于其对应的ASCII值进行判断的,并不能对输入的值的类型作出判断。

热心网友 时间:2023-09-09 03:16

isdigit(int ch)是判断输入的字符是不是数字的,好像跟你说的这个输入的个数没啥关系吧?
给个用isdigit()的例子
if(!isdigit(ch))
printf("%c is not a number!",ch);
else printf("%c is a number!",ch);

热心网友 时间:2023-09-09 03:17

具体做法如下:
int i;

char s1[100], s2[100];
scanf("%s", s1);
scanf("%s", s2);

for(i=0;i<strlen(s1), i++)
{
if(!isdigt(s1[i]))
{
printf("error!\n");
inputfun();
break;
}
}
for(i=0;i<strlen(s2), i++)
{
if(!isdigt(s2[i]))
{
printf("error!\n");
inputfun();
break;
}
}

热心网友 时间:2023-09-09 03:17

int i;
char s1[100], s2[100];
scanf("%s", s1);
scanf("%s", s2);

for(i=0;i<strlen(s1), i++)
{
if(!isdigt(s1[i]))
{
printf("error!\n");
inputfun();
break;
}
}
for(i=0;i<strlen(s2), i++)
{
if(!isdigt(s2[i]))
{
printf("error!\n");
inputfun();
break;
}
}

热心网友 时间:2023-09-09 03:18

你要C++的还是C的?

#include <iostream>
#include <cctype>

using namespace std;

int main(int argc, char* argv[])
{
char ch;
int n, m;
while (true) {
cout << "two integers please" << endl;
if (!isdigit(ch = cin.get()))
{
while (cin.get() != '\n');
continue;
}
cin.putback(ch);
cin >> n;
while (cin.peek() == ' ' || cin.peek() == '\t') cin.get();
if (!isdigit(ch = cin.peek()))
{
while (cin.get() != '\n');
continue;
}
cin >> m;
break;
}
cout << n << ' ' << m << endl;
return 0;
}
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
Top