-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAISD_LAB2.cpp
More file actions
349 lines (313 loc) · 8.67 KB
/
AISD_LAB2.cpp
File metadata and controls
349 lines (313 loc) · 8.67 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
#include <locale.h>
#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS
#include <malloc.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <iostream>
#include "matrix.h"
#include <complex>
template<class T>
T Check_Double()
{
T number;
while (!(cin >> number) || (cin.peek() != '\n'))
{
cin.clear();
while (cin.get() != '\n');
cout << "Введите корректное значение...\n";
}
return number;
}
int Check_Int()
{
int number = 0;
while (number <= 0)
{
while (!(cin >> number) || (cin.peek() != '\n'))
{
cin.clear();
while (cin.get() != '\n');
cout << "Введите корректное значение...\n";
}
if (number <= 0) cout << "Введите корректное значение...\n";
}
return number;
}
int get_key()
{
int key = _getch();
if ((key == 0) || (key == 224))
key = _getch();
return key;
}
int menu1()
{
std::cout << "\nMatrix Class.\n\n"
"1 - View/edit the studied matrices.\n"
"2 - Matrix Addition(+)\n"
"3 - Matrix difference(-)\n"
"4 - multiplication matrix(*)\n"
"5 - Multiplying a matrix by a scalar\n"
"6 - Dividing a matrix by a scalar\n"
"7 - Calculating the trace of the matrix\n"
"8 - Checking vectors for coplanarity\n"
"9 - Matrix Comparison\n"
"0 - Access to an element in matrix A by index.Var-3\n\n"
"Exit: Esc";
while (true)
{
int key = get_key();
if ((key == 27) || (key >= '0' && key <= '9'))
return key;
}
}
int menu2()
{
std::cout << "\n\n";
std::cout << "Resume: Enter";
while (true)
{
int key = get_key();
if (key == 13) return key;
}
}
int menu3()
{
std::cout << "1 - Yes\n2 - No";
while (true)
{
int key = get_key();
if (key == '1' || key == '2') return key;
}
}
template <class T>
std::ostream& operator << (std::ostream& s, const Matrix<T>& matrix);
template <class T>
int menu()
{
Matrix<T> A(2, 2), B(2, 2), C(2, 2), D(2, 2);
A.Random();
B.Random();
while (true) {
system("cls");
int m1 = menu1();
switch (m1) {
case 27: return 0;
case 49: {
system("cls");
std::cout << "\tInvestigated matrices:\n\n";
std::cout << "A:\n" << A << endl;
std::cout << "B:\n" << B;
std::cout << "\n\nDo you want to change the matrices?" << endl;
int m3 = menu3();
switch (m3) {
case 49: {
system("cls");
std::cout << "\n--------Setting the matrix A--------" << endl;
A.EnterMatrix();
std::cout << "\n--------Setting the matrix B--------" << endl;
B.EnterMatrix();
break;
}
case 50: {
break;
}
}
break;
}
case 50: {
system("cls");
std::cout << "\tMatrix Addition.\n\n";
bool check = 0;
try {
C = A + B;
check = 1;
}
catch (const char* message) {
std::cout << message;
}
if (check) {
std::cout << "A + B:\n\n" << endl;
std::cout << A << "\n +\n\n" << B << "\n = \n\n";
std::cout << A + B;
}
break;
}
case 51: {
system("cls");
std::cout << "\tMatrix difference.\n\n";
bool check = 0;
try {
C = A - B;
check = 1;
}
catch (const char* message) {
std::cout << message;
}
if (check) {
C = A - B;
std::cout << "A - B:\n\n" << endl;
std::cout << A << "\n -\n\n" << B << "\n = \n\n";
std::cout << C;
}
break;
}
case 52: {
system("cls");
std::cout << "\tmultiplication matrix.\n\n";
bool check = 0;
try {
C = A * B;
check = 1;
}
catch (const char* message) {
std::cout << message;
}
if (check) {
C = A * B;
std::cout << "A * B:\n\n" << endl;
std::cout << A << "\n *\n\n" << B << "\n = \n\n";
std::cout << C;
}
break;
}
case 53: {
system("cls");
std::cout << "\tMultiplying a matrix by a scalar.\n\n";
T a;
std::cout << "Enter a value (scalar): ";
a = Check_Double<T>();
std::cout << "A * a:\n\n" << endl;
std::cout << A << "\n *\n\n " << a << "\n\n = \n\n";
C = A * a;
std::cout << C;
break;
}
case 54: {
system("cls");
std::cout << "\tDividing a matrix by a scalar.\n\n";
T a;
std::cout << "Enter a value (scalar): ";
a = Check_Double<T>();
std::cout << "A / a:\n\n" << endl;
std::cout << A << "\n /\n\n " << a << "\n\n = \n\n";
C = A / a;
std::cout << C;
break;
}
case 55: {
system("cls");
std::cout << "\tCalculating the trace of the matrix.\n\n";
std::cout << "Tr(A) = " << A.tr();
break;
}
case 56: {
system("cls");
std::cout << "Checking vectors for coplanarity." << endl;
Matrix<T> test;
test.CreateMatrixForCheck();
test.CheckTheDeterminant();
break;
}
case '0':
{
system("cls");
std::cout << "Access to an element in matrix A by index." << endl;
std::cout << "enter the row: ";
int x;
cin >> x;
std::cout << "\nenter the column: ";
int y;
cin >> y;
std::cout << "\nthe value of the element: " << A(x, y);
std::cout << "\nDo you want to change ellement?\n";
int m3 = menu3();
switch (m3) {
case 49: {
system("cls");
cout << "Enter the value: ";
A(x, y) = Check_Double<T>();
break;
}
case 50: {
break;
}
break;
}
break;
}
case 57:
{
system("cls");
std::cout << "Matrix Comparison.\n Enter epsilon: " << endl;
std::cin >> A.epsilon;
if (A == B)
{
std::cout << "A == B\n";
}
else
{
std::cout << "A!=B\n";
}
}
}
while (true)
{
int m2 = menu2();
if (m2 == 13) break;
}
}
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "");
system("cls");
bool exit = true;
while (exit) {
system("cls");
cout << "Здравствуйте! Вас приветствует программа \"МНОГО МАТРИЦ\"\n" << endl;
cout << "Нажмите:" << endl;
cout << "1 - чтобы работать с типом int" << endl;
cout << "2 - чтобы работать с типом double" << endl;
cout << "3 - чтобы работать с типом float" << endl;
cout << "4 - чтобы работать с типом complex-double" << endl;
cout << "5 - чтобы работать с типом complex-float" << endl;
cout << "0 - чтобы завершить работу" << endl;
int key = get_key();
switch (key)
{
case 49:
menu<int>();
break;
case 50:
menu<double>();
break;
case 51:
menu<float>();
break;
case 52:
menu<complex<double>>();
break;
case 53:
menu<complex<float>>();
break;
case 48:
exit = false;
break;
default:
break;
}
}
return 0;
}