0% found this document useful (0 votes)
16 views3 pages

TD3 (Corrigé)

This document contains the code solutions for two programming exercises. The first exercise defines a rational number as a struct with numerator and denominator fields, and includes functions to create, simplify, display, compare, add rational numbers. The second exercise includes functions to count the occurrences of a character in a file, convert a character to uppercase, and copy the contents of one file to another while converting all characters to uppercase. Both exercises include a main function to test the code.

Uploaded by

Soufiane Hamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

TD3 (Corrigé)

This document contains the code solutions for two programming exercises. The first exercise defines a rational number as a struct with numerator and denominator fields, and includes functions to create, simplify, display, compare, add rational numbers. The second exercise includes functions to count the occurrences of a character in a file, convert a character to uppercase, and copy the contents of one file to another while converting all characters to uppercase. Both exercises include a main function to test the code.

Uploaded by

Soufiane Hamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Département d’Informatique

Module I132 : Algorithmique et programmation 2


Parcours MIP (S3)
22 mars 2020
Pr. S. ANTER

Corrigé TD No 3
Ex.1 :
1 # include < stdio .h >
2 typedef struct {
3 int numerator ;
4 int denominator ;
5 } rational ;
6
7 int pgcd ( int a , int b ) {
8 if ( b ==0) return a ;
9 return pgcd (b , a % b ) ;
10 }
11
12 rational creer ( int n , int d ) {
13 rational r ={ n , d };
14 return r ;
15 }
16
17 rational simplifier0 ( rational r ) {
18 rational a ;
19 int p = pgcd ( r . denominator , r . numerator ) ;
20 a . denominator = r . denominator / p ;
21 a . numerator = r . numerator / p ;
22 return a ;
23 }
24
25 void afficher ( rational r ) {
26 if ( r . denominator !=0) printf ( " % d /% d \ n " ,r . numerator , r . denominator )
;
27 else printf ( " Erreur \ n " ) ;
28 }
29
30 void simplifier ( rational * r ) {
31 int p = pgcd (r - > denominator ,r - > numerator ) ;
32 r - > denominator /= p ;
33 r - > numerator /= p ;
34 }
35
36 rational oppose ( rational r ) {
37 rational a ={ r . denominator , r . numerator };
38 return a ;
39 }
40
41 int egale ( rational r1 , rational r2 ) {
42 simplifier (& r1 ) ;
43 simplifier (& r2 ) ;
44 return r1 . denominator == r2 . denominator && r1 . numerator == r2 .
numerator ;
45 }
46
47 rational somme ( rational r1 , rational r2 ) {
48 rational s ;

1
49 s . numerator = r1 . numerator * r2 . denominator + r1 . denominator * r2 .
numerator ;
50 s . denominator = r1 . denominator * r2 . denominator ;
51 return s ;
52 }
53
54 main () {
55 rational r1 , r2 ;
56 r1 = creer (12 ,9) ;
57 afficher ( r1 ) ;
58 r2 = simplifier0 ( r1 ) ;
59 afficher ( r2 ) ;
60 simplifier (& r1 ) ;
61 afficher ( r1 ) ;
62 r2 = oppose ( r1 ) ;
63 afficher ( r2 ) ;
64 if ( egale ( r1 , r2 ) ) printf ( " r1 = r2 \ n " ) ;
65 else printf ( " r1 != r2 \ n " ) ;
66 }

Ex.2 :
1 # include < stdio .h >
2 int occurence ( char * fichier , char c ) {
3 FILE * f = fopen ( fichier , " r " ) ;
4 char a ;
5 int n =0;
6 if ( f != NULL ) {
7 do {
8 a = fgetc ( f ) ;
9 if ( c == a ) n ++;
10 } while ( a != EOF ) ;
11 }
12 else {
13 printf ( " Erreur d ’ ouverture \ n " ) ;
14 exit (0) ;
15 }
16 return n ;
17 }
18
19 char majuscule ( char c ) {
20 if (c >= ’a ’ && c <= ’z ’) return c -32;
21 return c ;
22 }
23
24 void copie_majuscule ( char * src , char * dest ) {
25 FILE * fsrc , * fdest ;
26 char c ;
27 fsrc = fopen ( src , " r " ) ;
28 fdest = fopen ( dest , " w " ) ;
29 if ( fsrc != NULL && fdest != NULL ) {
30 do {
31 c = fgetc ( fsrc ) ;
32 fputc ( majuscule ( c ) , fdest ) ;
33 } while ( c != EOF ) ;
34 }
35 else {
36 printf ( " Erreur d ’ ouverture \ n " ) ;
37 exit (0) ;

2
38 }
39 }
40
41 main () {
42 int n ;
43 char a ;
44 printf ( " Donner un caract è re : " ) ;
45 scanf ( " % c " ,& a ) ;
46 n = occurence ( " fichier . txt " ,a ) ;
47 printf ( " occurence (% c ) =% d \ n " ,a , n ) ;
48 printf ( " Majuscule de ’% c ’ est ’% c ’\ n " ,a , majuscule ( a ) ) ;
49 copie_majuscule ( " src . txt " ," dest . txt " ) ;
50 }

You might also like