usaa25/du1/program.c
2025-09-27 20:49:33 +02:00

123 lines
2.7 KiB
C

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define LINESIZE 100
#define MAX_PIZZAS 100
struct pizza {
float prize;
char name[LINESIZE];
};
void deshifr (char *text) {
for (int i = 0; text[i] != '\0'; i++) {
switch (text[i]) {
case '0':
text[i] = 'o';
break;
case '1':
text[i] = 'i';
break;
case '2':
text[i] = 'z';
break;
case '3':
text[i] = 'e';
break;
case '4':
text[i] = 'a';
break;
case '5':
text[i] = 's';
break;
case '6':
text[i] = 'b';
break;
case '7':
text[i] = 't';
break;
case '8':
text[i] = 'b';
break;
case '9':
text[i] = 'q';
break;
}
text[i] = tolower(text[i]);
}
}
int search_string (const char* heap_orig, const char* needle_orig) {
char heap[LINESIZE];
char needle[LINESIZE];
strcpy (heap, heap_orig);
strcpy (needle, needle_orig);
deshifr(heap);
deshifr(needle);
int heap_len = strlen(heap);
int needle_len = strlen(needle);
for (int i = 0; i <= heap_len - needle_len; i++) {
int o;
for (o = 0; o < needle_len; o++) {
if (heap[i+o] != needle[o]) {
break;
}
}
if (o == needle_len) {
return i;
}
}
return -1;
}
int read_pizza (struct pizza* item) {
char Line_jeden[LINESIZE]; //название пиццы
char Line_druha[LINESIZE]; //цена
if (fgets(Line_jeden, sizeof(Line_jeden), stdin)==NULL) {
return 0;
}
Line_jeden [strcspn(Line_jeden, "\n")] = '\0';
if (fgets(Line_druha, sizeof(Line_druha), stdin)==NULL) {
return 0;
}
Line_druha [strcspn(Line_druha, "\n")] = '\0';
float value;
if (sscanf(Line_druha, "%f", &value ) !=1) {
return 0;
}
item->prize = value;
strcpy(item->name, Line_jeden);
return 1;
}
int main() {
char s_item[LINESIZE];
struct pizza menu[MAX_PIZZAS];
int pocet = 0;
printf("Zadaj hladanu surovinu:\n");
fgets(s_item, sizeof(s_item), stdin);
s_item[strcspn(s_item, "\n")] = '\0';
printf("Zadaj hlavny listok:\n");
while (pocet < MAX_PIZZAS && read_pizza(&menu[pocet])) {
pocet++;
}
for (int i = 0; i < pocet; i++) {
if (search_string(menu[i].name, s_item) !=-1) {
printf("%s\n", menu[i].name);
printf("%.2f\n", menu[i].prize);
}
}
printf("Nacitannych %d poloziek.\n", pocet);
return 0;
}