Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include <stdbool.h>
- #ifdef _WIN64
- #include <windows.h>
- #define psleep(sec) Sleep((sec)*1000)
- #define clear() system("cls")
- #else
- #include <unistd.h>
- #define psleep(sec) sleep((sec))
- #define clear() system("clear")
- #endif
- int64_t controleSaisie();
- int main(void)
- {
- int64_t num;
- puts("Veuillez saisir un nombre\n");
- num = controleSaisie();
- printf("num vaut %"PRId64"\n", num);
- return 0;
- }
- int64_t controleSaisie()
- {
- char nombre[19], chiffre[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','\0','.','-'};
- bool flag, depassement = 0;
- int64_t num;
- int i, j;
- for(i = 0 ; i < 19 ; i++) //initialisation d'un tableau qui contiendra le nombre à vérifier
- nombre[i] = '0';
- scanf("%18s", nombre);
- while(getchar() != '\n')
- depassement = 1; //si tampon non vide, le cadre de scanf est dépassé, overflow présummé quant à num
- for(i = 0 ; i < 19 ; i++)
- {
- flag = 0;
- for(j = 0 ; j < 13 ; j++)
- {
- if(nombre[i] == chiffre[j])
- flag = 1; //est ce que les caractères entrés sont tels qu'attendus?
- }
- if(!flag)
- {
- puts("Un nombre svp!\n");
- psleep(2);
- clear();
- return num = controleSaisie();
- }
- }
- if(depassement)
- {
- puts("Saisie hors limites numeriques. Recommencez\n");
- psleep(2);
- clear();
- return num = controleSaisie();
- }
- if(strchr(nombre,'.') != 0) //le retour de la fonction doit etre un entier, pour des calculs precis en C
- {
- puts("Nombre decimal saisi. Recommencez\n");
- psleep(2);
- clear();
- return num = controleSaisie();
- }
- num = strtoll(nombre, NULL, 10);
- return num;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement