-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamsonov_Aleksey_201-331_lab5_2.cpp
221 lines (203 loc) · 5.11 KB
/
Samsonov_Aleksey_201-331_lab5_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix {
private:
int rows, columns;
double* data;
public:
Matrix(int rows_, int columns_, const double* arr = nullptr) : rows(rows_), columns(columns_) {
data = new double[rows*columns];
for (int i = 0; i < rows*columns; i++) {
data[i] = arr[i];
}
}
}
~Matrix() {
delete[] data;
}
Matrix* sum(const Matrix* m) {
if (rows != m->rows or columns != m->columns) {
return nullptr;
}
Matrix* result = new Matrix(rows, columns);
for (int i = 0; i < rows*columns; i++) {
result->data[i] = data[i] + m->data[i];
}
return result;
}
Matrix* mult(const Matrix* m) {
if (columns != m->rows) {
return nullptr;
}
Matrix* result = new Matrix(rows, m->columns);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < m->columns; j++) {
result->data[i*columns + j] = 0;
for (int k = 0; k < columns; k++) {
result->data[i*columns + j] += data[i*columns + k] * m->data[k*columns + j];
}
}
}
return result;
}
void input(int rows_, int columns_) {
rows = rows_;
columns = columns_;
delete[] data;
data = new double[rows*columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << "data[" << i << "][" << j << "]: ";
cin >> data[i*columns + j];
}
}
}
void input(int rows_, int columns_, const double* arr) {
rows = rows_;
columns = columns_;
delete[] data;
data = new double[rows*columns];
for (int i = 0; i < rows*columns; i++) {
data[i] = arr[i];
}
}
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << fixed << setprecision(1) << data[i*columns + j];
}
cout << endl;
}
}
int get_columns() {
return columns;
}
int get_rows() {
return rows;
}
double get_elem(int i, int j) {
return data[i*columns + j];
}
};
int main() {
Matrix* matrix = nullptr;
char comands[6][250] = {"Create Matrix", "Create Matrix by array", "Print Matrix", "Sum Matrix", "Multiply Matrix", "Exit" };
Matrix* tmp, *result;
int rows, columns;
int cmd = 0;
int cmds = 6;
bool running = true;
double* arr = nullptr;
while (running) {
for (int i = 0; i < cmds; i++) {
cout << i + 1 << ". " << comands[i] << endl;
}
cout << ">> ";
cin >> cmd;
cout << endl;
if (cmd != 1 and cmd != 6 and matrix == nullptr) {
cout << "Matrix doesn't exist" << endl;
continue;
}
switch (cmd) {
case 1:
cout << "Entering the Matrix: " << endl;
cout << "Enter the size of Matrix." << endl;
cout << "Rows >> ";
cin >> rows;
cout << "Columns >> ";
cin >> columns;
if (matrix != nullptr) {
delete matrix;
}
matrix = new Matrix(rows, columns);
cout << "Enter elements of the Matrix:" << endl;
matrix->input(rows, columns);
cout << "Successfully entered!" << endl;
break;
case 2:
cout << "Entering the Matrix: " << endl;
cout << "Enter the size of Matrix." << endl;
cout << "Rows >> ";
cin >> rows;
cout << "Columns >> ";
cin >> columns;
if (matrix != nullptr) {
delete matrix;
}
arr = new double[rows*columns];
cout << "Entering the array: " << endl;
for (int i = 0; i < rows*columns; i++) {
cout << "arr[" << i << "]: ";
cin >> arr[i];
}
matrix = new Matrix(rows, columns);
matrix->input(rows, columns, arr);
delete[] arr;
cout << "Successfully entered!" << endl;
break;
case 3:
cout << "Printing the Matrix: " << endl;
matrix->print();
cout << "Successfully printed!" << endl;
break;
case 4:
cout << "Sum of Matrixes: " << endl;
cout << "Entering the Matrix: " << endl;
cout << "Enter the size of Matrix." << endl;
cout << "Rows >> ";
cin >> rows;
cout << "Columns >> ";
cin >> columns;
tmp = new Matrix(rows, columns);
cout << "Enter elements of the Matrix:" << endl;
tmp->input(rows, columns);
result = matrix->sum(tmp);
if (result == nullptr) {
cout << "Matrixes cannot be sum" << endl;
}
else {
cout << "Result:" << endl;
result->print();
delete result;
}
delete tmp;
cout << "Successfully calculated!" << endl;
break;
case 5:
cout << "Multiply of Matrixes: " << endl;
cout << "Entering the Matrix: " << endl;
cout << "Enter the size of Matrix." << endl;
cout << "Rows >> ";
cin >> rows;
cout << "Columns >> ";
cin >> columns;
tmp = new Matrix(rows, columns);
cout << "Enter elements of the Matrix:" << endl;
tmp->input(rows, columns);
result = matrix->mult(tmp);
if (result == nullptr) {
cout << "Matrixes cannot be multiplied" << endl;
}
else {
cout << "Result:" << endl;
result->print();
delete result;
}
delete tmp;
cout << "Successfully calculated!" << endl;
break;
default:
running = false;
if (matrix != nullptr) {
delete matrix;
}
break;
}
cout << endl;
}
cout << "Terminated" << endl;
system("pause>nul");
return 0;
}