Update cv2/program.c
This commit is contained in:
		
							parent
							
								
									66a0c2f49f
								
							
						
					
					
						commit
						891d771332
					
				@ -0,0 +1,73 @@
 | 
				
			|||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <ctype.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define LINESIZE 100
 | 
				
			||||||
 | 
					#define MAX_ITEMS 100  // Максимальна кількість елементів меню
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct MenuItem {
 | 
				
			||||||
 | 
					    char name[LINESIZE];           
 | 
				
			||||||
 | 
					    float price;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Функція порівняння для сортування
 | 
				
			||||||
 | 
					int compare_menu_items(const void* a, const void* b) {
 | 
				
			||||||
 | 
					    struct MenuItem* item1 = (struct MenuItem*)a;
 | 
				
			||||||
 | 
					    struct MenuItem* item2 = (struct MenuItem*)b;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (item1->price != item2->price) {
 | 
				
			||||||
 | 
					        return (item1->price > item2->price) - (item1->price < item2->price);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return strcmp(item1->name, item2->name);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Функція для видалення символу нового рядка
 | 
				
			||||||
 | 
					void remove_newline(char* str) {
 | 
				
			||||||
 | 
					    str[strcspn(str, "\n")] = '\0';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Читання одного елемента меню
 | 
				
			||||||
 | 
					int read_menu_item(struct MenuItem* item) {
 | 
				
			||||||
 | 
					    if (fgets(item->name, LINESIZE, stdin) == NULL) {
 | 
				
			||||||
 | 
					        return 0;  // Кінець вводу
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    remove_newline(item->name);  // Видаляємо символ нового рядка
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Перевірка на кінець введення (наприклад, "end")
 | 
				
			||||||
 | 
					    if (strcmp(item->name, "end") == 0) {
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Читання ціни
 | 
				
			||||||
 | 
					    if (scanf("%f", &item->price) != 1) {
 | 
				
			||||||
 | 
					        return 0;  // Неправильний формат
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    getchar();  // Споживаємо символ нового рядка після ціни
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 1;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(void) {
 | 
				
			||||||
 | 
					    struct MenuItem menu[MAX_ITEMS];
 | 
				
			||||||
 | 
					    int item_count = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("Zadaj jedalny listok:\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Читання елементів меню
 | 
				
			||||||
 | 
					    while (item_count < MAX_ITEMS && read_menu_item(&menu[item_count])) {
 | 
				
			||||||
 | 
					        item_count++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Сортування меню
 | 
				
			||||||
 | 
					    qsort(menu, item_count, sizeof(struct MenuItem), compare_menu_items);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Виведення відсортованого меню
 | 
				
			||||||
 | 
					    for (int i = 0; i < item_count; i++) {
 | 
				
			||||||
 | 
					        printf("%s\n", menu[i].name);
 | 
				
			||||||
 | 
					        printf("%.6f\n", menu[i].price);  // Виведення ціни з 6 знаками після коми
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user