1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include int estentier( char ) ; int chaine2entier( char *) ; main() { /* Déclarations */ char CH[100]; /* chaîne numérique à convertir */ long N; /* résultat numérique */ printf ( "Entrez un nombre entier et positif : " ); gets (CH); printf ( "%s\n" ,CH) ; N = chaine2entier(CH) ; if (N<0) printf ( "%s ne représente pas correctement un entier positif.\n" , CH); else printf ( "La chaine %s a pour valeur %d\n" ,CH,N) ; } int chaine2entier( char *CH) { int I; int N = 0 ; int OK = 1 ; for (I=0; OK && CH[I]; I++) if (estentier(CH[I])) N = N*10 + (CH[I]- '0' ); else OK=0; if (OK) return N ; else return -1 ; } int estentier( char c) { if ((c>= '0' )&&(c<= '9' )) return 1 ; else return 0 ; } |
- Friday
- May 16th, 2025
- Ajouter un cours