




Étudies grâce aux nombreuses ressources disponibles sur Docsity
Gagnz des points en aidant d'autres étudiants ou achete-les avec un plan Premium
Cette page n'est pas visible dans l'aperçu
Ne manques pas les parties importantes!





ere : Informatique Ann´ee : Deuxieme Semestre : PremierCORRIG´E
1 #i n c l u d e 2 3 c l a s s C o n t a i n e r { 4 protected : 5 i n t n e x t ; 6 i n t s i z e ; 7 f l o a t ∗ tab ; 8 9 p u b l i c : 10 C o n t a i n e r ( ) ; 11 C o n t a i n e r ( i n t ) ; 12 C o n t a i n e r ( const C o n t a i n e r &); 13 ˜ C o n t a i n e r ( ) ; 14 15 // Test i f not empty 16 bool operator! ( ) ; 17 18 // Push 19 C o n t a i n e r& operator+=( f l o a t ) ; 20 21 // Access l a s t e l e m e n t 22 f l o a t& operator ∗ ( ) ; 23 24 // Pop ( i n t −> p o s t f i x ) 25 C o n t a i n e r& operator −−(i n t ) ; 26 27 f r i e n d s t d : : ostream &operator<< ( 28 s t d : : ostream &stream , const C o n t a i n e r &c ) ;
1 #i n c l u d e 2 #i n c l u d e " Container. hpp " 3 4 c l a s s Queue : p u b l i c C o n t a i n e r { 5 i n t f i r s t ; 6 bool atLeastOne ; 7 8 p u b l i c : 9 Queue ( ) ; 10 Queue ( i n t ) ; 11 e x p l i c i t Queue ( const Queue &); 12 ˜Queue ( ) ; 13 14 bool operator! ( ) ; 15 16 Queue& operator+=( f l o a t ) ; 17 18 f l o a t& operator ∗ ( ) ; 19 20 Queue& operator −−(i n t ) ; 21 22 operator s t d : : s t r i n g ( ) const ; 23 24 f r i e n d s t d : : ostream &operator<< ( 25 s t d : : ostream &stream , const Queue &q ) ; 26 } ;
1 #i n c l u d e " Container. hpp " 2 #i n c l u d e 3 4 using namespace s t d ; 5 6 C o n t a i n e r : : C o n t a i n e r ( ) : 7 n e x t ( 0 ) , 8 s i z e ( 0 ) { 9 c o u t << " Container () : " << ∗ t h i s << e n d l ; 10 } 11 12 13 C o n t a i n e r : : C o n t a i n e r ( i n t s i z e ) : 14 n e x t ( 0 ) , 15 s i z e ( s i z e ) , 16 tab (new f l o a t [ s i z e ] ) { 17 cout<< " Container ( int ) : " << ∗ t h i s << e n d l ; 18 } 19 20 21 C o n t a i n e r : : C o n t a i n e r ( const C o n t a i n e r& c ) : 22 n e x t ( c. n e x t ) , 23 s i z e ( c. s i z e ) , 24 tab (new f l o a t [ s i z e ] ) { 25 i n t i ; 26 f o r ( i = 0 ; i < n ex t ; i ++) 27 tab [ i ] = c. tab [ i ] ; 28 c o u t << " Container ( Container &) : " << 29 ∗ t h i s << e n d l ; 30 } 31 32 33 C o n t a i n e r : : ˜ C o n t a i n e r ( ) { 34 d e l e t e [ ] tab ; 35 c o u t << " ~ Co ntainer () " << e n d l ; 36 } 37 38 39 bool C o n t a i n e r : : operator! ( ) { 40 return ( n e x t != 0 ) ; 41 } 42 43 44 C o n t a i n e r& C o n t a i n e r : : operator+=( f l o a t f ) { 45 tab [ n e x t++] = f ; 46 return ∗ t h i s ; 47 } 48 49 f l o a t& C o n t a i n e r : : operator ∗ ( ) { 50 return tab [ n e x t ] ; 51 } 52 53 C o n t a i n e r& C o n t a i n e r : : operator −−(i n t ) { 54 next −−; 55 return ∗ t h i s ; 56 } 57 58 ostream &operator<< ( ostream &stream , 59 const C o n t a i n e r &c ) { 60 stream << " size : " << c. s i z e 61 << " , next : " << c. n ex t ; 62 return stream ; 63 }
1 #i n c l u d e " Queue. hpp " 2 #i n c l u d e 3 4 using namespace s t d ; 5 6 Queue : : Queue ( ) : 7 C o n t a i n e r ( ) , 8 f i r s t ( 0 ) , atLeastOne ( f a l s e ) { 9 c o u t << " Queue () : " << ∗ t h i s << e n d l ; 10 } 11 12 Queue : : Queue ( i n t s i z e ) : 13 C o n t a i n e r ( s i z e ) , 14 f i r s t ( 0 ) , 15 atLeastOne ( f a l s e ) { 16 c o u t << " Queue ( int ) : " << ∗ t h i s << e n d l ; 17 } 18 19 Queue : : Queue ( const Queue& q ) : 20 f i r s t ( q. f i r s t ) , 21 atLeastOne ( q. atLeastOne ) { 22 i n t i ; 23 f o r ( i = 0 ; i < s i z e ; i ++) 24 tab [ i ] = q. tab [ i ] ; 25 c o u t << " Queue ( Queue &) : " << ∗ t h i s << e n d l ; 26 } 27 28 29 Queue : : ˜ Queue ( ) { 30 c o u t << " ~ Queue () " << e n d l ; 31 } 32 33 bool Queue : : operator! ( ) { 34 return ( atLeastOne ) ; 35 } 36 37 Queue& Queue : : operator+=( f l o a t f ) { 38 i f (! atLeastOne ) 39 atLeastOne = true ; 40 tab [ n e x t ] = f ; 41 n ex t = ( n ex t + 1 ) % s i z e ; 42 return ∗ t h i s ; 43 } 44 45 f l o a t& Queue : : operator ∗ ( ) { 46 return tab [ f i r s t ] ; 47 } 48 49 Queue& Queue : : operator −−(i n t ) { 50 f i r s t = ( f i r s t + 1 ) % s i z e ; 51 i f ( f i r s t == n e x t ) 52 atLeastOne = f a l s e ; 53 return ∗ t h i s ; 54 } 55 56 Queue : : operator s t d : : s t r i n g ( ) const { 57 return " 42 " ; 58 } 59 60 ostream &operator<< ( ostream &stream , 61 const Queue &q ) { 62 // stream << ”Queue : ” << ∗( d y n a m i c c a s t <c o n s t C o n t a i n e r 63 // stream << ”Queue : ” << ∗ ( ( c o n s t C o n t a i n e r ∗) &q ) 64 stream << " Queue : " << ( ( C o n t a i n e r ) q ) 65 << " , first : " << q. f i r s t << 66 " , atLe ast One : " << q. atLeastOne 67 << e n d l ; 68 return stream ; 69 }
1 // Ligne 6 de Main1. cpp : 2 C o n t a i n e r ( i n t ) : s i z e : 1 0 , ne x t : 0 // l i g n e s 17 Cont , [ 6 0 Cont ] 3 Queue ( i n t ) : [ C o n t a i n e r ( C o n t a i n e r &) : s i z e : 1 0 , n ex t : 0 ] // l i g n e s 16 Queu , [ 2 8 Cont , 60 Cont ] 4 [ Queue : s i z e : 1 0 , ne x t : 0 , f i r s t : 0 , atLeastOne : 0 ] // [ l i g n e s 58 Queu , 60 Cont ] 5 ˜ C o n t a i n e r ( ) // l i g n e 35 Cont 6 7 // Ligne 7 de Main1. cpp : 8 C o n t a i n e r ( i n t ) : s i z e : 5 , n e x t : 0 // l i g n e s 17 Cont , [ 6 0 Cont ] 9 Queue ( i n t ) : [ C o n t a i n e r ( C o n t a i n e r &) : s i z e : 5 , ne x t : 0 ] // l i g n e s 16 Queu , [ 2 8 Cont , 60 Cont ] 10 [ Queue : s i z e : 5 , ne x t : 0 , f i r s t : 0 , atLeastOne : 0 ] // [ l i g n e s 58 Queu , 60 Cont ] 11 ˜ C o n t a i n e r ( ) // l i g n e 35 Cont 12 13 // Ligne 10 de Main1. cpp : 14 ˜ C o n t a i n e r ( ) // l i g n e 35 Cont 15 16 // Ligne 12 de Main1. cpp : 17 ˜Queue ( ) // l i g n e 30 Queu 18 ˜ C o n t a i n e r ( ) // l i g n e 35 Cont
1 #include 5 6 using namespace s t d ; 7 8 9 S t a c k : : S t a c k ( i n t s i z e ) : C o n t a i n e r ( s i z e ) { } 10 11 ostream &operator<< ( ostream &stream , 12 const S t a c k &s ) 13 { 14 stream << " Stack , size : " << s. s i z e 15 << " , next : " << s. n e xt ; 16 return stream ; 17 }
1 #include 2 3 c l a s s S t a c k : public C o n t a i n e r { 4 5 public : 6 S t a c k ( i n t ) ; 7 friend s t d : : ostream &operator<< ( 8 s t d : : ostream &stream , const S t a c k &q ) ; 9 } ;
1 #i n c l u d e
1 v i r t u a l ˜ C o n t a i n e r ( ) ; 2 v i r t u a l bool operator! ( ) ; 3 v i r t u a l C o n t a i n e r& operator+=(f l o a t ) ; 4 v i r t u a l f l o a t& operator ∗ ( ) ; 5 v i r t u a l C o n t a i n e r& operator−−(i n t ) ;
esa un ´el´ement d’une collection vide doit lever une exception de type out of range ;esa une m´ethode qui n’a pas de sens pour telle ou telle collection doit lever une exception deeres, mais ne possedent1 public : 2 v i r t u a l C o n t a i n e r& operator+=(f l o a t ) 3 throw ( s t d : : o v e r f l o w e r r o r ) ; 4 v i r t u a l f l o a t& operator ∗ ( ) 5 throw ( s t d : : o u t o f r a n g e ) ; 6 v i r t u a l C o n t a i n e r& operator−−(i n t ) 7 throw ( s t d : : u n d e r f l o w e r r o r ) ;
1 C o n t a i n e r& C o n t a i n e r : : operator+=(f l o a t f ) 2 throw ( o v e r f l o w e r r o r ) 3 { 4 i f ( n e x t == s i z e ) 5 throw o v e r f l o w e r r o r ( " error " ) ; 6 7 tab [ n e x t++] = f ; 8 return ∗ t h i s ; 9 } 10 11 12 f l o a t& C o n t a i n e r : : operator ∗ ( ) 13 throw ( o u t o f r a n g e ) 14 { 15 i f (! (! ∗ t h i s ) ) 16 throw o u t o f r a n g e ( " error " ) ; 17 return tab [ n e x t ] ; 18 } 19 20 21 C o n t a i n e r& C o n t a i n e r : : operator−−(i n t ) 22 throw ( u n d e r f l o w e r r o r ) 23 { 24 i f (! (! ∗ t h i s ) ) 25 throw u n d e r f l o w e r r o r ( " error " ) ; 26 27 next −−; 28 return ∗ t h i s ; 29 }
esa un ´el´ement de la collection) en prenant soin de donner1 public : 2 v i r t u a l f l o a t operator [ ] ( i n t ) 3 throw ( s t d : : o u t o f r a n g e , 4 s t d : : l o g i c e r r o r ) ;
1 f l o a t C o n t a i n e r : : operator [ ] ( i n t i n d e x ) 2 throw ( o u t o f r a n g e , l o g i c e r r o r ) 3 { 4 i f ( (! (! ∗ t h i s ) ) | | ( i n d e x >= ne x t ) ) 5 throw o u t o f r a n g e ( " error " ) ; 6 return tab [ i n d e x ] ;
1 private : 2 v i r t u a l f l o a t operator [ ] ( i n t ) 3 throw ( s t d : : o u t o f r a n g e , s t d : : l o g i c e r r o r ) ;
1 f l o a t Queue : : operator [ ] ( i n t ) 2 throw ( o u t o f r a n g e , l o g i c e r r o r ) { 3 throw l o g i c e r r o r ( " error " ) ; 4 }
1 #include 2 using namespace s t d ; 3 4 template