Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "matrice.h"
- #include <stdlib.h>
- using namespace std;
- matrice::matrice(int l,int c, int** buf){
- this->l=l;
- this->c=c;
- this->buf=new int*[l];
- for(int i=0;i<l;i++){
- this->buf[i]=new int[c];
- for(int j=0;j<c;j++)
- this->buf[i][j]=buf[i][j];
- }
- }
- matrice::matrice(const matrice& m)
- {
- *this=m;
- }
- void matrice::afis() const{
- cout<<"Nr linii: "<<l<<endl<<"Nr. coloane: "<<c<<endl;
- for(int i=0;i<l;i++){
- for(int j=0;j<c;j++){
- cout<<buf[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- matrice::~matrice(){
- if(buf!=NULL)
- {
- for(int i=0;i<l;i++)
- if(buf[i]!=NULL)
- delete[]buf[i];
- delete[] buf;
- }
- }
- matrice& matrice::operator=(const matrice& m){
- l=m.l;
- c=m.c;
- buf=new int*[l];
- for(int i=0;i<l;i++){
- buf[i]=new int[c];
- for(int j=0;j<c;j++)
- buf[i][j]=m.buf[i][j];
- }
- return *this;
- }
- matrice& matrice::operator+=(const matrice& m){
- if (this->buf== NULL || m.buf==NULL)
- {
- cout<<"Adunarea nu a fost facuta!"<<endl;
- return *this;
- }
- if (l!=m.l || m.c!=c)
- {
- cout<<"Adunarea nu a fost facuta!"<<endl;
- return *this;
- }
- for(int i=0;i<l;i++)
- for(int j=0;j<c;j++)
- buf[i][j]+=m.buf[i][j];
- return *this;
- }
- #include <iostream>
- #include "matrice.h"
- using namespace std;
- const matrice operator+(const matrice& a,const matrice& b){
- if (a.buf== NULL || b.buf==NULL)
- {
- cout<<"Adunarea nu a fost facuta!"<<endl;
- return matrice();
- }
- if (a.l!=b.l || a.c!=b.c)
- {
- cout<<"Adunarea nu a fost facuta!"<<endl;
- return matrice();
- }
- int **v;
- v=new int*[3];
- for(int i=0;i<3;i++){
- v[i]=new int[3];
- }
- matrice m(a.l,b.c,v);
- for(int i=0;i<a.l;i++)
- for(int j=0;j<b.c;j++)
- m.buf[i][j]=a.buf[i][j]+b.buf[i][j];
- return m;
- }
- bool operator==(const matrice&a,const matrice&b){
- if (a.buf== NULL || b.buf==NULL)
- {
- cout<<"Compararea nu poate fi facuta!"<<endl;
- return false;
- }
- if (a.l!=b.l || a.c!=b.c)
- {
- cout<<"Matricile au ordin diferit. Compararea nu poate fi facuta!"<<endl;
- return false;
- }
- for(int i=0;i<a.l;i++)
- for(int j=0;j<b.c;j++)
- if(a.buf[i][j]!=b.buf[i][j])
- return false;
- return true;
- }
- bool operator!=(const matrice&a,const matrice&b){
- if (a.buf== NULL || b.buf==NULL)
- {
- cout<<"Compararea nu poate fi facuta!"<<endl;
- return false;
- }
- if (a.l!=b.l || a.c!=b.c)
- {
- cout<<"Matricile au ordin diferit. Compararea nu poate fi facuta!"<<endl;
- return true;
- }
- for(int i=0;i<a.l;i++)
- for(int j=0;j<b.c;j++)
- if(a.buf[i][j]!=b.buf[i][j])
- return true;
- return false;
- }
- int main(int argc, char** argv) {
- int **v;
- v=new int*[3];
- for(int i=0;i<3;i++){
- v[i]=new int[3];
- for(int j=0;j<3;j++)
- v[i][j]=i+j;
- }
- matrice m1(3,3,v);
- m1.afis();
- system("PAUSE");
- system("CLS");
- matrice m2;
- m2.afis();
- system("PAUSE");
- m2=m1;
- m2.afis();
- system("PAUSE");
- system("CLS");
- m2+=m1;
- m2.afis();
- system("PAUSE");
- system("CLS");
- matrice m3;
- m3=(m2+m1);
- m3.afis();
- system("PAUSE");
- system("CLS");
- if (m1==m2){
- cout<<"Egale!"<<endl;
- }
- else
- cout<<"Nu egale!"<<endl;
- system("PAUSE");
- system("CLS");
- if (m2==m2){
- cout<<"Egale!"<<endl;
- }
- else
- cout<<"Nu egale!"<<endl;
- system("PAUSE");
- system("CLS");
- if (m1!=m2){
- cout<<"Diferite!"<<endl;
- }
- else
- cout<<"Nu diferite!"<<endl;
- system("PAUSE");
- system("CLS");
- if (m2!=m2){
- cout<<"Diferite!"<<endl;
- }
- else
- cout<<"Nu diferite!"<<endl;
- system("PAUSE");
- system("CLS");
- return 0;
- }
- class matrice{
- int l,c;
- int **buf;
- public:
- matrice(int l=0,int c=0 ,int **buf=NULL);
- void afis()const; //sau suprascriu operator <<
- matrice(const matrice&);
- matrice& operator=(const matrice&);
- matrice& operator+=(const matrice&);//testare dimensiune
- friend const matrice operator+(const matrice&,const matrice&);
- friend bool operator==(const matrice&,const matrice&);
- friend bool operator!=(const matrice&,const matrice&);
- ~matrice();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement