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
| #include <stdio.h>
#include <locale.h>
#include <malloc.h>
#include <windows.h>
#define _HUGE_ENUF 1e+300
#define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
#define MAXLINE 1024
#define name_sort_type 1
#define category_sort_type 2
#define forever for(;;)
struct Product
{
char name[MAXLINE];
char category[MAXLINE];
float price;
};
struct Store
{
struct Product* products;
int was_changed;
};
void store_initialize(const char* name_of_file, struct Store* S, int* size);
void menu(struct Store* S, int* size);
void add_product_to_store(struct Store* S, int* size);
void quickSort(struct Product* data, int const len, char sort_type);
int* find_product(char* name, struct Store* S, int size);
int choose_product(char* name, struct Store* S, int size);
void edit_product_in_store(struct Store* S, int* size);
void delete_product_from_store(struct Store* S, int* size);
void database_content_output(struct Store* S, int* size);
void Selection_of_the_minimum_basket(struct Store* S, int* size);
void save(const char* name_of_file, struct Store* S, int* size);
int main()
{
setlocale(LC_ALL, "RUS");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
struct Store S;
int size;
store_initialize("base.txt", &S, &size);
menu(&S, &size);
save("base.txt", &S, &size);
if (S.products) free(S.products);
return 0;
}
void store_initialize(const char* name_of_file, struct Store* S, int* size)
{
S->products = NULL;
S->was_changed = 0;
FILE* fpin;
fopen_s(&fpin, name_of_file, "rt");// открыть файл для чтения
if (fpin == NULL)
return; // ошибка при открытии файла
fscanf_s(fpin, "%d", size);
S->products = (struct Product*)malloc((*size) * sizeof(struct Product));
if (!S->products)
*size = 0;
for (int i = 0; i < *size; i++)
{
fgets(S->products[i].name, MAXLINE, fpin);
fgets(S->products[i].name, MAXLINE, fpin);
S->products[i].name[strlen(S->products[i].name) - 1] = '\0';
fgets(S->products[i].category, MAXLINE, fpin);
S->products[i].category[strlen(S->products[i].category) - 1] = '\0';
fscanf_s(fpin, "%f", &S->products[i].price);
}
fclose(fpin); // закрыть входной файл
}
void menu(struct Store* S, int* size)
{
int m = -1;
forever
{
printf("Меню\n 1. Пополнение базы\n 2. Редактирование базы\n 3. Удаление записей\n");
printf(" 4. Вывод содержимого базы\n 5. Подбор минимальной корзины\n 0. Выход\n-> ");
scanf_s("%d", &m);
switch (m)
{
case 1: add_product_to_store(S, size); break;
case 2: edit_product_in_store(S, size); break;
case 3: delete_product_from_store(S, size); break;
case 4: database_content_output(S, size); break;
case 5: Selection_of_the_minimum_basket(S, size); break;
case 0: return;
default: printf("выбран не существующий пункт меню");
}
}
}
void add_product_to_store(struct Store* S, int* size)
{
S->products = (struct Product*)realloc(S->products, ((*size) + 1) * sizeof(struct Product));
printf("\tВведите название товара: ");
gets_s(S->products[*size].name, MAXLINE);
gets_s(S->products[*size].name, MAXLINE);
printf("\tВведите категорию товара: ");
gets_s(S->products[*size].category, MAXLINE);
printf("\tВведите цену товара: ");
scanf_s("%f", &S->products[*size].price);
(*size)++;
S->was_changed = 1;
}
void quickSort(struct Product* data, int const len, char sort_type)
{
int const lenD = len;
struct Product pivot;
int ind = lenD / 2;
int i, j = 0, k = 0;
if (lenD > 1)
{
struct Product* L = (struct Product*)malloc(lenD * sizeof(struct Product));
struct Product* R = (struct Product*)malloc(lenD * sizeof(struct Product));
pivot = data[ind];
for (i = 0; i < lenD; i++)
if (i != ind)
{
if (sort_type == 1 ? strcmp(data[i].name, pivot.name) < 0 : strcmp(data[i].category, pivot.category) < 0)
{
L[j] = data[i];
j++;
}
else
{
R[k] = data[i];
k++;
}
}
quickSort(L, j, sort_type);
quickSort(R, k, sort_type);
for (int cnt = 0; cnt < lenD; cnt++)
{
if (cnt < j)
data[cnt] = L[cnt];
else if (cnt == j)
data[cnt] = pivot;
else
data[cnt] = R[cnt - (j + 1)];
}
free(L);
free(R);
}
}
int* find_product(char* name, struct Store* S, int size)
{
int i_size = 5, i_i = 0;
int* indexes = (int*)malloc(i_size * sizeof(int));
for (int k = 0; k < i_size; k++)
indexes[k] = -1;
quickSort(S->products, size, name_sort_type);
for (int i = 0; i < size; i++)
{
if (!strcmp(S->products[i].name, name))
{
if (i_size == i_i)
{
indexes = (int*)realloc(indexes, (i_size += 5) * sizeof(int));
for (int k = 0; k < 5; k++)
indexes[i_size + k] = -1;
}
indexes[i_i] = i;
i_i++;
}
}
return indexes;
}
int choose_product(char* name, struct Store* S, int size)
{
int* indexes = find_product(name, S, size);
int index;
if (indexes[0] == -1)
{
printf("\t\tНебыло найдено товаров по вашему запросу.\n");
free(indexes);
return -1;
}
printf("\t\tБыли найдены следующие товары:\n");
for (int i = 0; indexes[i] != -1; i++)
{
printf("\t\t %d. %s - %s : $%f\n",
i + 1,
S->products[indexes[i]].name,
S->products[indexes[i]].category,
S->products[indexes[i]].price);
}
printf("\n\t\tВыберите номер товара:\n\t\t-> ");
scanf_s("%d", &index);
index = indexes[index - 1];
free(indexes);
return index;
}
void edit_product_in_store(struct Store* S, int* size)
{
char name[MAXLINE];
printf("\tВведите название товара: ");
gets_s(name, MAXLINE);
gets_s(name, MAXLINE);
int index = choose_product(name, S, *size);
if (index == -1) return;
printf("\tВведите название товара: ");
gets_s(S->products[index].name, MAXLINE);
gets_s(S->products[index].name, MAXLINE);
printf("\tВведите категорию товара: ");
gets_s(S->products[index].category, MAXLINE);
printf("\tВведите цену товара: ");
scanf_s("%f", &S->products[index].price);
S->was_changed = 1;
}
void delete_product_from_store(struct Store* S, int* size)
{
char name[MAXLINE];
printf("\tВведите название товара: ");
gets_s(name, MAXLINE);
gets_s(name, MAXLINE);
int index = choose_product(name, S, *size);
int i_i = 0;
struct Product* P = (struct Product*)malloc(((*size) - 1) * sizeof(struct Product));
for (int i = 0; i < *size; i++)
{
if (i == index) continue;
P[i_i] = S->products[i];
i_i++;
}
(*size)--;
free(S->products);
S->products = P;
}
void database_content_output(struct Store* S, int* size)
{
int m = -1;
while (m < 1 || m>2)
{
printf("\tВыберите вывод содержимого базы\n\t 1. по названию товара\n\t 2. по категориям товара\n\t-> ");
scanf_s("%d", &m);
switch (m)
{
case 1: quickSort(S->products, *size, name_sort_type); break;
case 2: quickSort(S->products, *size, category_sort_type); break;
default: printf("\tвыбран не существующий пункт меню"); break;
}
}
for (int i = 0; i < *size; i++)
{
printf("\t\t %d. %s - %s : $%f\n",
i + 1,
S->products[i].name,
S->products[i].category,
S->products[i].price);
}
}
void Selection_of_the_minimum_basket(struct Store* S, int* size)
{
char name[MAXLINE];
gets_s(name, MAXLINE);
float min_price;
int min_i;
int* indexes = NULL;
int basket_size = 1, basket_i = 0;
struct Product* basket = (struct Product*)malloc(basket_size * sizeof(struct Product));
printf("\tДля выхода введите '0'\n");
forever
{
printf("\tВведите название товара: ");
gets_s(name, MAXLINE);
if (!strcmp(name, "0")) break;
indexes = find_product(name, S, *size);
if (indexes[0] == -1)
printf("\t\tНебыло найдено товаров по вашему запросу.\n");
else
{
min_price = INFINITY;
min_i = 0;
for (int i = 0; indexes[i] != -1; i++)
{
if (min_price > S->products[indexes[i]].price)
{
min_price = S->products[indexes[i]].price;
min_i = indexes[i];
}
}
if (basket_size == basket_i)
{
basket = (struct Product*)realloc(basket, ++basket_size * sizeof(struct Product));
}
strcpy_s(basket[basket_i].name, MAXLINE, S->products[min_i].name);
strcpy_s(basket[basket_i].category, MAXLINE, S->products[min_i].category);
basket[basket_i].price = S->products[min_i].price;
basket_i++;
}
}
quickSort(basket, basket_size, name_sort_type);
printf("\n\t\tКорзина:\n");
for (size_t i = 0; i < basket_size; i++)
{
printf("\t\t %d. %s - %s : $%f\n",
i + 1,
basket[i].name,
basket[i].category,
basket[i].price);
}
}
void save(const char* name_of_file, struct Store* S, int* size)
{
if (S->was_changed)
{
FILE* fpin;
fopen_s(&fpin, name_of_file, "wt");// открыть файл для чтения
if (fpin == NULL)
return; // ошибка при открытии файла
fprintf(fpin, "%d\n", *size);
for (int i = 0; i < *size; i++)
fprintf(fpin, "%s\n%s\n%f\n", S->products[i].name, S->products[i].category, S->products[i].price);
fclose(fpin);
}
S->was_changed = 0;
} |