Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
6
Добавлен:
28.06.2014
Размер:
3.75 Кб
Скачать
// Создание простого класса
// sclass.h - Объявление класса
#include "iostream.h"
class Matrix
{
private:
  enum {NMAX = 50};
  int matr[NMAX][NMAX];
  int col, row;
public:
  // Конструкторы:
  Matrix(); // Умолчание
  Matrix(int prow, int pcol); // Умолчание
  Matrix(const Matrix&, int number = 0);           // Копирование
  Matrix(int);                           // Преобразование
  // Деструктор:
  ~Matrix();
  // Методы:
  void show() const;                               // Вывод на экран
  operator double();

  //Операторы
    // Операторы:
  friend Matrix operator + (const Matrix &M, const Matrix &N);
  friend Matrix operator - (const Matrix &M, const Matrix &N);
  int operator += (int ch);
  Matrix operator =  (const Matrix &M);
  int operator == (const Matrix &M);
  int& operator () (const int i, const int g);                              // Индексное выражение

};

// Создание простого класса
// sclass_methods.h - Процедуры и конструкторы

// Деструктор:
Matrix::~Matrix() { cout << "Destructed\n"; }

Matrix::Matrix(int prow, int pcol)
{
  int i, j;
  if ((prow > 0) && (pcol > 0)) {
  col = pcol; row = prow;
  for (i=0; i < prow; i++)
    for (j=0; j < pcol; j++)
      matr[i][j] = 0;
}
}
Matrix::Matrix()
{
  int i, j;
  col = 3; row = 3;
  for (i=0; i < row; i++)
    for (j=0; j < col; j++)
      matr[i][j] = 0;
}

// Конструктор копирования:
Matrix::Matrix(const Matrix &m, int number)
{
  int i, j;
  col = m.col; row = m.row;
  for (i=0; i < row; i++)
    for (j=0; j < col; j++)
      matr[i][j] = m.matr[i][j] + number;
}

// Конструктор преобразования:
Matrix::Matrix( int ch)
{
  int i;
  if (row < col) 
    for (i=0; i < row; i++)
      matr[i][i] = ch;
  else 
    for (i=0; i < col; i++)
      matr[i][i] = ch;
}

// Вывод матрицы на экран:
void Matrix::show() const
{
  int i, j;
  for(i=0; i < row; i++)
  {
    for (j=0; j < col; j++)
      printf("%i ", matr[i][j]);
    printf("\n");
  }
}

// Преобразование типа double:
Matrix::operator double()
{
  int i, j, sum = 0;
  double mid;
  for (i=0; i < row; i++)
    for (j=0; j < col; j++)
      sum += matr[i][j];
  mid = (double)sum / (row * col);
  return mid;
}


//Операторы 
Matrix operator + (const Matrix &M, const Matrix &N){
  int i, j;
  if ((N.col != M.col) || (N.row != M.row))
  {
    cout << "Raznye razmernosti matrits\n";
    return 0;
  }
  Matrix sum(M.row, M.col);
  for (i=0; i < M.row; i++)
    for (j=0; j < M.col; j++)
      sum.matr[i][j] = N.matr[i][j] + M.matr[i][j];
  return sum;
}


Matrix operator - (const Matrix &M, const Matrix &N){


  int i, j;
  if ((N.col != M.col) || (N.row != M.row))
  {
    cout << "Raznye razmernosti matrits\n";
    return 0;
  }
  Matrix sum(M.row, M.col);
  for (i=0; i < M.row; i++)
    for (j=0; j < M.col; j++)
      sum.matr[i][j] = M.matr[i][j] - N.matr[i][j];
  return sum;
}

int Matrix::operator += (int ch){
  int i, j;
  for (i=0; i < row; i++)
    for (j=0; j < col; j++)
      matr[i][j] += ch;
}

Matrix Matrix::operator = (const Matrix &M)
{
  int i, j;
  for (i=0; i < row; i++)
    for (j=0; j < col; j++)
      matr[i][j] = M.matr[i][j];
  return *this;
}

int Matrix::operator == (const Matrix &M)
{
  int is_equal = 1, i = 0, j = 0;
  if ((M.col != col) || (M.row != row)) return 0;
  while (is_equal && (i < col))
  {
    while (is_equal && (j < row))
    {
      is_equal = (matr[i][j] == M.matr[i][j]);
      j++;
    }
    j = 0;
    i++;
  }
  return is_equal;
}

int& Matrix:: operator () (const int i, const int j)
{
  if ((i<col) && (j < col) && (i>=0) && (j>=0)) {
 return (matr[i][j]);
 }
}
Соседние файлы в папке laba11