Repository navigation
Expand file tree
/
Copy pathMatrix.cpp
More file actions
295 lines (232 loc) · 8.21 KB
/
Copy pathMatrix.cpp
File metadata and controls
295 lines (232 loc) · 8.21 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
#include <stdio.h>
#include "Matrix.h"
#include "IO.h"
#include <stdio.h>
#include "matrix.h"
int matrixes[MatMAX][MAX][MAX];
int rows[MatMAX];
int cols[MatMAX];
int savedmx = 2;
FILE* vstup = NULL;
FILE* vystup = NULL;
void addMatrix(int x, int y) {
// Vstupné riadky a ståpce matice 1
int r = rows[x];
int s = cols[x];
if (r != rows[y] || s != cols[y]) { // Kontrola rozmeru
printf("Matice nemaju rovnaky rozmer!\n");
return;
}
// Riadky a ståpce výslednej matice
rows[0] = r;
cols[0] = s;
// Sèítanie prvkov
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
matrixes[0][i][j] = matrixes[x][i][j] + matrixes[y][i][j];
}
}
printf("Matice %d a %d boli scitane do matice 0.\n", x, y);
// Výpis záznamu do súboru
fprintf(vystup, "\nMatice %d a %d boli scitane do matice 0.\n", x, y);
FileprintMatrix(x); // 1. matica
FileprintMatrix(y); // 2. matica
FileprintMatrix(0); // výsledná
fprintf(vystup, "----------------------------------------\n");
printMatrix(0); // Výpis výslednej matice do cmd
saveMatrix(); // Uloženie do pamäte
}
void subMatrix(int x, int y) {
int r = rows[x];
int s = cols[x];
if (r != rows[y] || s != cols[y]) {
printf("Matice nemaju rovnaky rozmer!\n");
return;
}
rows[0] = r;
cols[0] = s;
// Add values
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
matrixes[0][i][j] = matrixes[x][i][j] - matrixes[y][i][j]; // od addMatrix je rozdiel v znamienku
}
}
printf("Matice %d a %d boli odscitane do matice 0.\n", x, y);
fprintf(vystup, "\nMatice %d a %d boli odscitane do matice 0.\n", x, y);
FileprintMatrix(x);
FileprintMatrix(y);
FileprintMatrix(0);
fprintf(vystup, "----------------------------------------\n");
printMatrix(0);
saveMatrix();
}
void multMatrix(int x, int y) {
// Rozmery pre kontrolu a výpoèet násobenia
int r0 = rows[x];
int s0 = cols[x];
int r1 = rows[y];
int s1 = cols[y];
if (s0 != r1) { // Kontrola riadkov matice
printf("Nasobenie nie je mozne! Pocet stlpcov A sa nerovna poctu riadkov B.\n");
return;
}
// Zapísanie riadkov a ståpcov výslednej matice a do pamäte
rows[0] = r0;
cols[0] = s1;
// Nulová inicializácia matice ~ všetky prvky sú 0
for (int i = 0; i < r0; i++) {
for (int j = 0; j < s1; j++) {
matrixes[0][i][j] = 0;
// Násobenie prvkov
for (int k = 0; k < s0; k++) {
matrixes[0][i][j] += matrixes[x][i][k] * matrixes[y][k][j];
}
}
}
printf("Matice %d a %d boli vynasobene do matice 0.\n", x, y);
// Zápis do súboru vystup.txt
fprintf(vystup, "Matice %d a %d boli vynasobene do matice 0.\n", x, y);
FileprintMatrix(x);
FileprintMatrix(y);
FileprintMatrix(0);
fprintf(vystup, "----------------------------------------\n");
printMatrix(0);
saveMatrix();
}
void skamultMatrix(int x, int M) {
int r = rows[M];
int s = cols[M];
rows[0] = r;
cols[0] = s;
// Násobenie skalárom x
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
matrixes[0][i][j] = matrixes[M][i][j] * x;
}
}
printf("Matica %d bola vynasobena s cislom %d do matice 0.\n", M, x);
// Zápis matíc do súboru
fprintf(vystup, "Matica %d bola vynasobena s cislom %d do matice 0.\n", M, x);
FileprintMatrix(M);
FileprintMatrix(0);
fprintf(vystup, "----------------------------------------\n");
printMatrix(0);
saveMatrix();
}
void detMatrix(int M) {
// Kontrola èi je matica štvorcová
if (rows[M] != cols[M]) {
printf("Matica %d nie je stvorcova", M);
return;
}
// Kontrola maximálnej ve¾kosti matice
if (rows[M] >= 5) {
printf("Matica %d je vacsie nez 4x4", M);
return;
}
int n = rows[M]; // ve¾kos matice pre switch
int det; // premenná determinantu
switch (n) {
case 1: // matica 1x1
det = matrixes[M][0][0];
break;
case 2: // matica 2x2
det = (matrixes[M][0][0] * matrixes[M][1][1]) - (matrixes[M][0][1] * matrixes[M][1][0]);
break;
case 3: // matica 3x3
det = (matrixes[M][0][0] * matrixes[M][1][1] * matrixes[M][2][2] + matrixes[M][0][1] * matrixes[M][1][2] * matrixes[M][2][0] + matrixes[M][0][2] * matrixes[M][1][0] * matrixes[M][2][1]) -
(matrixes[M][0][2] * matrixes[M][1][1] * matrixes[M][2][0] + matrixes[M][0][1] * matrixes[M][1][0] * matrixes[M][2][2] + matrixes[M][1][2] * matrixes[M][2][1] * matrixes[M][0][0]);
break;
case 4: // matica 4x4
int a = matrixes[M][0][0], b = matrixes[M][0][1], c = matrixes[M][0][2], d = matrixes[M][0][3];
int m11 = matrixes[M][1][0], m12 = matrixes[M][1][1], m13 = matrixes[M][1][2], m14 = matrixes[M][1][3];
int m21 = matrixes[M][2][0], m22 = matrixes[M][2][1], m23 = matrixes[M][2][2], m24 = matrixes[M][2][3];
int m31 = matrixes[M][3][0], m32 = matrixes[M][3][1], m33 = matrixes[M][3][2], m34 = matrixes[M][3][3];
int dettemp =
a * (m12 * (m23 * m34 - m24 * m33)
- m13 * (m22 * m34 - m24 * m32)
+ m14 * (m22 * m33 - m23 * m32))
- b * (m11 * (m23 * m34 - m24 * m33)
- m13 * (m21 * m34 - m24 * m31)
+ m14 * (m21 * m33 - m23 * m31))
+ c * (m11 * (m22 * m34 - m24 * m32)
- m12 * (m21 * m34 - m24 * m31)
+ m14 * (m21 * m32 - m22 * m31))
- d * (m11 * (m22 * m33 - m23 * m32)
- m12 * (m21 * m33 - m23 * m31)
+ m13 * (m21 * m32 - m22 * m31));
det = dettemp; // uloženie do premenej det
break;
}
printf("Determinant matice %d je %d \n", M, det);
fprintf(vystup, "Determinant matice %d je %d \n", M, det);
FileprintMatrix(M);
fprintf(vystup, "\n\n%d\n", det);
fprintf(vystup, "----------------------------------------\n");
}
void transMatrix(int M) { // Transponovanie matice
int r = rows[M];
int s = cols[M];
rows[0] = s;
cols[0] = r;
// Èítanie matice
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
matrixes[0][i][j] = matrixes[M][j][i]; // Výmena riadkov so ståpcami
}
}
printf("Matica bola transponovana do matice 0.\n");
// Zápis do súboru
fprintf(vystup, "Matica bola transponovana do matice 0.\n");
FileprintMatrix(0);
fprintf(vystup, "----------------------------------------");
printMatrix(0);
saveMatrix();
}
void matrixChoice(int* ptr) { // Detekcia chýb aby uživate¾ zadal existujúcu maticu
int C;
while (1) { // Nekoneèný cyklus
scanf("%d", &C);
if (C >= 1 && C <= savedmx) { // rozsah medzi 1 - poèet uložených matíc
*ptr = C;
break;
}
printf("Prosim zadajte cislo medzi 1 a %d.\n", savedmx); // ak nesplní podmienky
}
}
void saveMatrix() {
// Kontrolné hodnoty na uloženie výsledku
int wantsave;
int tosave;
wantsavefunc:
printf("Chcete ulozit vysledok? 1-Ano, 0-Nie\n");
scanf("%d", &wantsave);
if (wantsave != 0 && wantsave != 1) { // Kontrola èi uživatel zadal 0 alebo 1
printf("Prosim zadajte cislo 0 alebo 1! \n");
goto wantsavefunc;
}
if (wantsave == 0) { // Ak 0 tak sa nezapíše výsledok a vráti sa do main()
return;
}
if (savedmx >= MatMAX - 1) { // Maximum matíc v súbore
printf("Uz ste ulozili %d matic. \n Ktoru maticu chcete prepisat? \n", MatMAX - 1);
scanf("%d", &tosave);
tosave;
}
else { // Ak nie je maximum matíc tak sa uloží matica
savedmx++;
tosave = savedmx;
}
int r = rows[0]; // Ve¾kos riadku matice 0
int s = cols[0]; // Ve¾kos ståpca matice 0
// Ve¾kos riadkov a ståpcov pre uložené matice
rows[tosave] = r;
cols[tosave] = s;
// Èítanie matice
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
matrixes[tosave][i][j] = matrixes[0][i][j]; // Uloženie matice 0 do matice[x]
}
}
printf("Matica bol ulozeny ako matice %d \n", tosave); // Výpis poradia matice
}