usaa24/cv1/program.c

102 lines
2.3 KiB
C
Raw Permalink Normal View History

2024-10-03 21:16:54 +00:00
#include <stdio.h>
#include <string.h>
2024-10-04 12:52:13 +00:00
#include <stdlib.h>
2024-10-03 21:16:54 +00:00
#include <ctype.h>
2024-10-03 21:02:43 +00:00
2024-10-04 13:01:44 +00:00
#define BUFFER_SIZE 120
#define MAX_PIZZAS 120
2024-10-04 12:52:13 +00:00
2024-10-04 13:01:44 +00:00
struct pizza {
float cost;
2024-10-04 12:52:13 +00:00
char name[BUFFER_SIZE];
};
2024-10-04 13:01:44 +00:00
char to_leetspeak(char chi) {
switch (tolower(chi)) {
2024-10-04 12:52:13 +00:00
case 'o': return '0';
case 'i': return '1';
case 'z': return '2';
case 'e': return '3';
case 'a': return '4';
case 's': return '5';
case 't': return '7';
case 'b': return '8';
2024-10-04 13:01:44 +00:00
default: return tolower(chi);
2024-10-04 12:52:13 +00:00
}
2024-10-04 12:50:33 +00:00
}
2024-10-04 13:01:44 +00:00
// Преобразование строки в Leetspeak
2024-10-04 12:52:13 +00:00
void apply_leetspeak(char *input) {
for (int i = 0; input[i] != '\0'; i++) {
input[i] = to_leetspeak(input[i]);
2024-10-03 21:16:54 +00:00
}
}
2024-10-04 13:01:44 +00:00
// Проверка на наличие подстроки в строке
int has_substring(const char *name, const char *keyword) {
2024-10-04 12:52:13 +00:00
char modified_name[BUFFER_SIZE];
char modified_keyword[BUFFER_SIZE];
2024-10-03 21:16:54 +00:00
2024-10-04 13:01:44 +00:00
strncpy(modified_name, name, BUFFER_SIZE);
2024-10-04 12:52:13 +00:00
strncpy(modified_keyword, keyword, BUFFER_SIZE);
2024-10-03 21:16:54 +00:00
2024-10-04 12:52:13 +00:00
apply_leetspeak(modified_name);
apply_leetspeak(modified_keyword);
2024-10-04 12:50:33 +00:00
2024-10-04 12:52:13 +00:00
return strstr(modified_name, modified_keyword) != NULL;
}
2024-10-04 12:50:33 +00:00
2024-10-04 13:01:44 +00:00
int load_pizza(struct pizza* p) {
2024-10-04 12:52:13 +00:00
char temp_price[BUFFER_SIZE];
2024-10-04 12:20:12 +00:00
2024-10-04 13:01:44 +00:00
if (!fgets(p->name, BUFFER_SIZE, stdin)) {
return 0;
2024-10-04 12:52:13 +00:00
}
2024-10-04 13:01:44 +00:00
2024-10-04 12:52:13 +00:00
2024-10-04 13:01:44 +00:00
p->name[strcspn(p->name, "\n")] = '\0';
2024-10-04 12:50:33 +00:00
2024-10-04 13:01:44 +00:00
2024-10-04 12:52:13 +00:00
if (!fgets(temp_price, BUFFER_SIZE, stdin)) {
2024-10-04 13:01:44 +00:00
return 0;
2024-10-04 12:52:13 +00:00
}
2024-10-04 13:01:44 +00:00
p->cost = strtof(temp_price, NULL);
return p->cost > 0 || strcmp(temp_price, "0.0") == 0;
2024-10-04 12:52:13 +00:00
}
int main() {
2024-10-04 13:01:44 +00:00
char query[BUFFER_SIZE];
struct pizza pizzas[MAX_PIZZAS];
int pizza_count = 0;
2024-10-04 12:52:13 +00:00
printf("Zadaj hladanu surovinu:\n");
2024-10-04 13:01:44 +00:00
fgets(query, BUFFER_SIZE, stdin);
query[strcspn(query, "\n")] = '\0'; // Удаление символа новой строки
2024-10-04 12:52:13 +00:00
printf("Zadaj jedalny listok:\n");
2024-10-04 13:01:44 +00:00
while (pizza_count < MAX_PIZZAS && load_pizza(&pizzas[pizza_count])) {
pizza_count++;
2024-10-04 12:52:13 +00:00
}
2024-10-04 12:50:33 +00:00
2024-10-04 12:52:13 +00:00
int match_count = 0;
2024-10-04 12:02:06 +00:00
2024-10-04 13:01:44 +00:00
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);
2024-10-04 12:52:13 +00:00
match_count++;
2024-10-03 21:16:54 +00:00
}
}
2024-10-04 13:01:44 +00:00
printf("Nacitanych %d poloziek.\n", pizza_count);
2024-10-04 12:50:33 +00:00
2024-10-03 21:25:32 +00:00
return 0;
2024-10-03 21:18:50 +00:00
}