Advertisement
madalinaradu

Matrice

Apr 15th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4.  
  5. class Matrice{
  6.     private:
  7.         int n;
  8.         int m;
  9.         float *p;
  10.     public:
  11.         Matrice(int n, int m);
  12.         Matrice(const Matrice &a);
  13.         ~Matrice();
  14.         void citire();
  15.         void afisare();
  16.         void setLiniiColoane(int n, int m);
  17.         void setElement(int i, int j, float val);//seteaza elem a[i][j] cu valoarea indicata
  18.         int getLinii();
  19.         int getColoane();
  20.         float getElement(int i, int j) const;
  21.         friend Matrice operator*(Matrice a, Matrice b);
  22.         float norma();
  23. };
  24.  
  25. Matrice::Matrice(int n, int m){
  26.     int i,j;
  27.     this->n = n;
  28.     this->m = m;
  29.     this->p = new float[n*m];
  30.     for(i=0;i<n;i++){
  31.         for(j=0;j<m;j++){
  32.             this->p[i*m+j]=0;
  33.         }
  34.     }
  35.      cout<<"Apel constructor cu parametri"<<endl;
  36. }
  37.  
  38. Matrice::Matrice(const Matrice &a){
  39.     int i,j;
  40.     this->n = a.n;
  41.     this->m = a.m;
  42.     this->p = new float[a.n*a.m];
  43.     for(i=0;i<n;i++){
  44.         for(j=0;j<m;j++){
  45.             this->p[i*m+j]= a.p[i*m+j];
  46.         }
  47.     }
  48.      cout<<"Apel constructor copiere"<<endl;
  49. }
  50.  
  51. Matrice::~Matrice(){
  52.     if(p!=0)
  53.         delete p;
  54.  cout<<"Apel destructor "<<endl;
  55. }
  56. void Matrice::citire(){
  57.     int i,j;
  58.     cout<<"Dati elementele"<<endl;
  59.     for(i=0;i<n;i++){
  60.         for(j=0;j<m;j++){
  61.             cout<<"elementul ["<<i<<"]["<<j<<"]= ";
  62.             cin>>p[i*m+j];
  63.         }
  64.     }
  65.     cout<<endl;
  66. }
  67.  
  68. void Matrice::afisare(){
  69.     int i,j;
  70.      for(i=0;i<n;i++){
  71.         for(j=0;j<m;j++){
  72.             cout<<p[i*m+j]<<"  ";
  73.         }
  74.         cout<<endl;
  75.     }
  76. }
  77. void Matrice:: setLiniiColoane(int n, int m){
  78.     if((this->n)*(this->m)< n*m){
  79.         delete p;
  80.         p= new float(n*m);
  81.     }
  82.     this->n = n;
  83.     this->m = m;
  84. }
  85. void Matrice::setElement(int i, int j, float val){//schimbam valorile elementelor de pe pozitia [i][j]
  86.             this->p[i*m+j]=val;
  87. }
  88.  
  89. int Matrice::getLinii(){
  90.     return n;
  91. }
  92. int Matrice::getColoane(){
  93.     return m;
  94. }
  95.  
  96. float Matrice::getElement(int i, int j) const{
  97.     return p[i*m+j];
  98. }
  99.  
  100. Matrice produs(Matrice &a, Matrice &b){
  101.     int i,j,k;
  102.     Matrice c(a.getLinii(),b.getColoane());
  103.     for(i=0; i<a.getLinii(); i++){
  104.         for(j=0; j<b.getColoane(); j++){
  105.                 float suma=0;
  106.             for(k=0; k<b.getLinii(); k++){
  107.                 suma+=a.getElement(i,k)*b.getElement(k,j);
  108.             }
  109.             c.setElement(i,j,suma);
  110.         }
  111.     }
  112.     return c;
  113. }
  114. Matrice operator*(Matrice a,Matrice b){
  115.     int i,j,k;
  116.     Matrice c(a.getLinii(),b.getColoane());
  117.     for(i=0; i<a.getLinii(); i++){
  118.         for(j=0; j<b.getColoane(); j++){
  119.                 float suma=0;
  120.             for(k=0; k<b.getLinii(); k++){
  121.                 suma+=a.getElement(i,k)*b.getElement(k,j);
  122.             }
  123.             c.setElement(i,j,suma);
  124.         }
  125.     }
  126.     return c;
  127. }
  128.  
  129. int main(){
  130.     int n=2,m=3,p=2;
  131.     Matrice a(n,m);
  132.     cout<<"elem matricii a"<<endl;
  133.     a.citire();
  134.     a.afisare();
  135.     cout<<"-----------------------"<<endl;
  136.     //Matrice b = a;//apeleaza const de copiere
  137.     Matrice b(m,p);
  138.     cout<<"elem matricii b"<<endl;
  139.     b.citire();
  140.     b.afisare();
  141.     cout<<"-----------------------"<<endl;
  142.     Matrice c = produs(a,b);
  143.     cout<<"elem matricii produs"<<endl;
  144.     c.afisare();
  145.     cout<<"-----------------------"<<endl;
  146.     Matrice d = a*b;
  147.     cout<<"elem matricii produs"<<endl;
  148.     d.afisare();
  149.  
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement