lab 1.3 main.cpp

← разместить еще код

Расшаренный код:

#include "Header.h"
#include <iostream>
using namespace std;

int main() {
	cout << "This program can create matrices and perform actions on them.\n";
	cout << "The program was developed by a student of the group 1.KH201.8a Stepanov Danil.\n";
	cout << "Enter matrix size (Use Enter to separate numbers): ";
	int row_mat, col_mat;
	cin >> row_mat >> col_mat;
	
	// створення матрицi з визначенною кiлькiстю рядкiв та стовбцiв
	
	Matrix FirstMatrix(row_mat, col_mat);
	cout << "Now the matrix looks like this:\n";
	cout << FirstMatrix;
	// заповнення матрицi
	cout << "Fill the matrix automatically? (1-Yes, 0-No): ";
	bool fill_matrix;
	cin >> fill_matrix;
	// випадкове заповнення матрицi
	if (fill_matrix)
		FirstMatrix.FillRandom();
	else
		cin >> FirstMatrix;

	cout << "The resulting matrix:\n";
	cout << FirstMatrix;

	// ***********************************
	//           ДРУГА МАТРИЦЯ
	// ***********************************
	
	cout << "Creating a second matrix.\n";
	cout << "Enter matrix size (Use Enter to separate numbers): ";
	cin >> row_mat >> col_mat;
	// cтворення новоi матрицi
	Matrix SecondMatrix(row_mat, col_mat);

	cout << "Now the matrix looks like this:\n";
	cout << SecondMatrix;

	cout << "Fill the matrix automatically? (1-Yes, 0-No): ";
	cin >> fill_matrix;
	// заповнення матрицi
	if (fill_matrix)
		SecondMatrix.FillRandom();
	else
		cin >> SecondMatrix;

	cout << "The resulting matrix:\n";
	cout << SecondMatrix;
	// ***********************************
	//         РОБОТА  З  МАТРИЦЯМИ
	// ***********************************
	int tempvar;
	do {
		cout << "What should the program do?\n1) Find the sum of matrices.\n";
		cout << "2) Find the difference of matrices.\n3) Find the product of matrices.\n";
		cout << "4) Replace elements with absolute values.\n";
		cout << "5) Print the first matrix.\n6) Print the second matrix.\n";
		cout << "7) Print element.\n0) Exit the program.\n";
		cin >> tempvar;
		cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
		if (tempvar == 0)
			break;
		Matrix ResultMatrix(row_mat, col_mat);
		Matrix ResultMatrix2(FirstMatrix.GetRows(), SecondMatrix.GetColumns());
		// блок обробки вибору користувача
		switch (tempvar) {
		case 1:
			// далi блок try{}catch(){} буде обробляти можливi помилки
			try {
				ResultMatrix = FirstMatrix + SecondMatrix;
			}
			catch (MyException &ex) {
				cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n      An error has occurred\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" << ex.what() << endl;
				ex.GetDataState();
				cout << "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
			}
			cout << "The resulting matrix:\n";
			cout << ResultMatrix << endl;
			break;
		case 2:
			try {
				ResultMatrix = FirstMatrix - SecondMatrix;
			}
			catch (MyException &ex) {
				cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n      An error has occurred\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" << ex.what() << endl;
				ex.GetDataState();
				cout << "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
			}
			cout << "The resulting matrix:\n";
			cout << ResultMatrix << endl;
			break;
		case 3:
			try {
				ResultMatrix2 = FirstMatrix * SecondMatrix;
			}
			catch (MyException &ex) {
				cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n      An error has occurred\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" << ex.what() << endl;
				ex.GetDataState();
				cout << "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
			}
			cout << "The resulting matrix:\n";
			cout << ResultMatrix2 << endl;
			break;
		case 4:
			FirstMatrix.Abs();
			cout << "The resulting matrix:\n";
			cout << FirstMatrix << endl;
			break;
		case 5:
			cout << "The first matrix:\n";
			cout << FirstMatrix << endl;
			break;
		case 6:
			cout << "The second matrix:\n";
			cout << SecondMatrix << endl;
			break;
		case 7:
			cout << "Enter the number of Matrix (1 or 2): ";
			int tempch;
			cin >> tempch;
			int i, j;
			cout << "Enter position(i,j): ";
			cin >> i >> j;
			cout << "You element: ";
			if (tempch == 1)
				cout << FirstMatrix(i, j) << endl;
			else
				cout << SecondMatrix(i, j) << endl;
		}
	} while (true);
	// завершення роботи програми
	cout << "Closing the program..." << endl;
}