DZ4
/
project2.cpp
170 строк · 4.8 Кб
1//Создайте шаблонный класс матрица. Необходимо реализовать динамическое выделение памяти, очистку памяти, заполнение матрицы с клавиатуры,
2// заполнение случайными значениями, отображение матрицы, арифметические операции с помощью перегруженных операторов (+, -
3//*, / ), поиск максимального и минимального элемента.
4#include <iostream>
5#include <iomanip>
6using namespace std;
7template <typename T>
8class Matrix {
9private:
10T** data;
11int rows;
12int cols;
13
14public:
15Matrix(int rows, int cols) : rows(rows), cols(cols) {
16data = new T * [rows];
17for (int i = 0; i < rows; ++i) {
18data[i] = new T[cols];
19}
20}
21
22~Matrix() {
23for (int i = 0; i < rows; ++i) {
24delete[] data[i];
25}
26delete[] data;
27}
28
29void fillFromKeyboard() {
30for (int i = 0; i < rows; ++i) {
31for (int j = 0; j < cols; ++j) {
32cout << "Enter element [" << i << "][" << j << "]: ";
33cin >> data[i][j];
34}
35}
36}
37
38void fillRandom(T min, T max) {
39for (int i = 0; i < rows; ++i) {
40for (int j = 0; j < cols; ++j) {
41data[i][j] = rand() % (max - min + 1) + min;
42}
43}
44}
45
46void display() {
47for (int i = 0; i < rows; ++i) {
48for (int j = 0; j < cols; ++j) {
49cout << setw(4) << data[i][j];
50}
51cout << endl;
52}
53}
54
55Matrix operator+(const Matrix& other) {
56Matrix result(rows, cols);
57for (int i = 0; i < rows; ++i) {
58for (int j = 0; j < cols; ++j) {
59result.data[i][j] = data[i][j] + other.data[i][j];
60}
61}
62return result;
63}
64
65Matrix operator-(const Matrix& other) {
66Matrix result(rows, cols);
67for (int i = 0; i < rows; ++i) {
68for (int j = 0; j < cols; ++j) {
69result.data[i][j] = data[i][j] - other.data[i][j];
70}
71}
72return result;
73}
74
75Matrix operator*(const Matrix& other) {
76Matrix result(rows, other.cols);
77for (int i = 0; i < rows; ++i) {
78for (int j = 0; j < other.cols; ++j) {
79result.data[i][j] = 0;
80for (int k = 0; k < cols; ++k) {
81result.data[i][j] += data[i][k] * other.data[k][j];
82}
83}
84}
85return result;
86}
87
88Matrix operator/(const Matrix& other) {
89// Just for demonstration, actual division is more complex
90Matrix result(rows, cols);
91for (int i = 0; i < rows; ++i) {
92for (int j = 0; j < cols; ++j) {
93result.data[i][j] = data[i][j] / other.data[i][j];
94}
95}
96return result;
97}
98
99T findMax() {
100T max = data[0][0];
101for (int i = 0; i < rows; ++i) {
102for (int j = 0; j < cols; ++j) {
103if (data[i][j] > max) {
104max = data[i][j];
105}
106}
107}
108return max;
109}
110
111T findMin() {
112T min = data[0][0];
113for (int i = 0; i < rows; ++i) {
114for (int j = 0; j < cols; ++j) {
115if (data[i][j] < min) {
116min = data[i][j];
117}
118}
119}
120return min;
121}
122};
123
124int main() {
125Matrix<int> mat1(2, 2);
126Matrix<int> mat2(2, 2);
127
128mat1.fillRandom(1, 10);
129mat2.fillRandom(1, 10);
130
131cout << "Matrix 1:" << endl;
132mat1.display();
133cout << endl;
134
135cout << "Matrix 2:" << endl;
136mat2.display();
137cout << endl;
138
139Matrix<int> sum = mat1 + mat2;
140cout << "Sum of matrices:" << endl;
141sum.display();
142cout << endl;
143
144Matrix<int> difference = mat1 - mat2;
145cout << "Difference of matrices:" << endl;
146difference.display();
147cout << endl;
148
149Matrix<int> product = mat1 * mat2;
150cout << "Product of matrices:" << endl;
151product.display();
152cout << endl;
153
154Matrix<int> quotient = mat1 / mat2;
155cout << "Quotient of matrices:" << endl;
156quotient.display();
157cout << endl;
158
159int max1 = mat1.findMax();
160int max2 = mat2.findMax();
161cout << "Max element in Matrix 1: " << max1 << endl;
162cout << "Max element in Matrix 2: " << max2 << endl;
163
164int min1 = mat1.findMin();
165int min2 = mat2.findMin();
166cout << "Min element in Matrix 1: " << min1 << endl;
167cout << "Min element in Matrix 2: " << min2 << endl;
168
169return 0;
170}