# Разработка и реализация шаблонного класса матриц, страница 2

m = y;

n = x;

}

else

{

m = x;

n = y;

}

for (i = 0; i < rows; i++)

for (j = 0; j < cols; j++)

mat[i][j] = rand() % (m-n+1) + n;

}

template <class value_type> Matrix<value_type> Matrix<value_type> :: Transponir()

size_type i, j, x = rows, y = cols;

Tmat tm = Tmat (cols, Tvec(rows));

for (i = 0; i < cols; i++)

for (j = 0; j < rows; j++)

tm[i][j] = mat[j][i];

mat = tm;

rows = y;

cols = x;

return *this;

}

Lab6.cpp:

#include "stdafx.h"

#include "Matrix_template.h"

#include <iostream>

ostream& operator<< (ostream& ost, const CStudent3& s)

{

cout << s.Getmark() << " " << s.Getnkol();

return ost;

}

template <class value_type> ostream& operator<< (ostream& ost, const Matrix<value_type>& m)

size_type i, j;

for (i = 0; i < m.GetRows(); i++)

{

for (j = 0; j < m.GetCols(); j++)

{

cout << m[i][j] << " ";

}

cout << endl;

}

return ost;

}

template <class value_type> istream& operator>> (istream& ist, Matrix<value_type>& m)

size_type i, j;

for (i = 0; i < m.GetRows(); i++)

for (j = 0; j < m.GetCols(); j++)

{

cin >> m[i][j];

}

return ist;

}

template <class value_type> bool operator== (const Matrix<value_type>& m, const Matrix<value_type>& n)

{

size_type i;

bool s = true;

for (i = 0; i < m.GetRows(); i++)

if (m[i] != n[i])

s = false;

return ((m.GetCols() == n.GetCols()) && (m.GetRows() == n.GetRows()) && s);

}

template <class value_type> bool operator != (const Matrix<value_type>& m, const Matrix<value_type>& n)

{

size_type i;

bool s = false;

for (i = 0; i < m.GetRows(); i++)

if (m[i] != n[i])

s = true;

return ((m.GetCols() != n.GetCols()) || (m.GetRows() != n.GetRows()) || s);

}

int _tmain()

{

Matrix<int> m1(6, 3, 4);

Matrix<int> mm(1,6,6);

cout << " Ishodnaya matrica tipa int: " << endl << m1 << endl;

cout << "Transponirovannaya matrica: " << endl << m1.Transponir() << endl;

cout << "Vstavili pered 2-m stolbcom stolbec s elementami ravnumi 2"<< endl <<" i pered 2 strokoi stroku s elementami ravnumi 6" << endl;

m1.insert_row_before(1,mm[0]);

m1.insert_col_before(1, 2);

cout << m1 << endl;

m1.RandomMatrix(5,9);

m1.delete_col(2);

m1.delete_row(2);

cout << "Proizvolnoe zapolnenie matricu elementami ot 5 do 9 i ydalaem 1 stroky i 1 stolbec:" << endl << m1 <<endl;

Matrix<int> m(5 , 5);

m[0][0]=1; m[0][1]=2; m[0][2]=3; m[0][3]=4; m[0][4]=5;

m[1][0]=2; m[1][1]=1; m[1][2]=2; m[1][3]=3; m[1][4]=4;

m[2][0]=3; m[2][1]=2; m[2][2]=1; m[2][3]=2; m[2][4]=3;

m[3][0]=4; m[3][1]=3; m[3][2]=2; m[3][3]=1; m[3][4]=2;

m[4][0]=5; m[4][1]=4; m[4][2]=3; m[4][3]=2; m[4][4]=1;

cout<<"Sozdaem matricu razmera 5x5 i zapolnaem chislami"<<endl<<m<<'\n';

CStudent3 s1;

s1.Setmark (4.6);

s1.Setname ("Ancybor Evhen");

s1.Setnkol (21);

s1.Setnzach ("456589");

Matrix<CStudent3> m2 (5, 1, s1);

cout << "Ishodnaya matrica tipa student:" << endl << m2 << endl;

Matrix<double> m3(4, 4, 1.5), m4(4, 4, 0.7);

cout << "m3: " << endl << m3 << endl << "m4: " << endl << m4 << endl;

cout << "m3+m4" << endl << m3+m4 << endl;

cout << "Izmenili razmer m3" << endl;

m3.resize_matrix(2, 2);

cout << m3 << endl;

Matrix<int> gr(2, 2);

Matrix<int> strok(1,3,7);

cout << "Sozdali nylvyjy matricy:" << endl << gr << endl;

gr.insert_col_before(0, 5);

cout << "Pered prervum stolbcom vstavili stolbec iz 5" << endl << gr << endl;

gr.insert_row_before(0, strok[0]);

cout << "Pered prervoj strokoj vstavili stroky iz 7" << endl << gr << endl;

cin.get();

}

Реализация:

Ishodnaya matrica tipa int:

4 4 4

4 4 4

4 4 4

4 4 4

4 4 4

4 4 4

Transponirovannaya matrica:

4 4 4 4 4 4

4 4 4 4 4 4

4 4 4 4 4 4

Vstavili pered 2-m stolbcom stolbec s elementami ravnumi 2

i pered 2 strokoi stroku s elementami ravnumi 6

4 2 4 4 4 4 4

6 2 6 6 6 6 6

4 2 4 4 4 4 4

4 2 4 4 4 4 4

Proizvolnoe zapolnenie matricu elementami ot 5 do 9 i ydalaem 1 stroky i 1 stolbec:

7 8 9 8 6 8

9 5 6 7 5 5

7 5 5 7 8 6

Sozdaem matricu razmera 5x5 i zapolnaem chislami

1 2 3 4 5

2 1 2 3 4

3 2 1 2 3

4 3 2 1 2

5 4 3 2 1

Ishodnaya matrica tipa student:

4.6 21

4.6 21

4.6 21

4.6 21

4.6 21

m3:

1.5 1.5 1.5 1.5

1.5 1.5 1.5 1.5

1.5 1.5 1.5 1.5

1.5 1.5 1.5 1.5

m4:

0.7 0.7 0.7 0.7

0.7 0.7 0.7 0.7

0.7 0.7 0.7 0.7

0.7 0.7 0.7 0.7

m3+m4

2.2 2.2 2.2 2.2

2.2 2.2 2.2 2.2

2.2 2.2 2.2 2.2

2.2 2.2 2.2 2.2

Izmenili razmer m3

2.2 2.2

2.2 2.2

Sozdali nylvyjy matricy:

0 0

0 0

Pered prervum stolbcom vstavili stolbec iz 5

5 0 0

5 0 0

Pered prervoj strokoj vstavili stroky iz 7

7 7 7

5 0 0

5 0 0