#include <iostream>
#include <conio.h>
using namespace std;
class Vect{
static int count;
int dim;
double *coor;
public:
int num;
Vect();
Vect(int n, double coor1[]);
Vect(Vect& x);
~Vect();
void print();
double getcoor(int i)
{
return coor[i];
}
int getdim()
{
return dim;
}
void operator=(Vect &b);
Vect operator+(Vect &b);
Vect operator-(Vect &b);
Vect operator-();
friend Vect operator*(double n, Vect &v);
friend double operator*(Vect &a, Vect &b);
friend void setvect(Vect& a, double* b);
};
class Matr
{
double *a;
int dim;
static int count;
public:
int num;
Matr();
Matr(int n);
Matr(int n, double *x);
Matr(Matr &x);
~Matr();
void print();
void operator= (Matr &r);
int getnum(int i, int j);
friend Matr operator+ (Matr &a,Matr &b);
friend Matr operator-(Matr& a, Matr& b);
Matr &operator-();
friend Matr &operator*(double k, Matr &r);
friend Vect &operator*(Matr&r, Vect& v);
friend Matr operator*(Matr& m1, Matr& m2);
};
Vect operator*(double n, Vect &v);
Vect &operator*(Matr&r, Vect& v);
double operator*(Vect &a, Vect &b);
Vect &operator*(Matr &r, Vect &v);
void setvect(Vect& a, double *b);
int Vect::count = 0;
int Matr::count = 0;
int main()
{
double b[] = { 15, 2, 3 };
Vect a(3, b);
a.print();
Vect c(a);
c.print();
Vect d = a + c;
d.print();
_getch();
return 0;
}
Matr::Matr()
{
count++;
num = count;
dim = 0;
a = new double [0];
cout << "Matrix " << num << " has been created by Matr::Matr()" << endl;
}
Matr::Matr(int n)
{
count++;
num = count;
dim = n;
a = new double[dim*dim];
for (int i = 0; i < dim*dim; i++)
{
if ((i%(dim + 1)) == 0) a[i] = 1;
else a[i] = 0;
}
cout << "Matrix " << num << " has been created by Matr::Matr(int n)" << endl;
}
Matr::Matr(int n, double *x)
{
count++;
num = count;
dim = n;
a = new double[dim*dim];
for (int i = 0; i < dim*dim;i++)
{
a[i] = x[i];
}
cout << "Matrix " << num << " has been created by Matr::Matr(int n,double *x)" << endl;
}
Matr::Matr(Matr &x)
{
count++;
num = count;
dim = x.dim;
a = new double[dim*dim];
for (int i = 0; i < dim*dim; i++)
{
a[i] = x.a[i];
}
cout << "Matrix " << num << " has been created by Matr::Matr(Matr& x)" << endl;
}
Matr::~Matr()
{
count--;
delete a;
cout << "Matrix " << num << " has been deleted by ~Matr::Matr()" << endl;
}
void Matr::print()
{
for (int i = 0; i < dim*dim; i++)
{
if ((i % dim) == dim-1) cout << a[i] << endl;
else cout << a[i] << " ";
}
}
void Matr::operator=(Matr &r)
{
dim = r.dim;
for (int i = 0; i < dim*dim; i++)
{
a[i] = r.a[i];
}
}
int Matr::getnum(int i, int j)
{
int k = i*dim + j;
return k;
}
Matr operator+(Matr &a,Matr &b)
{
Matr rs(a);
if (a.dim != b.dim) cout << "Cannot + matrix";
else
{
for (int i = 0; i < rs.dim*rs.dim; i++)
{
rs.a[i] += b.a[i];
}
cout << "Matrix " << a.num << " has been plused Matrix " << b.num << endl;
return rs;
}
}
Matr operator-(Matr &a, Matr &b)
{
Matr rs(a);
if (a.dim != b.dim) cout << "Cannot - matrix";
else
{
for (int i = 0; i < rs.dim*rs.dim; i++)
{
rs.a[i] -= b.a[i];
}
cout << "Matrix " << a.num << " has been deducted Matrix "<<b.num << endl;
return a;
}
}
Matr &Matr::operator-()
{
Matr rs = *this;
for (int i = 0; i < dim*dim; i++)
{
rs.a[i] = -a[i];
}
cout << "Matrix " << num << " has been changed sign" << endl;
return rs;
}
Vect &operator*(Matr &r, Vect &v)
{
if (v.getdim() != r.dim) cout << "Cannot * vector on matrix";
else
{
double* tmp = new double[r.dim];
for (int i = 0; i < r.dim; i++)
{
tmp[i] = v.getcoor(i);
}
double* tmp1 = new double[r.dim];
int j(0),k(0);
for (int i = 0; i < r.dim; i++)
{
j = 0;
tmp1[i] = 0;
while (j <r.dim)
{
tmp1[i] = tmp1[i] + (r.a[k] * tmp[j]);
j++;
k++;
}
}
Vect *rs=new Vect(r.dim, tmp1);
cout << "Vector " << v.num << " has been multyplied on Matrix " << r.num << endl;
return *rs;
}
}
Matr operator*(Matr &m1, Matr &m2)
{
Matr rs(m1);
if (m1.dim != m2.dim) cout << "Cannot * matrix";
else
{
for (int i = 0; i < m1.dim; i++)
{
for (int j = 0; j < m1.dim;j++)
{
float sum = 0;
for (int k = 0; k < m1.dim; k++)
{
sum += m1.a[m1.getnum(k, j)] * m2.a[m2.getnum(i, k)];
}
rs.a[m1.getnum(i, j)] = sum;
}
}
cout << "Matrix " << m1.num << " has been multyplied on Matrix " << m2.num << endl;
return rs;
}
}
Matr &operator*(double k, Matr &r)
{
Matr rs(r);
for (int i = 0; i < r.dim*r.dim; i++)
rs.a[i] *= k;
cout << "Matrix " << r.num << " has been multyplied on "<< k << endl;
return rs;
}
Vect::Vect()
{
count++;
num = count;
dim = 0;
coor = new double [0];
cout << "Vector " << num << " has been created by Vect()" << endl;
};
Vect::Vect(int n, double coor1[])
{
count++;
num = count;
dim = n;
coor = new double[n];
for (int i = 0; i < dim; i++)
{
coor[i] = coor1[i];
}
cout << "Vector " << num << " has been created by Vect(int n,double coor1[])" << endl;
}
Vect::Vect(Vect &x)
{
count++;
num = count;
dim = x.dim;
coor = new double[dim];
for (int i = 0; i < dim; i++)
{
coor[i] = x.coor[i];
}
cout << "Vector " << num << " has been created by Vect(Vect &x)" << endl;
}
Vect::~Vect()
{
count--;
delete coor;
cout << "Vector " << num << " has been deleted by ~Vect()" << endl;
}
void Vect::print()
{
cout << "Vector "<<num<< " Coordinats: ";
for (int i = 0; i < dim; i++)
{
cout <<coor[i] << " ";
}
cout << endl;
}
void Vect::operator=(Vect &b)
{
dim = b.dim;
for (int i = 0; i < dim; i++)
{
coor[i] = b.coor[i];
}
cout << "Vector " << num << " has been assigned Vector " << b.num << endl;
}
Vect Vect::operator+(Vect &b)
{
if(dim != b.dim) cout << "Cannot + vectors";
else
{
Vect a=*this;
for (int i = 0; i < dim; i++)
{
a.coor[i] += coor[i];
}
cout << "Vector " << num << " has been plussed Vector " << b.num << endl;
return a;
}
}
Vect Vect::operator-(Vect &b)
{
if (dim != b.dim) cout << "Cannot - vector";
else
{
Vect a=*this;
for (int i = 0; i < dim; i++)
{
a.coor[i] -= b.coor[i];
}
cout << "Vector " << num << " has been deducted Vector" << b.num << endl;
return a;
}
}
Vect Vect::operator-()
{
Vect a = *this;
for (int i = 0; i < dim; i++)
{
a.coor[i] = -coor[i];
}
cout << "Vector " << num << " has been changed sign" << endl;
return a;
}
Vect operator*(double n, Vect &v)
{
Vect rs(v);
for (int i = 0; i < v.dim; i++)
{
rs.coor[i] *= n;
}
return rs;
cout << "Vector " << v.num << " has been mulltyplied on " << n << endl;
}
double operator*(Vect& a, Vect& b)
{
if (a.dim != b.dim)
{
cout << "Cannot * vectors";
return 0;
}
else
{
double r = 0;
for (int i = 0; i < a.dim; i++)
{
r= r+a.coor[i]*b.coor[i];
}
return r;
}
}
void setvect(Vect& a, double *b)
{
int a1 = sizeof(b) / sizeof(int);
if (a.dim !=a1) cout << "Cannot set vector" << endl;
else
{
for (int i = 0; i < a.dim; i++)
{
a.coor[i] = b[i];
}
}
}