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

Страницы работы

7 страниц (Word-файл)

Содержание работы

МИНИСТЕРСТВО ОБРАЗОВАНИЯ И НАУКИ УКРАИНЫ

Национальный аэрокосмический университет им. Н. Е. Жуковского “ХАИ”

Кафедра 304

Лабораторная Работа №5

По дисциплине «Програмне забезпечення”

Выполнил студентка 325 гр.

Старцева А.В.

Проверил Подоляка А.Н.

Харьков  2014

 «Разработка и реализация шаблонного класса матриц»

Matrix_template:

#include <vector>

#include <time.h>

#include <stdlib.h>

typedef unsigned int size_type;

template <class value_type>

class Matrix

{

public:     

       typedef std::vector<value_type> Tvec;

       typedef std::vector<Tvec> Tmat; 

       explicit Matrix (int row_count, int col_count)

       {

             cols = col_count;

             rows = row_count;

             mat.resize(row_count);

             for (int i=0; i<row_count; i++)

             {

                    mat[i].resize(col_count);

             }

             srand( (unsigned)time( NULL ) );

       }

       Matrix (int row_count, int col_count,  value_type key)

       {

             cols = col_count;

             rows = row_count;

             mat.resize(row_count);

             for (int i=0; i< row_count; i++)

             {

                    mat[i].resize(col_count);

                    for (int j=0; j< col_count; j++)

                    {

                          mat[i][j] = key;

                    }

             }

             srand( (unsigned)time( NULL ) );

       };

       ~Matrix()

       {

       };

       void RandomMatrix (size_type x, size_type y);

    Matrix Transponir ();

       void delete_row (size_type i);

       void delete_col (size_type i);

    Tvec& operator[] (size_type i)

       {

             return mat[i];

       }

    const Tvec& operator[] (size_type i) const

       {

             return mat[i];

       }

       Matrix operator- (const Matrix& m);

       Matrix operator+ (const Matrix& m);

       Matrix operator+ (const value_type n);

       Matrix operator* (const value_type n);

       Matrix operator* (const Matrix& n);

private:

       Tmat mat;

       size_type rows;

       size_type cols;

public:

size_type GetRows()const

{

             return rows;

}

size_type GetCols()const

{

             return cols;

}

bool insert_row_before (size_type i, Tvec& val)

{

       if (i > rows)

       {

             cout << "Index vuhodit za predelu";

             exit(1);

       }

       else

       {

             Tmat::iterator iter = mat.begin()+i;

             mat.insert(iter,val);

             rows++;

       }

       return true;

}

bool insert_col_before (size_type i, value_type val)

{

       size_type j;

       if (i > cols)

       {

              cout << "(Index vuhodit za predelu)";

             exit(1);

       }

       else        

       {

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

             {

                    Tvec::iterator iter = mat[j].begin()+i;

                    mat[j].insert(iter,val);

             }

             cols++;

    }

}

void resize_matrix(int new_row_count, int new_col_count)

{

       cols = new_col_count;

       rows = new_row_count;

       mat.resize(new_col_count);

       for (int i=0; i<new_col_count; i++)

       {

             mat[i].resize(new_row_count);

       }

}

};

template <class value_type> Matrix<value_type> Matrix<value_type>:: operator- (const Matrix& m)

{

       size_type i, j;

       Matrix rez (rows, cols);

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

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

                    mat[i][j] = mat[i][j] - m[i][j];

       return *this;

}

template <class value_type> Matrix<value_type> Matrix<value_type>:: operator+ (const Matrix& m)

{

       size_type i, j;

       Matrix rez (rows, cols);

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

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

                    mat[i][j] = mat[i][j] + m[i][j];

   return *this;

}

template <class value_type> Matrix<value_type> Matrix<value_type>:: operator+ (const value_type n)

{

   size_type i, j;

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

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

                mat[i][j] = mat[i][j] + n;

   return *this;

}

template <class value_type> Matrix<value_type> Matrix<value_type>:: operator* (const value_type n)

{

   size_type i, j;

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

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

                mat[i][j] = mat[i][j] * n;

   return *this;

}

template <class value_type> Matrix<value_type> Matrix<value_type>:: operator* (const Matrix& n)

   size_type i, j, k;

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

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

          {

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

                       mul[i][j] = mat[i][k] * n[k][j] + mul[i][j];

          }

             mat[i,j] = mul[i][j];

   return *this;

}

template <class value_type> void Matrix<value_type> :: delete_col (size_type i)

{

       size_type j;

       if (i > cols)

       {

             cout << "Index vuhodit za predelu"<<endl;

             exit(1);

       }

       else

       {

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

             {

                    Tvec::iterator iter = mat[j].begin()+i;

                    mat[j].erase (iter);

             }

             cols--;

       }

}

template <class value_type> void Matrix<value_type> :: delete_row (size_type i)

{

       if (i > rows)

       {

             cout << "Index vuhodit za predelu"<<endl;

             exit(1);

       }

       else

       {

             Tmat::iterator iter=mat.begin()+i;

             mat.erase(iter);

             rows--;

       }

}

template <class value_type> void Matrix<value_type> :: RandomMatrix (size_type x, size_type y)

{

       size_type i, j, m, n;

       if (y > x)

       {

Похожие материалы

Информация о работе