0
I’m a beginner in programming.
I am performing an exercise in which I must determine whether a character string is a number, considering that the user can write anything as input. So I created the function below. I can only use the default library. I’m trying to use as few variables as possible.
Someone could evaluate?
int isNumber(char input[], int input_size){
int i;
//Elimina cadeia com caracteres não numéricos
for(i = 1; i < input_size; i++)
{
if(input[i-1] != '0' && input[i-1] != '1' &&
input[i-1] != '2' && input[i-1] != '3' &&
input[i-1] != '4' && input[i-1] != '5' &&
input[i-1] != '6' && input[i-1] != '7' &&
input[i-1] != '8' && input[i-1] != '9' &&
input[i-1] != '.' && input[i-1] != ',' &&
input[i-1] != ' ' && input[i-1] != '-' &&
input[i-1] != '\0'){
return 0;
}
}
//Elimina cadeia que só possui espaços e caracteres nulos
for(i = 1; i < input_size; i++){
if(input[i-1] != ' ' && input[i-1] != '\0'){
break;
}
}
if(i >= input_size){
return 0;
}
//Elimina cadeia com mais de um ponto ou vírgula
for(i = 1; i < input_size; i++){
if(input[i-1] == '.' || input[i-1] == ','){
for(i++; i < input_size; i++){
if(input[i-1] == '.' || input[i-1] == ','){
return 0;
}
}
break;
}
}
//Elimina cadeia com mais de um sinal de menos
for(i = 1; i < input_size; i++){
if(input[i-1] == '-'){
for(i++; i < input_size; i++){
if(input[i-1] == '-'){
return 0;
}
}
break;
}
}
//Elimina cadeia com dois números separados por espaço
for(i = 2; i < input_size; i++){
if(input[i-2] != ' ' &&
input[i-2] != '\0' &&
input[i-1] == ' ' &&
input[i] != ' ' &&
input[i] != '\0'){
return 0;
}
}
//Elimina cadeia com dois números separados por sinal de menos
for(i = 2; i < input_size; i++){
if(input[i-2] != ' ' &&
input[i-2] != '\0' &&
input[i-1] == '-' &&
input[i] != ' ' &&
input[i] != '\0'){
return 0;
}
}
//Elimina cadeia terminada em sinal de menos
for(i = input_size; i > 0; i--){
if(input[i-0] != '\0' && input[i-0] != ' ' && input[i-0] == '-'){
return 0;
}
}
//Elimina cadeia que somente tem ponto, vírgula ou sinal de menos
if((input[0] == '.' || input[0] == ',' || input[0] == '-') && (input[1] == ' ' || input[1] == '\0')){
return 0;
}
for(i = 2; i < input_size; i++){
if((input[i-2] == ' ' || input[i-2] == '\0') &&
(input[i-1] == '.' || input[i-1] == ',' || input[i-1] == '-') &&
(input[i] == ' ' || input[i] == '\0')){
return 0;
}
}
return 1;
}
You’re not giving me the exact exit I wanted. But it made me realize that by forcing me to use as few variables as possible, I’m making the logic of my code too complicated and making it difficult to read. Thank you.
– CL27
@CL27 Don’t worry about the number of variables in your code: The compiler will simplify many things to improve the performance of your program, anyway. Try using the number of variables you find necessary, and be descriptive when creating a variable.
– Nexus
but worry about simplifying yes - this code is much better than the one presented in the question - in particular pro only go through the sequence once, and not with a loop for each.
– jsbueno
and the essential of the answer,q I’m right here, is qeu in C if you can compare characters directly by the code - then the expression
( (input[i] - '0') < 0 || (input[i] - '0') > 9 )
is what really makes all the difference instead of comparing the character with each hard-coded digit.– jsbueno