Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <fstream>
- #include <typeinfo>
- using namespace std;
- class Exception : public std::exception
- {
- protected:
- char* str;
- public:
- Exception(const char* s)
- {
- str = new char[strlen(s) + 1];
- if (str != 0)
- {
- strcpy_s(str, strlen(s) + 1, s);
- }
- }
- Exception(const Exception& V)
- {
- str = new char[strlen(V.str) + 1];
- if (str != 0)
- {
- strcpy_s(str, strlen(V.str) + 1, V.str);
- }
- }
- ~Exception()
- {
- if (str != nullptr)
- delete[] str;
- }
- virtual void print() = 0;
- };
- class IndexOutOfBounds : public Exception
- {
- private:
- int lines;
- int columns;
- int index;
- int index2;
- public:
- IndexOutOfBounds(int Lines, int Columns, int Index, int Index2) : Exception("Index is out of bounds")
- {
- lines = Lines;
- columns = Columns;
- index = Index;
- index2 = Index2;
- }
- int getIndex() { return index; }
- int getIndex2() { return index2; }
- int getLines() { return lines; }
- int getColumns() { return columns; }
- void print()
- {
- cout << "Exception:" << str << " \nNumber of rows of the matrix:" << lines << " ,number of matrix columns:" << columns
- << "\nrequested indexes:" << "[" << index << ']' << "[" << index2 << "]\n";
- }
- ~IndexOutOfBounds() {};
- };
- //неправльные размеры матриц
- class WrongDimensionsException : public Exception
- {
- private:
- int lines;
- int columns;
- int lines2;
- int columns2;
- public:
- WrongDimensionsException(int Lines, int Columns, int Lines2, int Columns2) : Exception("Wrong Dimensions of Matrix")
- {
- lines = Lines;
- columns = Columns;
- lines2 = Lines2;
- columns2 = Columns2;
- }
- WrongDimensionsException(const char* s) : Exception(s) { }
- int getLines() { return lines; }
- int getLines2() { return lines2; }
- int getColumns() { return columns; }
- int getColumns2() { return columns2; }
- ~WrongDimensionsException() {};
- virtual void print()
- {
- cout << "Exception:" << str << '\n' << "Size1:" << lines << "x" << columns << '\n' << "Size2:" << lines2 << "x" << columns2 << '\n';
- }
- };
- class MemoryErrorI : public Exception
- {
- public:
- MemoryErrorI() : Exception("memory allocation error") { }
- void print()
- {
- cout << "Exception:" << str << '\n';
- }
- };
- class WrongSizeException : public WrongDimensionsException
- {
- private:
- int lines;
- int columns;
- public:
- WrongSizeException(int Lines, int Columns) : WrongDimensionsException("Non-positive size of matrix")
- {
- lines = Lines;
- columns = Columns;
- }
- int getLines() { return lines; }
- int getColumns() { return columns; }
- ~WrongSizeException() {};
- void print()
- {
- cout << "Exception:" << str << "\nYou are trying to create a size matrix:" << lines << "x" << columns << '\n';
- }
- };
- template<class T>
- class BaseMatrix
- {
- protected:
- T** arr;
- int lines;
- int columns;
- public:
- //конструктор
- BaseMatrix(int Lines = 1, int Columns = 1)
- {
- if (Lines <= 0 || Columns <= 0) throw WrongSizeException(Lines, Columns);
- lines = Lines;
- columns = Columns;
- arr = new T*[columns];
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- }
- }
- //конструктор копий
- BaseMatrix(BaseMatrix <T> & V)
- {
- if (V.arr == nullptr) return;
- lines = V.lines;
- columns = V.columns;
- arr = new T*[V.columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- if (arr[i] == 0) throw MemoryErrorI();
- for (int j = 0; j < lines; ++j)
- {
- arr[i][j] = V.arr[i][j];
- }
- }
- }
- //конструктор принимающий объект типа ifstream
- BaseMatrix(ifstream& in)
- {
- in >> columns;
- in >> lines;
- arr = new T*[columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- for (int j = 0; j < lines; ++j)
- {
- in >> arr[i][j];
- }
- }
- }
- //метод чтения файла
- BaseMatrix readm(ifstream& in)
- {
- in >> columns;
- in >> lines;
- arr = new T*[columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- if (arr[i] == 0) throw MemoryErrorI();
- for (int j = 0; j < lines; ++j)
- {
- in >> arr[i][j];
- }
- }
- return *this;
- }
- //оператор =
- BaseMatrix <T> operator=(BaseMatrix <T> & V)
- {
- if (this == &V) return *this;
- if (V.arr != nullptr)
- {
- lines = V.lines;
- columns = V.columns;
- arr = new T*[V.columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- if (arr[i] == 0) throw MemoryErrorI();
- for (int j = 0; j < lines; ++j)
- {
- arr[i][j] = V.arr[i][j];
- }
- }
- return *this;
- }
- }
- //оператор обращения к элементам матрицы
- T& operator()(int index1, int index2)
- {
- if (index1 < 0 || index2 < 0 || index1 >= columns || index2 >= lines)
- throw IndexOutOfBounds(lines, columns, index1, index2);
- return arr[index1][index2];
- }
- //функция возвращающая вектор, каждая компонента которого содержит сумму элементов из соответствующей строки матрицы.
- BaseMatrix<T> sum_lines()
- {
- if (arr != nullptr)
- {
- BaseMatrix <T> Rez;
- Rez.lines = 1;
- Rez.columns = columns;
- Rez.arr = new T*[columns];
- if (Rez.arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- Rez.arr[i] = new T[lines];
- if (Rez.arr == 0) throw MemoryErrorI();
- int sum = 0;
- for (int j = 0; j < lines; ++j)
- {
- sum += arr[i][j];
- }
- Rez.arr[i][0] = sum;
- }
- return Rez;
- }
- }
- //создание матрицы m на n (m и n - случайные числа от 2 до 21) и заполнение матрицы случайными числами
- void random_matrix()
- {
- columns = (rand() % 20) + 1;
- lines = (rand() % 20) + 1;
- arr = new T*[columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- if (arr[i] == 0) throw MemoryErrorI();
- for (int j = 0; j < lines; ++j)
- {
- T x = rand();
- arr[i][j] = (x % 201);
- }
- }
- }
- //заполнение матрицы с заданными размерами случайными числами
- void fill_matrix()
- {
- arr = new T*[columns];
- if (arr == 0) throw MemoryErrorI();
- for (int i = 0; i < columns; ++i)
- {
- arr[i] = new T[lines];
- if (arr[i] == 0) throw MemoryErrorI();
- for (int j = 0; j < lines; ++j)
- {
- T x = rand();
- arr[i][j] = (x % 201);
- }
- }
- }
- void print()
- {
- for (int i = 0; i < columns; ++i)
- {
- for (int j = 0; j < lines; ++j)
- {
- cout << arr[i][j] << " ";
- }
- cout << '\n';
- }
- }
- ~BaseMatrix()
- {
- if (arr != nullptr)
- {
- for (int i = 0; i < columns; ++i)
- delete[] arr[i];
- delete[] arr;
- }
- }
- template<class T>
- friend ostream& operator <<(ostream& ustream, BaseMatrix <T> V);
- template<class T>
- friend istream& operator >>(istream& ustream, BaseMatrix <T> &V);
- };
- template<class T>
- ostream& operator <<(ostream& ustream, BaseMatrix <T> V)
- {
- if (typeid(ustream).name() == typeid(ofstream).name())
- {
- ustream << V.columns << " " << V.lines << '\n';
- for (int i = 0; i < V.columns; ++i)
- {
- for (int j = 0; j < V.lines; ++j)
- {
- ustream << V.arr[i][j] << " ";
- }
- ustream << '\n';
- }
- return ustream;
- }
- else
- {
- for (int i = 0; i < V.columns; ++i)
- {
- for (int j = 0; j < V.lines; ++j)
- {
- ustream << V.arr[i][j] << " ";
- }
- ustream << '\n';
- }
- return ustream;
- }
- }
- template<class T>
- istream& operator >>(istream& ustream, BaseMatrix <T> &V)
- {
- if (typeid(ustream).name() == typeid(ifstream).name())
- {
- ustream >> V.columns >> V.lines;
- }
- for (int i = 0; i < V.columns; ++i)
- {
- for (int j = 0; j < V.lines; ++j)
- {
- ustream >> V.arr[i][j];
- }
- }
- return ustream;
- }
- int main()
- {
- BaseMatrix <int> A(5, 5);
- A.fill_matrix();
- A.print();
- cout << '\n';
- A.sum_lines().print();
- cout << '\n';
- try
- {
- BaseMatrix <int> B;
- B.fill_matrix();
- cout << B(-1, -1);
- }
- catch (IndexOutOfBounds n)
- {
- cout << "an error of going out of bounds was found:\n";
- n.print();
- }
- catch (WrongSizeException k)
- {
- cout << "Wrong Size Exception\n";
- k.print();
- }
- catch (...)
- {
- cout << "\nSomething has happened";
- }
- try
- {
- BaseMatrix <int> C(-100, 1);
- C.fill_matrix();
- C.print();
- }
- catch (IndexOutOfBounds n)
- {
- cout << "\nAn error of going out of bounds was found:\n";
- n.print();
- }
- catch (WrongSizeException k)
- {
- cout << "\nWrong Size Exception\n";
- k.print();
- }
- catch (...)
- {
- cout << "\nSomething has happened\n";
- }
- ofstream fout;
- fout.open("in.txt");
- if (fout.is_open())
- {
- fout << A;
- fout.close();
- }
- ifstream fin;
- fin.open("a.txt");
- if (fin.is_open())
- {
- try
- {
- BaseMatrix <int> K = fin;
- K.print();
- }
- catch (...)
- {
- cout << "\nSomething has happened\n";
- }
- }
- cout << '\n';
- ifstream in;
- in.open("in.txt");
- if (in.is_open())
- {
- try
- {
- BaseMatrix <int> U = in;
- U.print();
- }
- catch (...)
- {
- cout << "\nSomething has happened\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement