Добавил:
Tushkan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы (2) / laba13 / sclass_methods
.cpp// Создание простого класса
// sclass.h - Объявление класса
#include "iostream.h"
//#define NMAX 50;
enum {NMAX = 50};
class Matrix
{
private:
enum {NO_ERROR,BORDERS_OUT_OF_RANGE,FILE_READ_ERROR,FILE_OPEN_ERROR,MATRIX_DESTROYED};
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 j); // Индексное выражение
int error; //признак ошибки
int MatrixCheck (); //проверка параметров матрицы
const char* Error_Message (); //сообщение об ошибке
};
// Создание простого класса
// sclass_methods.h - Процедуры и конструкторы
class IndexException {
public:
char def_except[80];
IndexException () {*def_except=0;}
IndexException (char *s) {strcpy (def_except,s);}
};
class MathException{
public:
char math_except[80];
MathException() {*math_except=0;}
MathException(char *s) {strcpy (math_except,s);}
};
// Деструктор:
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;
}
error=NO_ERROR;
}
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;
error=NO_ERROR; // Умолчание
}
// Конструктор копирования:
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))
throw MathException ("Matrixes can't be summed up");
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))
throw MathException ("Matrixes can't be subtracted");
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;
return *this;
}
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))
cout<<("Matrixes can't be subtracted");
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 || i<0 || j>=row || j<0)
throw IndexException ("There's no such elem");
else
return (*matr[i]);
}
int Matrix::MatrixCheck() {
if ((col==-1)||(row==-1)) {
cout<<"empty!!\n";
error=MATRIX_DESTROYED;
return 0;
}
if ((col<1)||(row<1)) error= BORDERS_OUT_OF_RANGE;
return 1;
}
const char* Matrix::Error_Message(){
if (error==NO_ERROR)
return "No Errors Found!\n";
else if (error==BORDERS_OUT_OF_RANGE)
return "Matrix Borders are Wrong!\n";
else if (error==MATRIX_DESTROYED)
return "Matrix was destroyed or wasn't properly initialized\n";
}