Update cv1/program.c
This commit is contained in:
parent
f5d8c7855b
commit
b47fc10664
144
cv1/program.c
144
cv1/program.c
@ -1,96 +1,116 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#define LINESIZE 100
|
#define LINESIZE 100
|
||||||
#define MENU_SIZE 100
|
#define MAX_PIZZAS 100
|
||||||
|
|
||||||
|
// Structure to store pizza menu item
|
||||||
struct pizza {
|
struct pizza {
|
||||||
float prize;
|
float price;
|
||||||
char name[LINESIZE];
|
char name[LINESIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to convert a character to its Hacker Script form
|
||||||
char hacker_script(char c) {
|
char hacker_script(char c) {
|
||||||
if (c >= 'A' && c <= 'Z') c += 32; // Преобразование к нижнему регистру
|
char numbers[] = "0123456789";
|
||||||
switch (c) {
|
char letters[] = "oizeasbtbq";
|
||||||
case 'o': return '0';
|
|
||||||
case 'i': return '1';
|
// Convert to lowercase first
|
||||||
case 'z': return '2';
|
c = tolower(c);
|
||||||
case 'e': return '3';
|
|
||||||
case 'a': return '4';
|
// Check for special characters
|
||||||
case 's': return '5';
|
for (int i = 0; i < 10; i++) {
|
||||||
case 'b': return '6';
|
if (c == numbers[i]) {
|
||||||
case 't': return '7';
|
return letters[i];
|
||||||
case '8': return '8';
|
}
|
||||||
case 'q': return '9';
|
}
|
||||||
default: return c;
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to normalize a string by applying hacker script rules
|
||||||
|
void normalize_string(char *str) {
|
||||||
|
for (int i = 0; str[i]; i++) {
|
||||||
|
str[i] = hacker_script(str[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void normalize_string(const char* input, char* output) {
|
// Function to read one pizza item from input
|
||||||
int i = 0;
|
int read_pizza(struct pizza *item) {
|
||||||
while (input[i] != '\0' && input[i] != '\n') { // Убираем '\n'
|
char line1[LINESIZE], line2[LINESIZE];
|
||||||
output[i] = hacker_script(input[i]);
|
|
||||||
i++;
|
// Read name
|
||||||
|
if (fgets(line1, LINESIZE, stdin) == NULL) {
|
||||||
|
return 0; // Failed to read
|
||||||
}
|
}
|
||||||
output[i] = '\0'; // Завершаем строку
|
|
||||||
|
// Remove newline character
|
||||||
|
line1[strcspn(line1, "\n")] = '\0';
|
||||||
|
|
||||||
|
// Read price
|
||||||
|
if (fgets(line2, LINESIZE, stdin) == NULL) {
|
||||||
|
return 0; // Failed to read
|
||||||
|
}
|
||||||
|
|
||||||
|
float value = strtof(line2, NULL);
|
||||||
|
if (value == 0.0F) {
|
||||||
|
return 0; // Invalid price
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the read values
|
||||||
|
strcpy(item->name, line1);
|
||||||
|
item->price = value;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read_pizza(struct pizza* item) {
|
// Function to search if a normalized needle exists in a normalized heap
|
||||||
char line[LINESIZE];
|
int search_string(const char *heap, const char *needle) {
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
|
char norm_heap[LINESIZE], norm_needle[LINESIZE];
|
||||||
|
|
||||||
normalize_string(line, item->name); // Преобразуем название
|
// Make copies to normalize
|
||||||
|
strcpy(norm_heap, heap);
|
||||||
|
strcpy(norm_needle, needle);
|
||||||
|
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
|
// Normalize both strings
|
||||||
item->prize = strtof(line, NULL); // Преобразуем цену
|
normalize_string(norm_heap);
|
||||||
|
normalize_string(norm_needle);
|
||||||
|
|
||||||
return (item->prize > 0) ? 1 : 0; // Успех или ошибка
|
// Use strstr to find the normalized needle in the heap
|
||||||
}
|
return strstr(norm_heap, norm_needle) != NULL;
|
||||||
|
|
||||||
int find_string(const char* heap, const char* needle) {
|
|
||||||
char normalized_heap[LINESIZE];
|
|
||||||
char normalized_needle[LINESIZE];
|
|
||||||
normalize_string(heap, normalized_heap);
|
|
||||||
normalize_string(needle, normalized_needle);
|
|
||||||
|
|
||||||
return strstr(normalized_heap, normalized_needle) != NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct pizza menu[MENU_SIZE];
|
struct pizza menu[MAX_PIZZAS];
|
||||||
int item_count = 0;
|
char search[LINESIZE];
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
char search_term[LINESIZE];
|
// Read search string
|
||||||
printf("Zadaj hladanu surovinu:\n"); // Первое сообщение
|
printf("Enter the searched ingredient:\n");
|
||||||
fgets(search_term, sizeof(search_term), stdin);
|
fgets(search, LINESIZE, stdin);
|
||||||
|
search[strcspn(search, "\n")] = '\0'; // Remove newline character
|
||||||
|
|
||||||
// Удаляем '\n' из поискового запроса
|
// Read menu
|
||||||
search_term[strcspn(search_term, "\n")] = '\0';
|
printf("Enter the menu:\n");
|
||||||
|
while (read_pizza(&menu[count])) {
|
||||||
printf("Zadaj jedalny listok:\n"); // Второе сообщение
|
count++;
|
||||||
while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) {
|
if (count >= MAX_PIZZAS) {
|
||||||
item_count++;
|
break; // Limit reached
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Флаг для проверки, был ли найден хотя бы один элемент
|
// Display valid dishes containing the searched ingredient
|
||||||
int found = 0;
|
int found = 0;
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
// Печать подходящих элементов
|
if (search_string(menu[i].name, search)) {
|
||||||
for (int i = 0; i < item_count; i++) {
|
printf("%s\n", menu[i].name);
|
||||||
if (find_string(menu[i].name, search_term)) {
|
printf("%.2f\n", menu[i].price);
|
||||||
printf("%s\n%.2f\n", menu[i].name, menu[i].prize);
|
found++;
|
||||||
found = 1; // Найдено совпадение
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если совпадений нет, ничего не выводится, иначе выводим количество
|
// Display total items read
|
||||||
if (found) {
|
printf("A total of %d items were loaded.\n", count);
|
||||||
printf("Nacitanych %d poloziek.\n", item_count);
|
|
||||||
} else {
|
|
||||||
// Выводим только количество, если совпадений нет
|
|
||||||
printf("Nacitanych %d poloziek.\n", item_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user