寒,你按字符串读入,然后-个‘0’就知道了么
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#define NUM "0123456789."
bool IsNumber( const char *number ) {
bool isnumber = true;
int len = strlen( number );
if(len == 0 || len > 9) {
return !isnumber;
}
if (number[0] == '-' || number[0] == '+') {
for(int i=0; i<len -1; ++i ) {
if(strchr(NUM, number[i + 1]) == NULL)
return !isnumber;
}
}else{
for(int i=0; i<len; ++i ) {
if(strchr(NUM, number[i]) == NULL)
return !isnumber;
}
}
return isnumber;
}
void main()
{
char number[20] = {0};
cout << "Enter a number: ";
cin >> number;
while (!IsNumber(number)) {
cout << "not number" << endl;
cout << "Enter a number: ";
cin >> number;
}
cout << "OK!" << endl;
}
[此贴子已经被作者于2006-3-31 14:26:19编辑过]