Exercice langage C
[tab name=’Exercice langage C’]
[/tab][tab name=’Correction’]
#include <iostream.h> #include<stddef.h> // utile pour size_t #include<stdlib.h> class matrice{ int nbres_lg; int nbres_cl; int *tab; public: matrice(int nblg,int nbcl); friend ostream& operator<<( ostream& tmp,matrice d); int* operator()(matrice& d); void * operator new (size_t); void operator delete(void *p); void affiche(); }; matrice::matrice(int nblg,int nbcl){ nbres_lg=nblg; nbres_cl=nbcl; tab=new int[nbres_lg*nbres_cl]; for (int i=0;i<nbres_lg*nbres_cl;i++) tab[i]=0; } ostream& operator<<(ostream& tmp, matrice d){ for (int i=0; i<d.nbres_lg;i++){ for(int j=0;j<d.nbres_cl;j++){ tmp<<"tab["<<i<<"]["<<j<<"]="<<d.tab[(2*i*(d.nbres_cl- 1))+j]<<endl; //voir cours de Mr Wilfart //sur l'allocation des tableaux } } return tmp; } int* matrice::operator ()(matrice& d){ for (int l=0;l<d.nbres_lg;l++) { for(int c=0;c<d.nbres_cl;c++) { cout<<"entrez la valeur que vous desirez pour la matrice["<<l<<";"<<c<<"]:"<<endl; cin>>d.tab[(2*l*(d.nbres_cl-1))+c]; } } return d.tab; } void * matrice::operator new(size_t sz){ cout<<"appel new"<<endl; return malloc(sz);} void matrice::operator delete(void *p){ free(p); cout<<"appel delete\n"; } void matrice::affiche(){ for (int l=0;l<nbres_lg;l++) { for(int c=0;c<nbres_cl;c++) { cout<<tab[(2*l*(nbres_cl-1))+c]<<endl; } } } void main(){ matrice a(2,2); a(a); a.affiche(); cout<<a; matrice *b=new matrice(a); // en paramètre de ou il va chercher sa reference // c a d d'une autre matrice cout<<a; delete b; }
Exercice langage C
Exercice informatique, Correction exercice, exercices corrigés, Solution exercice, Exercice langage C
[/tab][end_tabset skin= »lightness » ]