-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.Solve.SetOfEquations.h
248 lines (213 loc) · 6.98 KB
/
room.Solve.SetOfEquations.h
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
//-----------------------------------------------declaration------------------------------------------------------------------//
// functions for creating interface
void highlightSolveSetOfEquations(int x0, int y0, int x0S, int x, double* solution, int secondColor);
void unhighlightSolveSetOfEquations(int x0, int y0, int x0S, int x, double* solution, int firstColor);
// get coefiicents
int getCoefficient(int x0, int y0, int x, int y, double a[50][50], int firstColor, int secondColor);
// functions for solving
void swapRow(int row1, int row2, double a[50][50]);
void solveSetOfNEquations(int num, int firstColor, int secondColor);
//----------------------------------------------------------------------------------------------------------------------------//
// get coefiicents
int getCoefficient(int x0, int y0, int x, int y, double a[50][50], int firstColor, int secondColor){
textColor(secondColor);
goToXY(x0 + 15 + min2Int(x - 1, 3)*15, y0 + 6 + min2Int(y - 1, 1)* 2); printf(" ");
goToXY(x0 + 19 + min2Int(x - 1, 3)*15, y0 + 6 + min2Int(y - 1, 1)* 2);
char ch, numStr[35];
int count = 0, dot = 0;
double num;
do{
ch = getch();
// esc
if (ch == 27) return 0;
// backspace
if ((ch == 8) && (count > 0)){
if (numStr[count] == '.') dot--;
count--;
goToXY(x0 + 19 + min2Int(x - 1, 3)*15 + count, y0 + 6 + min2Int(y - 1, 1)* 2); printf(" ");
goToXY(x0 + 19 + min2Int(x - 1, 3)*15 + count, y0 + 6 + min2Int(y - 1, 1)* 2);
}
// numbers
if ((ch >= '0') && (ch <= '9') && (count <= 29)){
goToXY(x0 + 19 + min2Int(x - 1, 3)*15 + count, y0 + 6 + min2Int(y - 1, 1)* 2); printf("%c", ch);
count++;
numStr[count] = ch;
}
// dot
if ((ch == '.') && (count > 0) && (count <= 29) && (numStr[count] != '-') && (dot == 0)){
goToXY(x0 + 19 + min2Int(x - 1, 3)*15 + count, y0 + 6 + min2Int(y - 1, 1)* 2); printf("%c", ch);
count++;
numStr[count] = ch;
dot++;
}
// minus
if ((ch == '-') && (count == 0)){
goToXY(x0 + 19 + min2Int(x - 1, 3)*15 + count, y0 + 6 + min2Int(y - 1, 1)* 2); printf("%c", ch);
count++;
numStr[count] = ch;
}
// enter
if (ch == 13){
if ((count > 0) && (numStr[count] != '.') && (numStr[count] != '-')){
numStr[++count] = '\0';
shiftLeft(numStr);
a[x][y] = convertStringToDouble(numStr);
textColor(firstColor);
return 1;
}
}
} while (1);
}
// highlight current part
void highlightSolveSetOfEquations(int x0, int y0, int x0S, int x, double* solution, int secondColor){
textColor(secondColor);
goToXY(x0 + 15 + (x - x0S)*15, y0 + 15); printf(" ");
goToXY(x0 + 15 + (x - x0S)*15, y0 + 15); printf("%9.4lf", solution[x]);
goToXY(119, 29);
}
// unhighlight current part
void unhighlightSolveSetOfEquations(int x0, int y0, int x0S, int x, double* solution, int firstColor){
textColor(firstColor);
goToXY(x0 + 15 + (x - x0S)*15, y0 + 15); printf(" ");
goToXY(x0 + 15 + (x - x0S)*15, y0 + 15); printf("%9.4lf", solution[x]);
}
// swap 2 equation's position
void swapRow(int row1, int row2, double a[50][50]){
int i;
double tmp;
for (i = 1; i <= 49; i++){
tmp = a[i][row1];
a[i][row1] = a[i][row2];
a[i][row2] = tmp;
}
}
// solve set of n equations
void solveSetOfNEquations(int num, int firstColor, int secondColor){
startOfSolve: num = num;
int x0 = 25, y0 = 10;
goToXY(x0, y0); printf("SOLVING SET OF %u EQUATIONS", num);
goToXY(x0, y0 + 2); printf("Input:");
// get input
double a[50][50];
int i, j, k, m, x0C, y0C;
for (i = 1; i <= num; i++)
for (j = 1; j <= num + 1; j++){
// delete previous contents
goToXY(0, y0 + 6);
for (k = 1; k <= 120*4; k++) printf(" ");
// print new contents
x0C = max2Int(j - 3, 1); y0C = max2Int(i - 1, 1);
for (k = x0C; k <= min(x0C + 3, num + 1); k++){
goToXY(x0 + 15 + (k - x0C)*15, y0 + 4); printf("%6.0u", k);
}
for (k = y0C; k <= y0C + 1; k++){
goToXY(x0 + 12, y0 + 6 + (k - y0C)*2); printf("%u", k);
}
for (m = y0C; m <= min2Int(y0C + 1, i); m++){
for (k = x0 + 15; k <= 120; k++){
goToXY(k, y0 + 8); printf(" ");
}
for (k = x0C; k <= x0C + 3; k++)
if (((m < i) || (k < j)) && (k <= num + 1)){
goToXY(x0 + 15 + (k - x0C)*15, y0 + 6 + (m - y0C)* 2); printf(" ");
goToXY(x0 + 15 + (k - x0C)*15, y0 + 6 + (m - y0C)* 2); printf("%8.2lf", a[k][m]);
}
}
// scan for coefficient
if (getCoefficient(x0, y0, j, i, a, firstColor, secondColor) == 0) return;
}
// print last coefficent
goToXY(x0 + 15 + (j - x0C - 1)*15, y0 + 6 + (i - y0C - 1)* 2);
for (k = 1; k <= 120; k++) printf(" ");
goToXY(x0 + 15 + (j - x0C - 1)*15, y0 + 6 + (i - y0C - 1)* 2); printf("%8.2lf", a[num + 1][num]);
// SOLVE
// make triangle and check for solutions
int noSolutionCheck, allZeroCheck;
double ratio;
for (i = 1; i <= num; i++){
if (a[i][i] == 0){
for (j = i + 1; j <= num; j++) if (a[i][j] == 0) swapRow(i, j, a);
if (a[i][i] == 0){ // no or infinite solutions
for (k = i; k <= num; k++){
allZeroCheck = 1;
for (m = i + 1; m <= num; m++) if (a[m][k] != 0) allZeroCheck = 0;
if ((allZeroCheck == 1) && (a[num + 1][k] != 0)){
noSolutionCheck = 1;
goto noSolution;
}
}
noSolutionCheck = 2;
goto noSolution;
}
}
else{
for (j = i + 1; j <= num; j++){
ratio = - a[i][j] / a[i][i];
for (k = i; k <= num + 1; k++)
a[k][j] += a[k][i] * ratio;
}
}
}
// get solutions
double solution[50];
solution[num] = a[num + 1][num] / a[num][num];
for (i = num; i >= 1; i--){
for (j = num; j >= i + 1; j--) a[num + 1][i] -= a[j][i] * solution[j];
solution[i] = a[num + 1][i] / a[i][i];
}
// print solutions
int x = 1, x0S = 1;
goToXY(x0, y0 + 11); printf("Solutions:");
char ch = 75;
do{
if ((ch == 75) || (ch == 77)){
for (k = x0S; k <= min(x0S + num - 1, x0S + 3); k++){
goToXY(x0 + 15 + (k - x0S)*15, y0 + 13); printf("%6u", k);
goToXY(x0 + 15 + (k - x0S)*15, y0 + 15); printf("%9.4lf", solution[k]);
}
highlightSolveSetOfEquations(x0, y0, x0S, x, solution, secondColor);
}
ch = getch();
// esc
if (ch == 27) return;
// arrow keys
if (ch == -32){
ch = getch();
// arrow left
if (ch == 75){
unhighlightSolveSetOfEquations(x0, y0, x0S, x, solution, firstColor);
x--;
if (x == 0) x = 1;
if (x < x0S) x0S -= 1;
}
// arrow right
if (ch == 77){
unhighlightSolveSetOfEquations(x0, y0, x0S, x, solution, firstColor);
x++;
if (x == num + 1) x = num;
if (x > x0S + 3) x0S += 1;
}
}
// enter
if (ch == 13){
textColor(firstColor);
goToXY(x0, y0 + 11); for (i = 1; i <= 120*5; i++) printf(" ");
goto startOfSolve;
}
} while (1);
// print no or infinite solutions
noSolution:
goToXY(x0, y0 + 11);
if (noSolutionCheck == 1) printf("Solutions: None");
else printf("Solutions: Infinite");
goToXY(119, 29);
do{
ch = getch();
if (ch == 13){
textColor(firstColor);
goToXY(x0, y0 + 11); for (i = 1; i <= 120*5; i++) printf(" ");
goto startOfSolve;
}
} while (ch != 27);
return;
}