-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
462 lines (368 loc) · 13.4 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
//Book implementation
struct Book{
char title[60];//Title of book
char author[50];//Name of author
int year;//Year of publishing
float book_rate;//Book rate
struct Book *nextbook;//Pointer to next book
};
//Customer implementation
typedef struct s{
char name[50];//Name of customer
int age;//Age of customer
float customer_rate;//Customer rate
struct s *nextcustomer;//Pointer to next customer
} customer;
struct Book *first_book = NULL; //First book pointer
customer *first_customer = NULL; //First customer pointer
//Print menu in console
void PrintMenu(){
printf("Commands: 1 - Show books\n");
printf(" 2 - Add book\n");
printf(" 3 - Update book\n");
printf(" 4 - Remove book\n");
printf(" 5 - Show customers\n");
printf(" 6 - Add customer\n");
printf(" 7 - Update customer\n");
printf(" 8 - Remove customer\n");
printf(" 9 - Exit\n");
printf("Enter command: ");
}
//Free input buffer (for using fgets without bugs)
void FreeBuffer(){
int c;
do{
c = getchar();
}while(c != EOF && c != '\n');
}
//Print string
void PrintString(char *str, int N){
for (int i = 0; i < N; i++)
if (str[i] != '\0' && str[i] != '\n')
printf("%c", str[i]);
else
break;
}
//Compare strings
int CompareStrings(char *str1, char *str2, int N1, int N2){
int N = 0;
if (N1 < N2)
N = N1;
else
N = N2;
int i = 0;
while(str1[i] != '\0' && str2[i] != '\0' && i < N){
if (str1[i] != str2[i])
return 0;
i++;
}
return 1;
}
//Add book to collection by title, author, book rate and year
void AddBook(){
struct Book *new_book = first_book;
struct Book *last_book = NULL;
//Search of last book
while (new_book != NULL){
last_book = new_book;
new_book = new_book->nextbook;
}
new_book = malloc(sizeof(struct Book));//Memory allocation for new book
if (last_book != NULL)
last_book->nextbook = new_book;//Last book contains pointer to next one
else
first_book = new_book;//Or new book is first one
FreeBuffer();//Necessary command
printf("Title: ");
fgets(new_book->title, 60, stdin);//Initialize title of book
printf("Author: ");
fgets(new_book->author, 50, stdin);//Initialize author of book
printf("Year: ");
scanf("%d", &(new_book->year));//Initialize year of book
printf("Book rate: ");
scanf("%f", &(new_book->book_rate));//Initialize rate of book
new_book->nextbook = NULL;//Pointer to next book is null because next book does not exist
printf("Book has been added.\n");
};
//Show all books
void ShowBooks(){
struct Book *current_book = first_book;
//Print books until pointer to new one is not empty
while (current_book != NULL){
printf("Title: ");
PrintString(current_book->title, sizeof(current_book->title));
printf(", Author: ");
PrintString(current_book->author, sizeof(current_book->author));
printf(", Year: ");
printf("%d, Rate: %f\n", current_book->year, current_book->book_rate);
current_book = current_book->nextbook;
}
//If collection of book is empty
if (first_book != NULL)
printf("End of list.\n");
else
printf("Empty list.\n");
}
//Update book given the title
void UpdateBook(){
FreeBuffer();//Necessary command
char title[60];//Title of necessary book
printf("Enter title of the book: ");
fgets(title, 60, stdin);//User enters title
struct Book *current_book = first_book;
//Search book until they are not over or coincidence is not found
while (current_book != NULL){
if (CompareStrings(current_book->title, title, 60, 60))
break;
current_book = current_book->nextbook;
}
if (current_book != NULL){//If book is found
printf("Book is found.\n");
int command = 0;//Pointer of current command
while (command != 5){//Loop for action
printf("You can change: 1 - Update title\n");
printf(" 2 - Update author\n");
printf(" 3 - Update year\n");
printf(" 4 - Update book rate\n");
printf(" 5 - Exit\n");
printf("Enter command: ");
scanf("%d", &command);
FreeBuffer();
switch (command){
case 1:{
printf("New title: ");
fgets(current_book->title, 60, stdin);//Rename book
break;
}
case 2:{
printf("New author: ");
fgets(current_book->author, 60, stdin);//Change author of book
break;
}
case 3:{
printf("New year: ");
scanf("%d", &(current_book->year));//Change year of book
break;
}
case 4:{
printf("New book rate: ");
scanf("%f", &(current_book->book_rate));//Change book rate
break;
}
case 5:
printf("Enter to menu.\n");//Entering to main menu
break;
default:
printf("Command not found.\n");//If command is not found
break;
}
}
}
else
printf("Book is not found\n");
}
//Remove book from collection by title
void RemoveBook(){
FreeBuffer();//Necessary command
char title[60];//Title of necessary book
printf("Enter title of the book: ");
fgets(title, 60, stdin);//User enters title
struct Book *current_book = first_book;
struct Book *previous_book = first_book;
//Search book until they are not over or coincidence is not found
while (current_book != NULL){
if (CompareStrings(current_book->title, title, 60, 60))
break;
previous_book = current_book;
current_book = current_book->nextbook;
}
if (current_book != NULL){//If book is found
previous_book->nextbook = current_book->nextbook;//Previous book must contain new pointer to other book
//free(current_book->title);
//free(current_book->author);
free(current_book);//Free memory for removable book
if (first_book == current_book && current_book->nextbook == NULL)
first_book = NULL;//If removable book is last
else if (first_book == current_book && current_book->nextbook != NULL){
first_book = current_book->nextbook;//If removable book is first in list of ones
}
printf("Book has been removed\n");
}
else
printf("Book is not found\n");
}
//Add customer to collection by name, age and customer rate
void AddCustomer(){
customer *new_customer = first_customer;
customer *last_customer = NULL;
//Search of last CUSTOMER
while (new_customer != NULL){
last_customer = new_customer;
new_customer = new_customer->nextcustomer;
}
new_customer = malloc(sizeof(customer));//Memory allocation for new customer
if (last_customer != NULL)
last_customer->nextcustomer = new_customer;//Last customer contains pointer to next one
else
first_customer = new_customer;//Or new customer is first one
FreeBuffer();//Necessary command
printf("Name: ");
fgets(new_customer->name, 50, stdin);//Set name of customer
printf("Age: ");
scanf("%d", &(new_customer->age));//Set age of customer
printf("Customer rate: ");
scanf("%f", &(new_customer->customer_rate));//Set customer rate
new_customer->nextcustomer = NULL;//Next customer does not exist yet
printf("Customer has been added.\n");
}
//Show all customers
void ShowCustomers(){
customer *current_customer = first_customer;
//Print customers until pointer to new one is not empty
while (current_customer != NULL){
printf("Name: ");
PrintString(current_customer->name, sizeof(current_customer->name));
printf(", Age: ");
printf("%d, Rate: %f\n", current_customer->age, current_customer->customer_rate);
current_customer = current_customer->nextcustomer;
}
//If collection of customers is empty
if (first_customer != NULL)
printf("End of list.\n");
else
printf("Empty list.\n");
}
//Update customer given the name
void UpdateCustomer(){
FreeBuffer();//Necessary command
char name[60];//Name of necessary customer
printf("Enter name of the customer: ");
fgets(name, 60, stdin);//User enters name
customer *current_customer = first_customer;
//Search customer until they are not over or coincidence is not found
while (current_customer != NULL){
if (CompareStrings(current_customer->name, name, sizeof(current_customer->name), sizeof(name)))
break;
current_customer = current_customer->nextcustomer;
}
if (current_customer != NULL){//If book is found
printf("Customer is found.\n");
int command = 0;//Pointer to current command
while (command != 4){
printf("You can change: 1 - Update name\n");
printf(" 2 - Update age\n");
printf(" 3 - Update customer rate\n");
printf(" 4 - Exit\n");
printf("Enter command: ");
scanf("%d", &command);
FreeBuffer();
switch (command){
case 1:{
printf("New name: ");
fgets(current_customer->name, 60, stdin);//Change name of customer
break;
}
case 2:{
printf("New age: ");
scanf("%d", &(current_customer->age));//Change age of customer
break;
}
case 3:{
printf("New customer rate: ");
scanf("%f", &(current_customer->customer_rate));//Change rate of customer
break;
}
case 4:
printf("Enter to menu.\n");//Entering to main menu
break;
default:
printf("Command not found.\n");//If command is not found
break;
}
}
}
else
printf("Customer is not found\n");//Customer is not found
}
//Remove customer from collection by name
void RemoveCustomer(){
FreeBuffer();//Necessary command
char name[60];//Name of necessary customer
printf("Enter name of the customer: ");
fgets(name, 60, stdin);//User enters name
customer *current_customer = first_customer;
customer *previous_customer = first_customer;
//Search customer until they are not over or coincidence is not found
while (current_customer != NULL){
if (CompareStrings(current_customer->name, name, sizeof(current_customer->name), sizeof(name)))
break;
previous_customer = current_customer;
current_customer = current_customer->nextcustomer;
}
if (current_customer != NULL){//If customer is found
previous_customer->nextcustomer = current_customer->nextcustomer;//Previous customer must contain new pointer to other customer
//free(current_book->title);
//free(current_book->author);
free(current_customer);
if (first_customer == current_customer && current_customer->nextcustomer == NULL)
first_customer = NULL;//If removable customer is last
else if (first_customer == current_customer && current_customer->nextcustomer != NULL){
first_customer = current_customer->nextcustomer;//If removable customer is first in list of ones
}
printf("Customer has been removed\n");
}
else
printf("Customer is not found\n");//If customer is not found
}
int main()
{
auto int command = 0;//Command pointer
//main loop
while (command != 9){
PrintMenu();
scanf("%d", &command); //Get current command
switch (command){
case 1:{//Show books
ShowBooks();
break;
}
case 2:{//Add book
AddBook();
break;
}
case 3:{//Update book
UpdateBook();
break;
}
case 4:{//Remove book
RemoveBook();
break;
}
case 5:{//Show customers
ShowCustomers();
break;
}
case 6:{//Add customer
AddCustomer();
break;
}
case 7:{//Update customer
UpdateCustomer();
break;
}
case 8:{//Remove customer
RemoveCustomer();
break;
}
case 9://Command for termination process
printf("End of session.\n");
break;
default://If command is not found
printf("Command is not found.\n");
break;
}
}
return 0;
}