Update cv1/program.c
This commit is contained in:
parent
f5d8c7855b
commit
b47fc10664
154
cv1/program.c
154
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);
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
|
strcpy(norm_needle, needle);
|
||||||
item->prize = strtof(line, NULL); // Преобразуем цену
|
|
||||||
|
// Normalize both strings
|
||||||
return (item->prize > 0) ? 1 : 0; // Успех или ошибка
|
normalize_string(norm_heap);
|
||||||
}
|
normalize_string(norm_needle);
|
||||||
|
|
||||||
int find_string(const char* heap, const char* needle) {
|
// Use strstr to find the normalized needle in the heap
|
||||||
char normalized_heap[LINESIZE];
|
return strstr(norm_heap, norm_needle) != NULL;
|
||||||
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' из поискового запроса
|
|
||||||
search_term[strcspn(search_term, "\n")] = '\0';
|
|
||||||
|
|
||||||
printf("Zadaj jedalny listok:\n"); // Второе сообщение
|
// Read menu
|
||||||
while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) {
|
printf("Enter the menu:\n");
|
||||||
item_count++;
|
while (read_pizza(&menu[count])) {
|
||||||
}
|
count++;
|
||||||
|
if (count >= MAX_PIZZAS) {
|
||||||
// Флаг для проверки, был ли найден хотя бы один элемент
|
break; // Limit reached
|
||||||
int found = 0;
|
|
||||||
|
|
||||||
// Печать подходящих элементов
|
|
||||||
for (int i = 0; i < item_count; i++) {
|
|
||||||
if (find_string(menu[i].name, search_term)) {
|
|
||||||
printf("%s\n%.2f\n", menu[i].name, menu[i].prize);
|
|
||||||
found = 1; // Найдено совпадение
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если совпадений нет, ничего не выводится, иначе выводим количество
|
// Display valid dishes containing the searched ingredient
|
||||||
if (found) {
|
int found = 0;
|
||||||
printf("Nacitanych %d poloziek.\n", item_count);
|
for (int i = 0; i < count; i++) {
|
||||||
} else {
|
if (search_string(menu[i].name, search)) {
|
||||||
// Выводим только количество, если совпадений нет
|
printf("%s\n", menu[i].name);
|
||||||
printf("Nacitanych %d poloziek.\n", item_count);
|
printf("%.2f\n", menu[i].price);
|
||||||
|
found++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display total items read
|
||||||
|
printf("A total of %d items were loaded.\n", count);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user