-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
492 lines (459 loc) · 14.3 KB
/
source.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <Windows.h>
#include <conio.h>
#include <chrono>
#include "matrix.h"
#define period 70 //speed of spinnig
void loading(int time, int count) //count is the number of slashes //time is the period of spinning //period is the speed of spinning
{
for (int i = 0; i < time; i++)
{
if (_kbhit())
break;
for (int k = 0; k < count; k++)cout << "\\";
Sleep(period);
for (int k = 0; k < count; k++)cout << "\b";
for (int k = 0; k < count; k++)cout << "|";
Sleep(period);
if (_kbhit())
{
cout << "\b";
break;
}
for (int k = 0; k < count; k++)cout << "\b";
for (int k = 0; k < count; k++)cout << "/";
Sleep(period);
for (int k = 0; k < count; k++)cout << "\b";
for (int k = 0; k < count; k++)cout << "-";
Sleep(period);
for (int k = 0; k < count; k++)cout << "\b"; //change count to 1 and this can be commented to give a nice effect
}
}
void intro(void)
{
cout << "////////////////////////////////////////////////////////////////////////////////";
cout << "///**///***////***/////////////////////////////////////////*****//**//**/*****//";
cout << "//*//*//**//*/**/**///// /////**//**/**/**//**//**/";
cout << "/**//**/****//**/**/// Welcome To Matrix Calculator ///*****//***////*****//";
cout << "/******/**//*/**/**///// /////**//**/***////*****//";
cout << "/**//**/**//*/**/**////////////////////////////////////////**//**/**/**//**//**/";
cout << "/**//**/***////***/////////////////////////////////////////*****//**//**/**//**/";
cout << "///////////////////// *How to Use* //////////////////////";
cout << "// //";
cout << "// Example 1: Example 2: Example 3: Example 4*: //";
cout << "// //";
cout << "// [1 2 3;4 5 6] [1 2+i;2 1+i] [3 6;7 2] [5 7;9 4] //";
cout << "// + D ^ C //";
cout << "// [1 2 3;4 5 6] = 2 [6;9] //";
cout << "// = [-3-i] = X1 = 0.9069 //";
cout << "// [2 4 6;8 10 12] [51 30;35 46] X2 = 0.2093 //";
cout << "// //";
cout << "// Supported Operations: + , - , * , / , ^ , T , D , I , C , E , R //";
cout << "// T:Transpose D:Determinanat I:Inverse C:Cramer E:Elimination R:Rank //";
cout << "// *Enter coefficient matrix followed by \"C\" then the answer column //";
cout << "// //";
cout << "////////////////////////////////////////////////////////////////////////////////";
cout << "////////////////////////////////////////////////////////////////////////////////\n";
}
int main() //note that the run time that is calculated is just for filling and the opertaion it self ignoring the const. periodes like the checks for exit and skip
{
intro();
loading(20, 1); //any key to skip preferably (down arrow)
system("pause");
matrix matA, matC; //variables are declared here to avoid re-construction
string input;
auto start = chrono::high_resolution_clock::now();
auto elapsed = chrono::high_resolution_clock::now() - start;
int fill;
char operation/*, repeat*/;
start:
cout << "Waiting for input:\n\n";
error_start:
while (1)
{
cin.sync();
getline(cin, input);
start = chrono::high_resolution_clock::now();
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
fill = matA.fill(input); //to be calculated once of better run time
if (fill == 1)
{
cout << "\nPlease Try Again:\n\n";
input.clear();
goto error_start;
}
else if (fill == 2)
{
matA = matC;
matA.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matA.get_rows() << " x " << matA.get_columns() << endl;
}
else if (fill == 3)
{
matA.randomize();
matA.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matA.get_rows() << " x " << matA.get_columns() << endl;
}
else if (fill == 4)
{
matA.square_randomize();
matA.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matA.get_rows() << " x " << matA.get_columns() << endl;
}
else if (fill == 6)
{
cout << "\nSorry, You Can Not Use prand As The First Operand\nPlease Try Again:\n\n";
input.clear();
goto error_start;
}
else if (fill == 7)
{
int r, c;
cin >> r >> c; //run time not right this case
cin.sync();
matA.selective_randomize(r, c);
matA.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matA.get_rows() << " x " << matA.get_columns() << endl;
}
else if (fill == 0)
cout << "Dimentions(Rows x Columns): " << matA.get_rows() << " x " << matA.get_columns() << endl;
elapsed = chrono::high_resolution_clock::now() - start;
long long microseconds = chrono::duration_cast<chrono::microseconds>(elapsed).count();
opr_start:
input.clear();
getline(cin, input);
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
else if (input.size() > 1)
{
cout << "Error!\nInvalid/Unsupported Operation\n" << "Please Choose The Operation Again:\n\n";
matA.print();
cout << endl;
goto opr_start;
}
operation = input[0];
input.clear();
/*--------------------------------------------------------------------------------------------------------------------------------*/
if (operation == '+' || operation == '-' || operation == '*' || operation == '/' || operation == 'C')
{
matrix matB;
E1:
cin.sync(); //because getline is right after cin >> opertaion so it just grasps that input (e.g the operation) as input2
getline(cin, input);
start = chrono::high_resolution_clock::now();
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
fill = matB.fill(input);
if (fill == 1)
{
cout << "\nPlease Enter the Second Matrix Again:\n\n";
matA.print();
cout << endl << operation << endl;
input.clear();
goto E1;
}
else if (fill == 2)
{
matB = matC;
matB.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matB.get_rows() << " x " << matB.get_columns() << endl;
}
else if (fill == 3 || fill == 4 || fill == 7)
{
cout << "\nSorry, You Can Not Use a Random Matrix As The Second Operand\nConsider Using prand Instead\nPlease Enter the Second Matrix Again:\n\n";
matA.print();
cout << endl << operation << endl;
input.clear();
goto E1;
}
else if (fill == 6)
{
if (operation == '+' || operation == '-')
matB.proper_randomize(matA.get_rows(), matA.get_columns());
else if (operation == '*')
matB.proper_randomize(matA.get_columns(), matA.get_rows());
else if (operation == '/')
matB.proper_randomize(matA.get_columns(), matA.get_columns());
else
matB.proper_randomize(matA.get_rows(), 1);
matB.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matB.get_rows() << " x " << matB.get_columns() << endl;
}
else if (fill == 7) //keep it for later
{
int r, c;
cin >> r >> c;
cin.sync();
matB.selective_randomize(r, c);
matB.print();
cout << endl;
cout << "Dimentions(Rows x Columns): " << matB.get_rows() << " x " << matB.get_columns() << endl;
}
else if (fill == 0)
cout << "Dimentions(Rows x Columns): " << matB.get_rows() << " x " << matB.get_columns() << endl;
switch (operation)
{
case '+':
{
if (matC.add(matA, matB) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case '-':
{
if (matC.subtract(matA, matB) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case '*': //using show_result with the output dimentions rowsIn1 , columnIn2
{
if (matC.multiply(matA, matB) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case '/': //multiplying A(A^(-1)) --> columns of A should be the same as rows of A^(-1)
{
if (matC.divide(matA, matB) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case 'C':
{
vector_m vec_C; //result vec
bool state1;
vec_C = matA.cramer(matB, state1);
if (state1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
if (vec_C.has_NaN())
{
cout << "Sorry Result is Out of Precision Range\n";
matC.intialize();
goto start;
}
vec_C.print_C();
break; //Done
}
}
if (operation != 'C')
{
if (matC.has_NaN())
{
cout << "Sorry Result is Out of Precision Range\n";
matC.intialize();
goto start;
}
cout << "=\n";
matC.print();
cout << endl << "Dimentions(Rows x Columns): " << matC.get_rows() << " x " << matC.get_columns();
}
//matB.deletem(); //not important as after the while restarts matB stays at the smae location so no memory leak
elapsed = chrono::high_resolution_clock::now() - start;
long long microseconds0 = chrono::duration_cast<chrono::microseconds>(elapsed).count();
cout << "\n\n\nExecution Time: " << (microseconds0 + microseconds) / 1000 << " ms" << endl;
}
/*--------------------------------------------------------------------------------------------------------------------------------*/
else if (operation == '^' || operation == 'I' || operation == 'T' || operation == 'D')
{
switch (operation)
{
case '^':
{
input.clear();
getline(cin, input);
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
string Power = input;
input.clear();
start = chrono::high_resolution_clock::now();
if (matC.power(matA, Power) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case 'I': //not working
{
start = chrono::high_resolution_clock::now();
if (matC.inverse(matA) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case 'T':
{
start = chrono::high_resolution_clock::now();
if (matC.transpose(matA) == 1)
{
cout << "\nPlease Try Again:\n\n";
goto error_start;
}
break; //Done
}
case 'D':
{
start = chrono::high_resolution_clock::now();
complex temp;
bool state;
temp = matA.determinant(state);
if (state)
{
cout << "\nPlease Try Again:\n\n";
state = 0;
goto error_start;
}
matC.set_element(0, 0, temp); //to be used with (ans)
matC.set_dimentions(1, 1);
break; //Done
}
}
if (matC.has_NaN())
{
cout << "Sorry Result is Out of Precision Range\n";
matC.intialize();
goto start;
}
cout << "=\n";
matC.print();
if (operation != 'D')cout << endl << "Dimentions(Rows x Columns): " << matC.get_rows() << " x " << matC.get_columns();
auto elapsed = chrono::high_resolution_clock::now() - start;
long long microseconds0 = chrono::duration_cast<chrono::microseconds>(elapsed).count();
cout << "\n\n\nExecution Time: " << (microseconds0 + microseconds) / 1000 << " ms" << endl;
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
else if (operation == 'E' || operation == 'R')
{
cout << "Do You Want The Detailed Solution ? [Y/N]: ";
input.clear();
again0:
cin.sync();
getline(cin, input);
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
char detail = input[0];
input.clear();
start = chrono::high_resolution_clock::now();
switch (detail)
{
case'Y':
case'y':
{
if (operation == 'E')
matC.eliminate_d(matA);
else
{
int R;
matC = matA;
R = matC.rank_d();
cout << "\nRank = " << R;
}
break;
}
case'N':
case'n':
{
if (operation == 'E')
{
matC.eliminate(matA);
cout << "=\n";
matC.print();
}
else
{
int R;
matC = matA;
R = matC.rank();
cout << "=\n";
matC.print();
cout << "\nRank = " << R;
}
break;
}
default:
cout << "Wrong Choice! Please Try Again :";
goto again0;
break; //Done
}
auto elapsed = chrono::high_resolution_clock::now() - start;
long long microseconds0 = chrono::duration_cast<chrono::microseconds>(elapsed).count();
cout << "\n\nExecution Time: " << (microseconds0 + microseconds) / 1000 << " ms" << endl;
}
else
{
cout << "Error!\nInvalid/Unsupported Operation\n" << "Please Choose The Operation Again:\n\n";
matA.print();
cout << endl;
goto opr_start;
}
/*cout << endl << endl << "Do you want to re-calculate ? [Y/N]: ";
input.clear();
again:
cin.sync();
getline(cin, input);
if (input == "skip" || input == "Skip" || input == "SKIP")
goto start;
else if (input == "exit" || input == "Exit" || input == "EXIT")
goto Out;
repeat = input[0];
input.clear();
switch (repeat)
{
case'Y':
case'y':
input.clear();
cout << endl << endl;
goto start;
break;
case'N':
case'n':
goto Out;
break;
default:
cout << "Wrong Choice! Please Try Again :";
goto again;
}
}
Out:
cout << endl << "Thank You!" << endl;
system("pause");
return 0;*/
goto start;
}
Out:
cout << endl << "Thank You!" << endl;
system("pause");
return 0;
}