0% found this document useful (0 votes)
120 views5 pages

Correction Td4

This document discusses data structures in C including strings, polynomials, matrices, and stacks. It provides examples of functions to: 1. Search for a character in a string and count the number of occurrences. 2. Perform operations on polynomial structures like addition and multiplication by defining a polynomial struct and functions to sum and multiply polynomials. 3. Allocate and free matrix structures and multiply matrices by defining a matrix struct and functions. 4. Implement simple and amortized stacks using arrays with functions to push and pop elements and dynamically reallocating memory as needed.

Uploaded by

Sarah Mc
Copyright
© Attribution Non-Commercial (BY-NC)
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)
120 views5 pages

Correction Td4

This document discusses data structures in C including strings, polynomials, matrices, and stacks. It provides examples of functions to: 1. Search for a character in a string and count the number of occurrences. 2. Perform operations on polynomial structures like addition and multiplication by defining a polynomial struct and functions to sum and multiply polynomials. 3. Allocate and free matrix structures and multiply matrices by defining a matrix struct and functions. 4. Implement simple and amortized stacks using arrays with functions to push and pop elements and dynamically reallocating memory as needed.

Uploaded by

Sarah Mc
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 5

TD 4 : Pointeurs et structures

Semaine du 18 fvrier 2008

Chanes de caractres
c) {

Exercice 1 char r e c h e r c h e ( char s , char i f ( s != NULL) { while ( s != ' \ 0 ' ) { i f ( s == c ) return s ;


s ++; } }

return

NULL ;

Exercice 2 int
compte (

int
s

char s , char
0; c ); { != NULL)

c)

n =

while
s }

recherche ( s , (s

n++; s ++; = recherche ( s , c );

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 ;

/ Q >d e g r e <= P >d e g r e

[ i ];

struct p o l y n o m e p r o d u i t _ p o l y n o m e ( 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 , j ;


resultat resultat

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
{

Exercice 4 struct m a t r i c e a l l o u e _ m a t r i c e ( int l i g n e s , int c o l o n n e s ) struct m a t r i c e A = m a l l o c ( s i z e o f ( struct m a t r i c e ) ) ; int i ;


A >l i g n e s A >c o l o n n e s

= 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)

a l l o u e _ m a t r i c e ( A >l i g n e s , i < A >l i g n e s ; 0;

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 ;

Exercice 6 void int for


libere_matrice ( i ; ( i = 0; i < A >l i g n e s ;

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 ++)

ptr [ i ] free ( pile

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)

> t a i l l e == p i l e >c a p a c i t e ) int p t r , i ; i f ( p i l e >c a p a c i t e != 0 )

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 }

>e l e m e n t s [ p i l e > t a i l l e > t a i l l e ++;

= 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 }

>e l e m e n t s [ p i l e > t a i l l e > t a i l l e ++;

= 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;

You might also like