Update cv1/program.c
This commit is contained in:
		
							parent
							
								
									8f48b51943
								
							
						
					
					
						commit
						968a479681
					
				@ -3,17 +3,17 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
 | 
			
		||||
#define BUFFER_SIZE 100
 | 
			
		||||
#define MAX_ITEMS 100
 | 
			
		||||
#define BUFFER_SIZE 120
 | 
			
		||||
#define MAX_PIZZAS 120
 | 
			
		||||
 | 
			
		||||
struct FoodItem {
 | 
			
		||||
    float price;
 | 
			
		||||
struct pizza {
 | 
			
		||||
    float cost;
 | 
			
		||||
    char name[BUFFER_SIZE];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Převod znaku na Leetspeak
 | 
			
		||||
char to_leetspeak(char ch) {
 | 
			
		||||
    switch (tolower(ch)) {
 | 
			
		||||
 | 
			
		||||
char to_leetspeak(char chi) {
 | 
			
		||||
    switch (tolower(chi)) {
 | 
			
		||||
        case 'o': return '0';
 | 
			
		||||
        case 'i': return '1';
 | 
			
		||||
        case 'z': return '2';
 | 
			
		||||
@ -22,23 +22,23 @@ char to_leetspeak(char ch) {
 | 
			
		||||
        case 's': return '5';
 | 
			
		||||
        case 't': return '7';
 | 
			
		||||
        case 'b': return '8';
 | 
			
		||||
        default: return tolower(ch);
 | 
			
		||||
        default: return tolower(chi);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Převod řetězce na Leetspeak
 | 
			
		||||
// Преобразование строки в Leetspeak
 | 
			
		||||
void apply_leetspeak(char *input) {
 | 
			
		||||
    for (int i = 0; input[i] != '\0'; i++) {
 | 
			
		||||
        input[i] = to_leetspeak(input[i]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Kontrola existence podřetězce ve jménu
 | 
			
		||||
int contains_substring(const char *item_name, const char *keyword) {
 | 
			
		||||
// Проверка на наличие подстроки в строке
 | 
			
		||||
int has_substring(const char *name, const char *keyword) {
 | 
			
		||||
    char modified_name[BUFFER_SIZE];
 | 
			
		||||
    char modified_keyword[BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
    strncpy(modified_name, item_name, BUFFER_SIZE);
 | 
			
		||||
    strncpy(modified_name, name, BUFFER_SIZE);
 | 
			
		||||
    strncpy(modified_keyword, keyword, BUFFER_SIZE);
 | 
			
		||||
 | 
			
		||||
    apply_leetspeak(modified_name);
 | 
			
		||||
@ -47,57 +47,56 @@ int contains_substring(const char *item_name, const char *keyword) {
 | 
			
		||||
    return strstr(modified_name, modified_keyword) != NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Funkce pro načtení položky
 | 
			
		||||
int load_food_item(struct FoodItem* item) {
 | 
			
		||||
 | 
			
		||||
int load_pizza(struct pizza* p) {
 | 
			
		||||
    char temp_price[BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
    // Čtení názvu položky
 | 
			
		||||
    if (!fgets(item->name, BUFFER_SIZE, stdin)) {
 | 
			
		||||
        return 0;  // Chyba při čtení nebo konec vstupu
 | 
			
		||||
    
 | 
			
		||||
    if (!fgets(p->name, BUFFER_SIZE, stdin)) {
 | 
			
		||||
        return 0; 
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Odstranění znaku nového řádku
 | 
			
		||||
    item->name[strcspn(item->name, "\n")] = '\0';
 | 
			
		||||
    
 | 
			
		||||
    // Čtení ceny položky
 | 
			
		||||
    p->name[strcspn(p->name, "\n")] = '\0';
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    if (!fgets(temp_price, BUFFER_SIZE, stdin)) {
 | 
			
		||||
        return 0;  // Chyba při čtení nebo konec vstupu
 | 
			
		||||
        return 0;  
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Převod řetězce na číslo s plovoucí desetinnou čárkou
 | 
			
		||||
    item->price = strtof(temp_price, NULL);
 | 
			
		||||
    return item->price > 0 || strcmp(temp_price, "0.0") == 0;
 | 
			
		||||
	
 | 
			
		||||
    p->cost = strtof(temp_price, NULL);
 | 
			
		||||
    return p->cost > 0 || strcmp(temp_price, "0.0") == 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    char search_keyword[BUFFER_SIZE];
 | 
			
		||||
    struct FoodItem menu_items[MAX_ITEMS];
 | 
			
		||||
    int item_count = 0;
 | 
			
		||||
    char query[BUFFER_SIZE];
 | 
			
		||||
    struct pizza pizzas[MAX_PIZZAS];
 | 
			
		||||
    int pizza_count = 0;
 | 
			
		||||
    
 | 
			
		||||
    // Vstup hledaného klíčového slova
 | 
			
		||||
    printf("Zadaj hladanu surovinu:\n");
 | 
			
		||||
    fgets(search_keyword, BUFFER_SIZE, stdin);
 | 
			
		||||
    search_keyword[strcspn(search_keyword, "\n")] = '\0';  // Odstranění znaku nového řádku
 | 
			
		||||
    fgets(query, BUFFER_SIZE, stdin);
 | 
			
		||||
    query[strcspn(query, "\n")] = '\0';  // Удаление символа новой строки
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // Vstup dat o položkách
 | 
			
		||||
    printf("Zadaj jedalny listok:\n");
 | 
			
		||||
    while (item_count < MAX_ITEMS && load_food_item(&menu_items[item_count])) {
 | 
			
		||||
        item_count++;
 | 
			
		||||
    while (pizza_count < MAX_PIZZAS && load_pizza(&pizzas[pizza_count])) {
 | 
			
		||||
        pizza_count++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int match_count = 0;
 | 
			
		||||
 | 
			
		||||
    // Hledání položek, které odpovídají hledanému klíčovému slovu
 | 
			
		||||
    for (int i = 0; i < item_count; i++) {
 | 
			
		||||
        if (contains_substring(menu_items[i].name, search_keyword) || contains_substring(menu_items[i].name, "KO")) {
 | 
			
		||||
            printf("%s\n", menu_items[i].name);
 | 
			
		||||
            printf("%.2f\n", menu_items[i].price);
 | 
			
		||||
    
 | 
			
		||||
    for (int i = 0; i < pizza_count; i++) {
 | 
			
		||||
        if (has_substring(pizzas[i].name, query)) {
 | 
			
		||||
            printf("%s\n", pizzas[i].name);
 | 
			
		||||
            printf("%.2f\n", pizzas[i].cost);
 | 
			
		||||
            match_count++;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Výstup celkového počtu načtených položek
 | 
			
		||||
    printf("Nacitanych %d poloziek.\n", item_count);
 | 
			
		||||
 
 | 
			
		||||
    printf("Nacitanych %d poloziek.\n", pizza_count);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user