, str[i]>='0' && str[i]<='9' , str[i] '.'
if(str[i]>='0' && str[i]<='9') {
if(str[i]=='.')
, , .
, -, , " , ". : "123e4" '.', "".
"", "", . 2 .
C C strto...() . .
#include <stdlib.h>
bool is_integer(const char *s) {
char *endptr;
long long ll = strtoll(s, &endptr, 0);
if (s == endptr) return false;
if (*endptr) return false;
return true;
}
bool is_real(const char *s) {
char *endptr;
double d = strtod(s, &endptr);
if (s == endptr) return false;
if (*endptr) return false;
return true;
}
void classify(const char *s) {
bool i = is_integer(s);
bool r = is_real(s);
if (i) {
if (r) puts("Both");
else puts("Integer");
} else {
if (r) puts("Real");
else puts("Neither");
}
}
When the string "Both" and the integer conversion does not overflow, the "integer" can be approved compared to "Real". However, if "Both" is encountered with an input like "1234567890123456789012345678901234567890", it might be better to use it as a "real" one, since usually an integer type does not exist.
source
share