-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomenzi.c
298 lines (240 loc) · 5.96 KB
/
comenzi.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
// Copyright Sima Alexandru 312CA 2022-2023
#include <stdio.h>
#include <stdlib.h>
#include "alocari.h"
#include "comenzi.h"
#include "operatii.h"
#include "strassen.h"
// Verifica daca indicele `i` este valid (apartine `[0, n)`).
static inline int e_index_valid(int i, int n);
// Citeste un index de la tastatura. Afiseaza un mesaj de
// eroare si returneaza -1 daca nu este valid.
static int citire_index(int n);
// Citeste 2 indecsi de matrice de la tastatura, apoi verifica daca sunt valizi
// si daca cele 2 matrice pot fi inmultite (nr. de linii al primeia = nr. de
// coloane ale celei de a 2-a).
static int citire_matrice_produs(int *col, int *lin, int n, int *a, int *b);
// Aloca 2 vectori si citeste in ei liniile si coloanele pentru operatia de
// redimensionare. Returneaza 1 daca intervine o eroare.
static int citire_indecsi_redimensionare(int **lin, int *nr_lin, int **col,
int *nr_col);
// Interschimba 2 numere.
static void interschimba(int *a, int *b);
int comanda_adaugare(int ****mat, int **lin, int **col, int *nr)
{
int n, m;
scanf("%d%d", &n, &m);
int **a = citire_matrice(n, m);
if (!a)
return 1;
int ***mat_nou = inserare_mat(*mat, lin, col, nr, a, n, m);
if (!mat_nou)
return 1;
*mat = mat_nou;
return 0;
}
void comanda_dimensiuni(int *lin, int *col, int nr)
{
int index = citire_index(nr);
if (index != -1)
printf("%d %d\n", lin[index], col[index]);
}
void comanda_afisare(int ***mat, int *lin, int *col, int nr)
{
int index = citire_index(nr);
if (index != -1)
printare_matrice(mat[index], lin[index], col[index]);
}
int comanda_redimensionare(int ***matrice, int *lin, int *col, int nr)
{
int *linii;
int *coloane;
int l, c;
int index;
index = citire_index(nr);
if (citire_indecsi_redimensionare(&linii, &l, &coloane, &c))
return 1;
if (index == -1) {
free(linii);
free(coloane);
return 0;
}
int **aux = partitionare_matrice(matrice[index], linii, coloane, l, c);
if (!aux) {
free(linii);
free(coloane);
return 1;
}
eliberare_matrice(matrice[index], lin[index]);
matrice[index] = aux;
lin[index] = l;
col[index] = c;
free(linii);
free(coloane);
return 0;
}
int comanda_produs(int ****mat, int **lin, int **col, int *nr)
{
int x, y;
if (!citire_matrice_produs(*col, *lin, *nr, &x, &y))
return 0;
int **c =
prod_matrice((*mat)[x], (*mat)[y], (*lin)[x], (*col)[x], (*col)[y]);
if (!c)
return 1;
int ***mat_nou = inserare_mat(*mat, lin, col, nr, c, (*lin)[x], (*col)[y]);
if (!mat_nou)
return 1;
*mat = mat_nou;
return 0;
}
int comanda_sortare(int ***mat, int *lin, int *col, int nr)
{
int *sume = (int *)calloc(nr, sizeof(int));
if (!sume)
return 1;
for (int i = 0; i < nr; ++i)
sume[i] = insumare_elemente(mat[i], lin[i], col[i]);
for (int i = 0; i < nr - 1; ++i) {
for (int j = i + 1; j < nr; ++j) {
if (sume[i] > sume[j]) {
interschimba(&lin[i], &lin[j]);
interschimba(&col[i], &col[j]);
interschimba(&sume[i], &sume[j]);
int **aux = mat[i];
mat[i] = mat[j];
mat[j] = aux;
}
}
}
free(sume);
return 0;
}
int comanda_transpunere(int ***mat, int *lin, int *col, int nr)
{
int index = citire_index(nr);
if (index == -1)
return 0;
int n = lin[index];
int m = col[index];
int **transpusa = alocare_matrice(m, n);
if (!transpusa)
return 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
transpusa[j][i] = mat[index][i][j];
}
interschimba(&lin[index], &col[index]);
eliberare_matrice(mat[index], n);
mat[index] = transpusa;
return 0;
}
int comanda_putere(int ***mat, int *lin, int *col, int nr)
{
int index, exp;
index = citire_index(nr);
scanf("%d", &exp);
if (index == -1)
return 0;
if (exp < 0) {
printf("Power should be positive\n");
return 0;
}
// Matricea trebuie sa fie patratica.
if (lin[index] != col[index]) {
printf("Cannot perform matrix multiplication\n");
return 0;
}
int **rez = putere_matrice(mat[index], lin[index], exp);
if (!rez)
return 1;
mat[index] = rez;
return 0;
}
int comanda_stergere(int ****mat, int **lin, int **col, int *nr)
{
int index = citire_index(*nr);
if (index == -1)
return 0;
eliberare_matrice((*mat)[index], (*lin)[index]);
for (int i = index; i < *nr - 1; ++i) {
(*mat)[i] = (*mat)[i + 1];
(*lin)[i] = (*lin)[i + 1];
(*col)[i] = (*col)[i + 1];
}
int ***mat_nou = stergere_mat(*mat, lin, col, nr);
// Daca nr = 1, `mat`, `lin` si `col` vor fi toti dealocati
// si cu valoarea NULL, care nu este insa o eroare.
if (!mat_nou && *nr == 1)
return 1;
*mat = mat_nou;
return 0;
}
int comanda_prod_strassen(int ****mat, int **lin, int **col, int *nr)
{
int x, y;
if (!citire_matrice_produs(*col, *lin, *nr, &x, &y))
return 0;
int **rez = prod_strassen((*mat)[x], (*mat)[y], (*lin)[x]);
if (!rez)
return 1;
int ***mat_nou =
inserare_mat(*mat, lin, col, nr, rez, (*lin)[x], (*lin)[x]);
if (!mat_nou)
return 1;
*mat = mat_nou;
return 0;
}
static inline int e_index_valid(int i, int n)
{
return i >= 0 && i < n;
}
static int citire_index(int n)
{
int index;
scanf("%d", &index);
if (e_index_valid(index, n))
return index;
printf("No matrix with the given index\n");
return -1;
}
static int citire_matrice_produs(int *col, int *lin, int n, int *a, int *b)
{
scanf("%d%d", a, b);
if (!(e_index_valid(*a, n) && e_index_valid(*b, n))) {
printf("No matrix with the given index\n");
return 0;
}
if (col[*a] != lin[*b]) {
printf("Cannot perform matrix multiplication\n");
return 0;
}
return 1;
}
static int citire_indecsi_redimensionare(int **lin, int *nr_lin, int **col,
int *nr_col)
{
scanf("%d", nr_lin);
int *linii = (int *)malloc(*nr_lin * sizeof(int));
if (!linii)
return 1;
for (int i = 0; i < *nr_lin; ++i)
scanf("%d", &linii[i]);
scanf("%d", nr_col);
int *coloane = (int *)malloc(*nr_col * sizeof(int));
if (!coloane) {
free(linii);
return 1;
}
for (int i = 0; i < *nr_col; ++i)
scanf("%d", &coloane[i]);
*lin = linii;
*col = coloane;
return 0;
}
static void interschimba(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}