Correction Td4
Correction Td4
Chanes de caractres
c) {
return
NULL ;
Exercice 2 int
compte (
int
s
char s , char
0; c ); { != NULL)
c)
n =
while
s }
recherche ( s , (s
return
n;
Polynmes
{
Exercice 3 struct p o l y n o m e somme_polynome ( struct p o l y n o m e P , struct p o l y n o m e Q) struct p o l y n o m e r e s u l t a t = m a l l o c ( s i z e o f ( struct p o l y n o m e ) ) ; int i ; i f ( P>d e g r e < Q>d e g r e ) { struct p o l y n o m e T = P ; P = Q ; Q = T ;
}
return
}
/ >d e g r e = P>d e g r e ; r e s u l t a t c o e f f i c i e n t s = m a l l o c ( ( P >d e g r e + 1 ) s i z e o f ( double ) ) ; > for ( i = 0 ; i <= Q>d e g r e ; i ++) r e s u l t a t c o e f f i c i e n t s [ i ] = P >c o e f f i c i e n t s [ i ] + Q >c o e f f i c i e n t s > for ( ; i <= P>d e g r e ; i ++) / i = Q>d e g r e + 1 / r e s u l t a t c o e f f i c i e n t s [ i ] = P >c o e f f i c i e n t s [ i ] ; >
resultat resultat ;
[ i ];
return
}
>d e g r e = P>d e g r e + Q >d e g r e ; > c o e f f i c i e n t s = m a l l o c ( ( r e s u l t a t >d e g r e + 1 ) s i z e o f ( double ) ) ; for ( i = 0 ; i <= r e s u l t a t >d e g r e ; i ++) resultat coefficients [ i ] = 0.0; > for ( i = 0 ; i <= P>d e g r e ; i ++) for ( j = 0 ; j <= Q>d e g r e ; j ++) resultat coefficients [ i + j ] + > = P >c o e f f i c i e n t s [ i ] Q >c o e f f i c i e n t s [ j ] ;
resultat ;
Matrices
{
= lignes ; = colonnes ; A > c o e f f i c i e n t s = m a l l o c ( A > l i g n e s s i z e o f ( double ) ) ; for ( i = 0 ; i < A> l i g n e s ; i ++) A > c o e f f i c i e n t s [ i ] = m a l l o c ( A >c o l o n n e s s i z e o f ( double ) ) ;
return
A;
Exercice 5 struct m a t r i c e p r o d u i t _ m a t r i c e ( struct struct m a t r i c e r e s u l t a t ; int i , j , k ; i f ( A>c o l o n n e s != B> l i g n e s ) return NULL ; for
resultat = 0; ( j = matrice
A , struct
matrice
B)
B >c o l o n n e s ) ;
for
( i
i ++)
j < B >c o l o n n e s ; j ++) { > c o e f f i c i e n t s [ i ] [ j ] = 0 . 0 ; for ( k = 0 ; k < A>c o l o n n e s ; k++) resultat coefficients [ i ] [ j ] + > = A >c o e f f i c i e n t s B> c o e f f i c i e n t s resultat
[ i ][ k] [k ][ j ];
return
}
resultat ;
struct
matrice
A)
i ++)
f r e e (A > c o e f f i c i e n t s [ i ] ) ; f r e e (A > c o e f f i c i e n t s ) ; f r e e (A ) ; }
4
void
Pile
int p t r for
ptr = ( i empile_pile_simple ( , i ;
Exercice 7 struct
pile_simple
pile
int
n)
> t a i l l e + 1 ) s i z e o f ( int ) ) ; > t a i l l e ; i ++) ptr [ i ] = pile >e l e m e n t s [ i ] ; p t r [ i ] = n ; / p t r [ p i l e t a i l l e ] = n / > i f ( p i l e > t a i l l e != 0 ) free ( pile >e l e m e n t s ) ; pile >e l e m e n t s = p t r ; p i l e t a i l l e ++; >
malloc (( pile = 0; i < pile
void
struct p i l e _ s i m p l e p i l e , int n ) { > t a i l l e == 0 ) pile >e l e m e n t s = NULL ; / r e a l l o c (NULL, . ) q u i v a u t m a l l o c ( . ) / pile >e l e m e n t s = r e a l l o c ( p i l e >e l e m e n t s , ( p i l e t a i l l e + 1 ) s i z e o f ( int ) ) ; > pile >e l e m e n t s [ p i l e t a i l l e ] = n ; > p i l e t a i l l e ++; > if
empile_pile_simple2 ( ( pile
int
int p t r
pile ptr
depile_pile_simple ( , i , n =
struct
0)
pile_simple pile
pile
>e l e m e n t s [
pile ) { > t a i l l e
1];
> t a i l l e ; i f ( p i l e > t a i l l e
=
!=
e lse for
pile
malloc ( pile
/ p i l e t a i l l e == 0 >
ptr ( i = NULL ; = 0; = i < pile
> t a i l l e s i z e o f ( int ) ) ; /
; i ++)
pile
> t a i l l e >e l e m e n t s [
i ];
return
}
>e l e m e n t s ) ; >e l e m e n t s = p t r ;
n;
int
int
struct p i l e _ s i m p l e p i l e ) { >e l e m e n t s [ p i l e > t a i l l e 1 ] ; p i l e t a i l l e ; > i f ( p i l e > t a i l l e != 0 ) pile >e l e m e n t s = r e a l l o c ( p i l e >e l e m e n t s , pile t a i l l e > s i z e o f ( int ) ) ; e lse { / p i l e > t a i l l e == 0 / free ( pile >e l e m e n t s ) ; pile >e l e m e n t s = NULL ;
depile_pile_simple2 ( n = pile n;
return
Exercice 8 void if
empile_pile_amortie ( ( pile
struct
pile_amortie {
pile
int
n)
el se
ptr
pile
>c a p a c i t e =
2;
>c a p a c i t e = 1 ; >c a p a c i t e ( s i z e o f ( int ) ) ) ; for ( i = 0 ; i < p i l e > t a i l l e ; i ++) ptr [ i ] = pile >e l e m e n t s [ i ] ; i f ( p i l e > t a i l l e != 0 ) free ( pile >e l e m e n t s ) ; pile >e l e m e n t s = p t r ;
pile = malloc ( pile } pile pile }
= n;
void
if
empile_pile_amortie2 ( ( pile
struct p i l e _ a m o r t i e p i l e , int n ) { > t a i l l e == p i l e >c a p a c i t e ) { i f ( p i l e >c a p a c i t e == 0 ) { pile >c a p a c i t e = 1 ; pile >e l e m e n t s = NULL ; / r e a l l o c (NULL, . ) q u i v a u t m a l l o c ( . ) / el se
pile >c a p a c i t e = 2 ; >e l e m e n t s = r e a l l o c ( p i l e >e l e m e n t s , pile >c a p a c i t e
pile
s i z e o f ( int ) ) ) ;
} pile pile }
= n;
int
int
depile_pile_amortie ( n = pile
struct p i l e _ a m o r t i e p i l e ) { >e l e m e n t s [ p i l e > t a i l l e 1 ] ; ; p i l e t a i l l e ; > i f ( p i l e > t a i l l e <= p i l e >c a p a c i t e / 4 ) { int p t r , i ; pile >c a p a c i t e /= 2 ; i f ( p i l e >c a p a c i t e != 0 ) ptr = malloc ( pile >c a p a c i t e s i z e o f ( int ) ) ; el se / p i l e >c a p a c i t e == 0 / for
pile ptr ( i = NULL ; = 0; = i < pile ptr [ i ] free ( pile pile
> t a i l l e >e l e m e n t s [
i ++)
i ];
>e l e m e n t s ) ; >e l e m e n t s = p t r ;
return
n;
int
int
depile_pile_amortie2 ( n = pile
struct p i l e _ a m o r t i e p i l e ) { >e l e m e n t s [ p i l e > t a i l l e 1 ] ; p i l e t a i l l e ; > i f ( p i l e > t a i l l e <= p i l e >c a p a c i t e / 4 ) { pile >c a p a c i t e /= 2 ; i f ( p i l e >c a p a c i t e != 0 ) pile >e l e m e n t s = r e a l l o c ( p i l e >e l e m e n t s , pile >c a p a c i t e s i z e o f ( int ) ) ; el se { / p i l e >c a p a c i t e == 0 / free ( pile >e l e m e n t s ) ;
4
pile } }
>e l e m e n t s
= NULL ;
return
n;