Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- using namespace std;
- class Matrice{
- private:
- int n;
- int m;
- float *p;
- public:
- Matrice(int n, int m);
- Matrice(const Matrice &a);
- ~Matrice();
- void citire();
- void afisare();
- void setLiniiColoane(int n, int m);
- void setElement(int i, int j, float val);//seteaza elem a[i][j] cu valoarea indicata
- int getLinii();
- int getColoane();
- float getElement(int i, int j) const;
- friend Matrice operator*(Matrice a, Matrice b);
- float norma();
- };
- Matrice::Matrice(int n, int m){
- int i,j;
- this->n = n;
- this->m = m;
- this->p = new float[n*m];
- for(i=0;i<n;i++){
- for(j=0;j<m;j++){
- this->p[i*m+j]=0;
- }
- }
- cout<<"Apel constructor cu parametri"<<endl;
- }
- Matrice::Matrice(const Matrice &a){
- int i,j;
- this->n = a.n;
- this->m = a.m;
- this->p = new float[a.n*a.m];
- for(i=0;i<n;i++){
- for(j=0;j<m;j++){
- this->p[i*m+j]= a.p[i*m+j];
- }
- }
- cout<<"Apel constructor copiere"<<endl;
- }
- Matrice::~Matrice(){
- if(p!=0)
- delete p;
- cout<<"Apel destructor "<<endl;
- }
- void Matrice::citire(){
- int i,j;
- cout<<"Dati elementele"<<endl;
- for(i=0;i<n;i++){
- for(j=0;j<m;j++){
- cout<<"elementul ["<<i<<"]["<<j<<"]= ";
- cin>>p[i*m+j];
- }
- }
- cout<<endl;
- }
- void Matrice::afisare(){
- int i,j;
- for(i=0;i<n;i++){
- for(j=0;j<m;j++){
- cout<<p[i*m+j]<<" ";
- }
- cout<<endl;
- }
- }
- void Matrice:: setLiniiColoane(int n, int m){
- if((this->n)*(this->m)< n*m){
- delete p;
- p= new float(n*m);
- }
- this->n = n;
- this->m = m;
- }
- void Matrice::setElement(int i, int j, float val){//schimbam valorile elementelor de pe pozitia [i][j]
- this->p[i*m+j]=val;
- }
- int Matrice::getLinii(){
- return n;
- }
- int Matrice::getColoane(){
- return m;
- }
- float Matrice::getElement(int i, int j) const{
- return p[i*m+j];
- }
- Matrice produs(Matrice &a, Matrice &b){
- int i,j,k;
- Matrice c(a.getLinii(),b.getColoane());
- for(i=0; i<a.getLinii(); i++){
- for(j=0; j<b.getColoane(); j++){
- float suma=0;
- for(k=0; k<b.getLinii(); k++){
- suma+=a.getElement(i,k)*b.getElement(k,j);
- }
- c.setElement(i,j,suma);
- }
- }
- return c;
- }
- Matrice operator*(Matrice a,Matrice b){
- int i,j,k;
- Matrice c(a.getLinii(),b.getColoane());
- for(i=0; i<a.getLinii(); i++){
- for(j=0; j<b.getColoane(); j++){
- float suma=0;
- for(k=0; k<b.getLinii(); k++){
- suma+=a.getElement(i,k)*b.getElement(k,j);
- }
- c.setElement(i,j,suma);
- }
- }
- return c;
- }
- int main(){
- int n=2,m=3,p=2;
- Matrice a(n,m);
- cout<<"elem matricii a"<<endl;
- a.citire();
- a.afisare();
- cout<<"-----------------------"<<endl;
- //Matrice b = a;//apeleaza const de copiere
- Matrice b(m,p);
- cout<<"elem matricii b"<<endl;
- b.citire();
- b.afisare();
- cout<<"-----------------------"<<endl;
- Matrice c = produs(a,b);
- cout<<"elem matricii produs"<<endl;
- c.afisare();
- cout<<"-----------------------"<<endl;
- Matrice d = a*b;
- cout<<"elem matricii produs"<<endl;
- d.afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement