-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-calculator.c
486 lines (471 loc) · 14 KB
/
matrix-calculator.c
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
#include <stdio.h>
#include <stdlib.h>
#include "matrix-operations.h"
matrix_t matrices[3];
typedef enum operations{
DISPLAY_MATRIX = 1,
CREATE_MATRIX, CREATE_IDENTITY_MATRIX,
IS_SQUARE, NEGATE, CREATE_SUBMATRIX, CALCULATE_DETERMINANT, CALCULATE_COFACTOR,
CALCULATE_TRANSPOSE, CALCULATE_ADJUGATE, CALCULATE_INVERSE,
FILL, ADD_N, SUBSTRACT_N, MULTIPLY_N, DIVIDE_N, RAISE_TO_N,
ADD_M, SUBSTRACT_M, MULTIPLY_M, DIVIDE_M
} operation;
int handleInput(){
int input = 0, s = 0;
do{
printf("Select operation\n");
printf("\t0. Exit.\n");
printf("\t1. Display matrix\n");
printf("\t2. Create matrix\n");
printf("\t3. Operations with a single matrix\n");
printf("\t4. Operations with a matrix and a scalar\n");
printf("\t5. Operations between 2 matrices\n");
printf("\n");
scanf("%d", &input);
printf("\n");
switch(input){
case 0:
return 0;
break;
case 1:
printf("Display matrix\n");
printf("Which matrix would you like to display?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
displayMatrix(matrices[s]);
break;
case 2:
do{
printf("Create matrix\n");
printf("\t0. Go back\n");
printf("\t1. Create custom matrix\n");
printf("\t2. Create identity matrix\n");
scanf("%d", &input);
switch(input){
case 1:
return CREATE_MATRIX;
break;
case 2:
return CREATE_IDENTITY_MATRIX;
break;
}
}while(input);
break;
case 3:
do{
printf("Operations with a single matrix\n");
printf("\t0. Go back\n");
printf("\t1. Check if matrix is square\n");
printf("\t2. Negate a matrix\n");
printf("\t3. Create a submatrix out of a matrix\n");
printf("\t4. Calculate the determinant of a matrix\n");
printf("\t5. Calculate the cofactor matrix of a matrix\n");
printf("\t6. Calculate the transpose matrix of a matrix\n");
printf("\t7. Calculate the adjugate matrix of a matrix\n");
printf("\t8. Calculate the inverse of a matrix\n");
scanf("%d", &input);
switch(input){
case 1:
return IS_SQUARE;
break;
case 2:
return NEGATE;
break;
case 3:
return CREATE_SUBMATRIX;
break;
case 4:
return CALCULATE_DETERMINANT;
break;
case 5:
return CALCULATE_COFACTOR;
break;
case 6:
return CALCULATE_TRANSPOSE;
break;
case 7:
return CALCULATE_ADJUGATE;
break;
case 8:
return CALCULATE_INVERSE;
break;
}
}while(input);
break;
case 4:
do{
printf("Operations with a matrix and a scalar\n");
printf("\t0. Go back\n");
printf("\t1. Fill matrix with a number\n");
printf("\t2. Add a number to a matrix\n");
printf("\t3. Substract a number from a matrix\n");
printf("\t4. Multiply matrix by a number\n");
printf("\t5. Divide matrix by a number\n");
printf("\t6. Raise matrix to the power of a number\n");
scanf("%d", &input);
switch(input){
case 1:
return FILL;
break;
case 2:
return ADD_N;
break;
case 3:
return SUBSTRACT_N;
break;
case 4:
return MULTIPLY_N;
break;
case 5:
return DIVIDE_N;
break;
case 6:
return RAISE_TO_N;
break;
}
}while(input);
break;
case 5:
do{
printf("Operations between 2 matrices\n");
printf("\t0. Go back\n");
printf("\t1. Add two matrices together\n");
printf("\t2. Substract one matrix from another\n");
printf("\t3. Multiply two matrices\n");
printf("\t4. Divide two matrices\n");
scanf("%d", &input);
switch(input){
case 1:
return ADD_M;
break;
case 2:
return SUBSTRACT_M;
break;
case 3:
return MULTIPLY_M;
break;
case 4:
return DIVIDE_M;
break;
}
}while(input);
break;
}
printf("\n");
if(!input)
input = -1;
}while(input);
}
void matrixCalculator(int input){
int s = 0, r = 0, c = 0, d = 0, f = 0;
float n;
switch(input){
case CREATE_MATRIX:
printf("Creating a new matrix\n");
printf("Insert the rows of the matrix: ");
scanf("%d", &r);
printf("Insert the columns of the matrix: ");
scanf("%d", &c);
printf("Where do you want to store the newly created matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = createMatrix(r, c);
initializeMatrix(&matrices[s]);
break;
case CREATE_IDENTITY_MATRIX:
printf("Creating a new identity matrix\n");
printf("Insert the dimensions of the matrix: ");
scanf("%d", &d);
printf("Where do you want to store the newly created matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = identityMatrix(d);
break;
case IS_SQUARE:
printf("Which matrix do you want to check if it is square?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Is matrix %s square? %s\n",
(s==0)?"A":(s==1)?"B":"C", (isSquare(matrices[s])?"Yes":"No"));
break;
case NEGATE:
printf("Which matrix do you wanna negate?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
negateMatrix(matrices[s]);
break;
case CREATE_SUBMATRIX:
printf("Out of which matrix would you like to create a submatrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
// TODO: not remove any
printf("Which row would you like to remove? From 0 to %d\n", matrices[s].rows-1);
scanf("%d", &r);
printf("Which columns would you like to remove? From 0 to %d\n", matrices[s].columns-1);
scanf("%d", &c);
matrices[s] = createSubmatrix(matrices[s], r, c);
break;
case CALCULATE_DETERMINANT:
printf("Of which matrix would you like to calculate the determinant of?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Determinant of matrix %s: %d\n",
(s==0)?"A":(s==1)?"B":"C", determinant(matrices[s]));
break;
case CALCULATE_COFACTOR:
printf("Of which matrix would you like to calculate the cofactor matrix of?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Where would you like to store the resulting cofactor matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
matrices[d] = cofactor(matrices[s]);
break;
case CALCULATE_TRANSPOSE:
printf("Of which matrix would you like to calculate the transpose matrix of?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Where would you like to store the resulting transpose matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
matrices[d] = transpose(matrices[s]);
break;
case CALCULATE_ADJUGATE:
printf("Of which matrix would you like to calculate the adjugate matrix of?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Where would you like to store the resulting adjugate matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
matrices[d] = adjugate(matrices[s]);
break;
case CALCULATE_INVERSE:
printf("Of which matrix would you like to calculate the inverse matrix of?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("Where would you like to store the resulting inverse matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
matrices[d] = inverse(matrices[s]);
break;
case FILL:
printf("What matrix would you like to fill with a number?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to fill matrix %s?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = fillN(matrices[d], n);
break;
case ADD_N:
printf("What matrix would you like to add a number to?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to add to matrix %s?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = addN(matrices[d], n);
break;
case SUBSTRACT_N:
printf("What matrix would you like to substract a number from?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to substract from matrix %s?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = substractN(matrices[d], n);
break;
case MULTIPLY_N:
printf("What matrix would you like to multiply by a number?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to multiply matrix %s by?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = multiplyByN(matrices[d], n);
break;
case DIVIDE_N:
printf("What matrix would you like to fill with a number?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to divide matrix %s by?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = divideByN(matrices[d], n);
break;
case RAISE_TO_N:
printf("What matrix would you like to raise to the power of a number?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("Where would you like to store the resulting matrix?\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
printf("With which number would you like to raise matrix %s to?\n",
(d==0)?"A":(d==1)?"B":"C");
scanf("%f", &n);
matrices[s] = raiseMatrixToN(matrices[d], n);
break;
case ADD_M:
printf("Which two matrices would you like to add up?\n");
printf("1st matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("2nd matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &f);
}while(f < 0 || f > 2);
printf("In what matrix would you like to store the resulting matrix of adding up matrices %s and %s?\n",
(d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = addMatrices(matrices[d], matrices[f]);
break;
case SUBSTRACT_M:
printf("Which two matrices would you like to substract?\n");
printf("1st matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("2nd matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &f);
}while(f < 0 || f > 2);
printf("In what matrix would you like to store the resulting matrix of substracting matrices %s and %s?\n",
(d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = substractMatrices(matrices[d], matrices[f]);
break;
case MULTIPLY_M:
printf("Which two matrices would you like to multiply?\n");
printf("1st matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("2nd matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &f);
}while(f < 0 || f > 2);
printf("In what matrix would you like to store the resulting matrix of multiplying matrices %s and %s?\n",
(d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = multiplyMatrices(matrices[d], matrices[f]);
break;
case DIVIDE_M:
printf("Which two matrices would you like to divide?\n");
printf("1st matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &d);
}while(d < 0 || d > 2);
printf("2nd matrix\n");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &f);
}while(f < 0 || f > 2);
printf("In what matrix would you like to store the resulting matrix of dividing matrices %s and %s?\n",
(d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
do{
printf("Matrix A (0), B (1) or C (2)? ");
scanf("%d", &s);
}while(s < 0 || s > 2);
matrices[s] = divideMatrices(matrices[d], matrices[f]);
break;
}
printf("Matrix %s:\n", (s==0)?"A":(s==1)?"B":"C");
displayMatrix(matrices[s]);
printf("\n");
}
int main(){
for(int i = 0; i < 3; i++)
matrices[i] = createMatrix(1, 1);
int input = 0;
do{
input = handleInput();
matrixCalculator(input);
}while(input);
return 0;
}