I132 - TP 3 (Solution)
I132 - TP 3 (Solution)
Corrigé TP No 3
Ex.1 :
1 # include < stdio .h >
2 # include < string .h >
3 # define NB 5
4 # define L 7
5 int hamming ( char * mot1 , char * mot2 ) {
6 int i =0 , s =0;
7 while ( mot1 [ i ]!= ’ \0 ’) {
8 if ( mot1 [ i ]!= mot2 [ i ]) s ++;
9 i ++;
10 }
11 return s ;
12 }
13 int distanceLangage ( char langage [ NB ][ L ]) {
14 int d ,i ,j , m ;
15 d = hamming ( langage [0] , langage [1]) ;
16 for ( i =0; i < NB ; i ++) {
17 for ( j = i +1; j < NB ; j ++) {
18 m = hamming ( langage [ i ] , langage [ j ]) ;
19 if (d > m ) d = m ;
20 }
21 }
22 return d ;
23 }
24 main () {
25 char langage [ NB ][ L ]={ " AERDFR " ," ERDFRT " ," AZRDIT " ," TEUDFR " ,"
ERDTEY " };
26 printf ( " % d \ n " , distanceLangage ( langage ) ) ;
27 }
Ex.2 :
1 # include < stdio .h >
2 typedef struct {
3 float reel ;
4 float imaginaire ;
5 } complexe ;
6 complexe creer ( float x , float y ) {
7 complexe z ={ x , y };
8 return z ;
9 }
10 void afficher ( complexe z ) {
11 printf ( " %.2 f + i (%.2 f ) \ n " ,z . reel , z . imaginaire ) ;
12 }
13 complexe somme ( complexe z1 , complexe z2 ) {
14 complexe z ;
15 z . reel = z1 . reel + z2 . reel ;
16 z . imaginaire = z1 . imaginaire + z2 . imaginaire ;
17 return z ;
18 }
19 complexe difference ( complexe z1 , complexe z2 ) {
1
20 complexe z ;
21 z . reel = z1 . reel - z2 . reel ;
22 z . imaginaire = z1 . imaginaire - z2 . imaginaire ;
23 return z ;
24 }
25 complexe produit ( complexe z1 , complexe z2 ) {
26 complexe z ;
27 z . reel =( z1 . reel * z2 . reel - z1 . imaginaire * z2 . imaginaire ) ;
28 z . imaginaire = z1 . imaginaire * z2 . reel + z2 . imaginaire * z1 . reel ;
29 return z ;
30 }
31 float module ( complexe z ) {
32 return z . imaginaire * z . imaginaire + z . reel * z . reel ;
33 }
34 void conjugue ( complexe * z ) {
35 z - > imaginaire = -z - > imaginaire ;
36 }
37 main () {
38 complexe z , z1 , z2 ;
39 z = creer (3 ,6) ;
40 z1 = creer (4 , -2) ;
41 z2 = creer (7 ,8.5) ;
42 afficher ( z ) ;
43 afficher ( z1 ) ;
44 afficher ( z2 ) ;
45 z = somme ( z1 , z2 ) ;
46 afficher ( z ) ;
47 z = difference ( z1 , z2 ) ;
48 afficher ( z ) ;
49 z = produit ( z1 , z2 ) ;
50 afficher ( z ) ;
51 printf ( " %. f \ n " , module ( z ) ) ;
52 conjugue (& z ) ;
53 afficher ( z ) ;
54 }