Add du1/program.c

This commit is contained in:
Denis Landa 2025-10-03 07:55:48 +00:00
parent 67729244c8
commit fe2d410fa0

116
du1/program.c Normal file
View File

@ -0,0 +1,116 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINESIZE 100
#define MAX_ITEMS 100
struct pizza {
float prize;
char name[LINESIZE];
};
char hacker_script(char c) {
if (isupper(c)) {
return tolower(c);
}
char numbers[] = "0123456789";
char letters[] = "oizeasbtbq";
for (int i = 0; i < 10; i++) {
if (c == numbers[i]) {
return letters[i];
}
}
return c;
}
int search_string(const char* heap, const char* needle) {
int heap_len = strlen(heap);
int needle_len = strlen(needle);
if (needle_len > heap_len) {
return -1;
}
for (int i = 0; i <= heap_len - needle_len; i++) {
int found = 1;
for (int j = 0; j < needle_len; j++) {
char h_char = hacker_script(heap[i + j]);
char n_char = hacker_script(needle[j]);
if (h_char != n_char) {
found = 0;
break;
}
}
if (found) {
return i;
}
}
return -1;
}
int read_pizza(struct pizza* item, FILE* stream) {
char line[LINESIZE];
char price_line[LINESIZE];
if (fgets(line, LINESIZE, stream) == NULL) {
return 0;
}
line[strcspn(line, "\n")] = 0;
if (fgets(price_line, LINESIZE, stream) == NULL) {
return 0;
}
char* endptr;
float value = strtof(price_line, &endptr);
if (endptr == price_line || value == 0.0f) {
return 0;
}
strcpy(item->name, line);
item->prize = value;
return 1;
}
int main() {
char search_term[LINESIZE];
struct pizza items[MAX_ITEMS];
int item_count = 0;
printf("Zadaj hladanu surovinu:\n");
if (fgets(search_term, LINESIZE, stdin) == NULL) {
printf("Chyba pri čítaní hľadaného reťazca\n");
return 1;
}
search_term[strcspn(search_term, "\n")] = 0;
printf("Zadaj jedalny listok:\n");
while (item_count < MAX_ITEMS && read_pizza(&items[item_count], stdin)) {
item_count++;
}
for (int i = 0; i < item_count; i++) {
if (search_string(items[i].name, search_term) != -1) {
printf("%s\n", items[i].name);
printf("%.2f\n", items[i].prize);
}
}
printf("Nacitanych %d poloziek.\n", item_count);
return 0;
}