如何判断一个字符是否数字?
如题。谢谢。
[解决办法]
ASC码,用getchar()接受判断是否在0x30~0x39之间
[解决办法]
#include <stdio.h>
int main()
{
int i;
if ( scanf( "%d ", &i) == 1)
printf( "input correctly\n ");
else
printf( "input error\n ");
return 0;
}
[解决办法]
对C++不熟..
个人认为这个跟上面if ( scanf( "%d ", &i) == 1)这个道理是一样的,当输入的不是数字时,cin这个返回为0,所以if(!cin)能起到判断是否数字的作用...
[解决办法]
C++只所以提供输入流cin来替代scanf,就是他内置了类型检查
同意楼上的,就是和scanf一样
[解决办法]
函数名称: isdigit
函数原型: int isdigit(int ch);
函数功能: 检查ch是否是数字(0-9)
函数返回: 是返回1,否则返回0
参数说明:
所属文件: <ctype.h>
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch1= '1 ';
char ch2= 'a ';
if(isdigit(ch1)!=0)
printf( "%c is the ASCII number\n ",ch1);
else
printf( "%c is not the ASCII number\n ",ch1);
if(isdigit(ch2)!=0)
printf( "%c is the ASCII number\n ",ch2);
else
printf( "%c is not the ASCII number\n ",ch2);
return 0;
}
[解决办法]
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
string line, prum( "0123456789 ");//prum是数字字符集,如果是小数,包括小数点符号即可
cout < < "Input a number, nothing for break: ";
getline(cin, line); //读取所有输入内容
while(line != " ") //读入空串时候退出【直接回车】
{
if(line.find_first_of(prum) != string::npos) //搜索非数字字符
cout < <line < < " is not a number. " < <endl; //找到了说明不是数字
else cout < < "It 's a number: " < <line < <endl; //没有非数字字符,说明输入是数值
cout < <endl; //输入下一组
cout < < "Input a number, nothing for break: ";
getline(cin, line);
}
system( "pause ");
return 0;
}
[解决办法]
char c = getchar();
printf( "c is a digtal = %d\n ",( c> = '0 '&&c <= '9 ' )?1:0);