Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "macierze2.h"
- Matrix::Matrix(int dr, int dc){
- wektor=new Vector *[dr];
- if(wektor==NULL){
- r=0;
- c=0;
- return ;
- }
- for(int i=0; i<dr; ++i)
- wektor[i]=new Vector(dc);
- r=dr;
- c=dc;
- }
- Matrix::Matrix(const Matrix &a){
- wektor=new Vector *[a.r];
- for(int i=0; i<a.r; ++i){
- wektor[i]=new Vector(a.c);
- *(wektor[i])=*(a.wektor[i]);
- }
- r=a.r;
- c=a.c;
- }
- Matrix::~Matrix(){
- delete [] wektor;
- }
- Vector& Matrix::operator[] (size_t index){
- return *(wektor[index]);
- }
- Matrix operator+(const Matrix &a, const Matrix &b){
- Matrix c(a.r,a.c);
- for(int i=0; i<a.r; ++i){
- *(c.wektor[i])=*(a.wektor[i])+*(b.wektor[i]);
- }
- return c;
- }
- Matrix operator-(const Matrix &a, const Matrix &b){
- Matrix c(a.r,a.c);
- for(int i=0; i<a.r; ++i){
- *(c.wektor[i])=*(a.wektor[i])-*(b.wektor[i]);
- }
- return c;
- }
- Matrix operator*(Matrix &a,Matrix &b){
- Matrix d(b.r,a.c);
- for(int i=0; i < a.c; ++i)
- {
- for(int j=0; j < b.r; ++j)
- {
- for(int k=0; k < a.c; ++k)
- {
- d[i][j]+=a[i][k]*b[k][j];
- }
- }
- }
- return d;
- }
- Matrix operator*(const int &a, const Matrix &b){
- Matrix d(b.r,b.c);
- for(int i=0; i<b.r; ++i)
- *(d.wektor[i])=*(b.wektor[i])*a;
- return d;
- }
- Matrix& Matrix::operator+=(const Matrix &a){
- for(int i=0; i<a.r; ++i)
- *(wektor[i])+=*(a.wektor[i]);
- return *this;
- }
- Matrix& Matrix::operator-=(const Matrix &a){
- for(int i=0; i<a.r; ++i)
- *(wektor[i])-=*(a.wektor[i]);
- return *this;
- }
- Matrix& Matrix::operator*=(const int &a){
- for(int i=0; i<r; ++i)
- *(wektor[i])*=a;
- return *this;
- }
- bool operator==(const Matrix &a, const Matrix &b){
- if(a.r!=b.r || a.c!=b.c)
- return false;
- for(int i=0; i<a.r; ++i){
- if(a.wektor[i]!=b.wektor[i])
- return false;
- else
- return true;
- }
- }
- bool operator!=(const Matrix &a, const Matrix &b){
- return(!(a==b));
- }
- std::ostream& operator<< (std::ostream& os, Matrix &a){
- for(int i = 0; i < a.r; i++)
- os << *(a.wektor[i]) << std::endl;
- return os;
- }
- std::istream& operator>>(std::istream& is, Matrix &a){
- for(int i=0; i<a.r; i++)
- is >> *(a.wektor[i]);
- return is;
- }
- Matrix& Matrix::operator=(const Matrix &a){
- if(*this==a)
- return *this;
- if(r!=a.r || c!=a.c)
- std::cout << "nie mozna przypisywac macierzy o roznych rozmiarach" <<std::endl;
- for(int i=0; i<r; i++)
- *(wektor[i])=*(a.wektor[i]);
- r=a.r;
- c=a.c;
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement