du1
This commit is contained in:
parent
d5d067c290
commit
636ccf6c1c
102
du1/program.c
102
du1/program.c
@ -1,6 +1,104 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define LINESIZE 100
|
||||
#define MAXITEMS 100
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
struct pizza {
|
||||
float prize;
|
||||
char name[LINESIZE];
|
||||
};
|
||||
|
||||
// transformacia znaku do normalizovanej podoby
|
||||
char hacker_script(char c) {
|
||||
c = tolower((unsigned char)c);
|
||||
switch (c) {
|
||||
case '0': return 'o';
|
||||
case '1': return 'i';
|
||||
case '2': return 'z';
|
||||
case '3': return 'e';
|
||||
case '4': return 'a';
|
||||
case '5': return 's';
|
||||
case '6': return 'b';
|
||||
case '7': return 't';
|
||||
case '8': return 'b';
|
||||
case '9': return 'q';
|
||||
default: return c;
|
||||
}
|
||||
}
|
||||
|
||||
// normalizacia retazca
|
||||
void normalize(char *s) {
|
||||
for (int i = 0; s[i]; i++) {
|
||||
s[i] = hacker_script(s[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// vyhladavanie podretazca
|
||||
int search_string(const char *heap, const char *needle) {
|
||||
int n = strlen(heap);
|
||||
int m = strlen(needle);
|
||||
if (m == 0) return 0;
|
||||
|
||||
for (int i = 0; i <= n - m; i++) {
|
||||
int j = 0;
|
||||
while (j < m && heap[i + j] == needle[j]) {
|
||||
j++;
|
||||
}
|
||||
if (j == m) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// načítanie jednej položky, vráti 1 ak sa podarilo, 0 ak nie
|
||||
int read_pizza(struct pizza *item) {
|
||||
char line1[LINESIZE];
|
||||
char line2[LINESIZE];
|
||||
|
||||
if (!fgets(line1, LINESIZE, stdin)) return 0; // meno
|
||||
if (!fgets(line2, LINESIZE, stdin)) return 0; // cena
|
||||
|
||||
line1[strcspn(line1, "\n")] = 0;
|
||||
line2[strcspn(line2, "\n")] = 0;
|
||||
|
||||
float value;
|
||||
if (sscanf(line2, "%f", &value) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy(item->name, line1);
|
||||
item->prize = value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char search[LINESIZE];
|
||||
struct pizza listok[MAXITEMS];
|
||||
int count = 0;
|
||||
|
||||
printf("Zadaj hladanu surovinu:\n");
|
||||
if (!fgets(search, LINESIZE, stdin)) return 1;
|
||||
search[strcspn(search, "\n")] = 0;
|
||||
normalize(search);
|
||||
|
||||
printf("Zadaj jedalny listok:\n");
|
||||
while (count < MAXITEMS && read_pizza(&listok[count])) {
|
||||
count++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
char normalized[LINESIZE];
|
||||
strcpy(normalized, listok[i].name);
|
||||
normalize(normalized);
|
||||
|
||||
if (search_string(normalized, search) != -1) {
|
||||
printf("%s\n", listok[i].name);
|
||||
printf("%.2f\n", listok[i].prize);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Nacitanych %d poloziek.\n", count);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user