Advertisement
BonuxDu13

controleSaisie

Aug 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include <stdbool.h>
  6. #ifdef _WIN64
  7. #include <windows.h>
  8. #define psleep(sec) Sleep((sec)*1000)
  9. #define clear() system("cls")
  10. #else
  11. #include <unistd.h>
  12. #define psleep(sec) sleep((sec))
  13. #define clear() system("clear")
  14. #endif
  15.  
  16. int64_t controleSaisie();
  17.  
  18. int main(void)
  19. {
  20. int64_t num;
  21. puts("Veuillez saisir un nombre\n");
  22. num = controleSaisie();
  23. printf("num vaut %"PRId64"\n", num);
  24. return 0;
  25. }
  26.  
  27. int64_t controleSaisie()
  28. {
  29. char nombre[19], chiffre[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','\0','.','-'};
  30. bool flag, depassement = 0;
  31. int64_t num;
  32. int i, j;
  33. for(i = 0 ; i < 19 ; i++) //initialisation d'un tableau qui contiendra le nombre à vérifier
  34. nombre[i] = '0';
  35. scanf("%18s", nombre);
  36. while(getchar() != '\n')
  37. depassement = 1; //si tampon non vide, le cadre de scanf est dépassé, overflow présummé quant à num
  38. for(i = 0 ; i < 19 ; i++)
  39. {
  40. flag = 0;
  41. for(j = 0 ; j < 13 ; j++)
  42. {
  43. if(nombre[i] == chiffre[j])
  44. flag = 1; //est ce que les caractères entrés sont tels qu'attendus?
  45. }
  46. if(!flag)
  47. {
  48. puts("Un nombre svp!\n");
  49. psleep(2);
  50. clear();
  51. return num = controleSaisie();
  52. }
  53. }
  54. if(depassement)
  55. {
  56. puts("Saisie hors limites numeriques. Recommencez\n");
  57. psleep(2);
  58. clear();
  59. return num = controleSaisie();
  60. }
  61. if(strchr(nombre,'.') != 0) //le retour de la fonction doit etre un entier, pour des calculs precis en C
  62. {
  63. puts("Nombre decimal saisi. Recommencez\n");
  64. psleep(2);
  65. clear();
  66. return num = controleSaisie();
  67. }
  68. num = strtoll(nombre, NULL, 10);
  69. return num;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement