usaa24/cv1/program.c

143 lines
3.3 KiB
C
Raw Permalink Normal View History

2024-09-30 11:44:44 +00:00
#include <stdio.h>
2024-09-30 12:02:02 +00:00
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define LINESIZE 100
#define MAX_PIZZAS 1000
struct pizza {
float prize;
char name[LINESIZE];
};
// "Hacker Script"
char hacker_script(char c) {
c = tolower(c);
char numbers[] = "0123456789";
char letters[] = "oizeasbtbq";
for (int i = 0; i < 10; i++) {
if (c == numbers[i]) {
return letters[i];
}
}
switch(c) {
case 'o': return 'o';
case 'i': return 'i';
case 'z': return 'z';
case 'e': return 'e';
case 'a': return 'a';
case 's': return 's';
case 'b': return 'b';
case 't': return 't';
case 'q': return 'q';
default: return c;
}
}
2024-10-24 16:53:07 +00:00
// transformuje vstupný retazec a normalizuje ho pomocou funkcie hacker script uloži sa do reťazca dest
2024-09-30 12:02:02 +00:00
void normalize_string(char* dest, const char* src) {
int j = 0;
for(int i = 0; src[i] != '\0' && j < LINESIZE - 1; i++) {
dest[j++] = hacker_script(src[i]);
}
dest[j] = '\0';
}
2024-10-24 16:53:07 +00:00
// jednoduchý algoritmus na vyhľadanie podreťazca
2024-09-30 12:02:02 +00:00
int search_string(const char* heap, const char* needle) {
int heap_len = strlen(heap);
int needle_len = strlen(needle);
if(needle_len == 0) return 0;
for(int i = 0; i <= heap_len - needle_len; i++) {
int j = 0;
while(j < needle_len && heap[i + j] == needle[j]) {
j++;
}
if(j == needle_len) {
return i;
}
}
return -1;
}
// Funkcia na načítanie jednej položky jedálneho lístka
int read_pizza(FILE* fp, struct pizza* item) {
char line1[LINESIZE];
char line2[LINESIZE];
// Načítať názov
if(fgets(line1, sizeof(line1), fp) == NULL) {
return 0;
}
line1[strcspn(line1, "\n")] = '\0';
if(fgets(line2, sizeof(line2), fp) == NULL) {
return 0;
}
line2[strcspn(line2, "\n")] = '\0';
float value = strtof(line2, NULL);
if(value == 0.0F && strcmp(line2, "0") != 0) {
return 0;
}
item->prize = value;
strncpy(item->name, line1, LINESIZE - 1);
item->name[LINESIZE - 1] = '\0';
return 1;
}
2024-09-30 11:44:44 +00:00
int main() {
2024-09-30 12:02:02 +00:00
char search_input[LINESIZE];
char normalized_search[LINESIZE];
struct pizza jedalny_listok[MAX_PIZZAS];
int count = 0;
int matched = 0;
printf("Zadaj hladanu surovinu:\n");
if(fgets(search_input, sizeof(search_input), stdin) == NULL) {
fprintf(stderr, "Chyba pri načítavaní vyhľadávacieho reťazca.\n");
return 1;
}
search_input[strcspn(search_input, "\n")] = '\0';
normalize_string(normalized_search, search_input);
printf("Zadaj jedalny listok:\n");
while(count < MAX_PIZZAS && read_pizza(stdin, &jedalny_listok[count])) {
count++;
}
for(int i = 0; i < count; i++) {
char normalized_name[LINESIZE];
normalize_string(normalized_name, jedalny_listok[i].name);
if(search_string(normalized_name, normalized_search) != -1) {
printf("%s\n%.2f\n", jedalny_listok[i].name, jedalny_listok[i].prize);
matched++;
}
}
printf("Nacitanych %d poloziek.\n", count);
2024-09-30 11:44:44 +00:00
return 0;
2024-09-30 12:02:02 +00:00
}