-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (94 loc) · 2.54 KB
/
main.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
#include "Matrix.h"
#include <cassert>
#include <iomanip>
using namespace std;
size_t cast_to_sizet(const string &s) {
for(char c: s) {
if(!isdigit(c)) { // also takes care of negative numbers
cout << "invalid input for matrix dimensions" << endl;
exit(0);
}
}
return size_t(stoi(s));
}
void print_available_operation() {
cout << "Please select a matrix operation to perform:\n"
<< "0: quit\n"
<< "1: get determinant\n"
<< "2: get REF\n"
<< "3: get RREF\n"
<< "4: multiply 2 matrix\n"
<< "5: get inverse\n"
<< "6: get transpose\n"
<< "Enter: \n";
}
pair<int, int> get_dimensions() {
size_t row, col;
string input;
cout << "Please enter number of rows: ";
getline(cin, input); row = cast_to_sizet(input);
cout << "Please enter number of columns: ";
getline(cin, input); col = cast_to_sizet(input);
return {row, col};
}
void test_an_operation(size_t op_code) {
if(op_code == 0)
return;
size_t row, col;
tie(row, col) = get_dimensions();
Matrix m(row, col, true);
Matrix result_matrix;
switch (op_code) {
case 1: {// numeric result
double det = m.getDeterminant();
cout << "determinant: " << det << endl;
return;
}
case 2: {
result_matrix = m.getREF(false);
break;
}
case 3: {
result_matrix = m.getREF(true);
break;
}
case 4: {
size_t row, col;
tie(row, col) = get_dimensions();
Matrix m2(row, col, true);
result_matrix = m.multiply(m2);
break;
}
case 5: {
result_matrix = m.getInverse();
// auto identity = m.multiply(result_matrix);
// identity.printMatrix();
break;
}
case 6: {
result_matrix = m.getTranspose();
break;
}
}
cout << "result matrix: \n";
result_matrix.printMatrix();
}
int main(int argc, char *argv[]) {
// this translates it to scientific number then set precision
// cout.precision(2);
// e.g. 0.00015 --> 1.5e-4
// this direcly sets precision
// e.g. 0.00015 --> 0.00
cout << fixed;
cout << setprecision(2);
print_available_operation();
string op_code;
getline(cin, op_code);
test_an_operation(cast_to_sizet(op_code));
return 0;
}
// some ideas
// - make it iteractive:
// - put a while to create a session
// - hold a set of matrix user created or resulted from operation
// and make them reusable (with a map)