Advertisement
Graf_Spee

[C] [FUNCTION] isNumeric

Jun 18th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdbool.h> // Include necessária
  2.  
  3. // Abaixo das Includes
  4.  
  5. bool isNumeric(const char string[]);
  6.  
  7. // No fim do código, depois da "int main"
  8.  
  9. bool isNumeric(const char string[]) // Aceita números inteiros de até 10 algarismos
  10. {
  11.      int c = 0;
  12.      bool foisinal;
  13.      for(c; c < 10; c++)
  14.      {
  15.             if(string[c] == '\0') break;
  16.             foisinal = false;
  17.             if(string[c] < '0' || string[c] > '9')
  18.             {
  19.                     if(c != 0)                                         return 0;
  20.                     else if(string[c] != '+' && string[c] != '-')      return 0;
  21.                     else foisinal = true;
  22.  
  23.             }
  24.      }
  25.      if(foisinal == false) return true;
  26.      else return false;
  27. }
  28.  
  29. // NÃO INCLUA O CÓDIGO ABAIXO NO SEU CÓDIGO !
  30.  
  31. // Forma de uso:
  32.  
  33. if(isNumeric("abc")) // retorna falso, ou seja, não entra no if
  34. if(isNumeric("123")) // retorna verdadeiro, ou seja, entra no if
  35.  
  36. // Façam bom uso xD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement