71 lines
1.7 KiB
C
71 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define SIZE 100
|
|
|
|
typedef struct {
|
|
char name[SIZE];
|
|
float price;
|
|
} Pizza;
|
|
|
|
void strip_newline(char *s) {
|
|
size_t n = strlen(s);
|
|
n && s[n-1]=='\n' ? s[n-1]='\0' : 0;
|
|
}
|
|
|
|
char normal_char(char c) {
|
|
char lc = tolower((unsigned char)c);
|
|
return (lc >= '0' && lc <= '9') ? "oizeasbtbq"[lc - '0'] : lc;
|
|
}
|
|
|
|
void normal_str(const char *src, char *dst, size_t n) {
|
|
size_t i;
|
|
for (i = 0; src[i] && i + 1 < n; i++) dst[i] = normal_char(src[i]);
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
int contains(const char *text, const char *pattern) {
|
|
size_t len_text = strlen(text), len_pat = strlen(pattern);
|
|
if (len_pat > len_text) return 0;
|
|
|
|
for (size_t i = 0; i <= len_text - len_pat; i++)
|
|
!strncmp(text + i, pattern, len_pat) ? (void)(i = len_text) : 0;
|
|
|
|
for (size_t i = 0; i <= len_text - len_pat; i++)
|
|
if (!strncmp(text + i, pattern, len_pat)) return 1;
|
|
return 0;
|
|
}
|
|
|
|
int read_pizza(Pizza *p) {
|
|
char buf[SIZE];
|
|
return (!fgets(p->name, SIZE, stdin) || !fgets(buf, SIZE, stdin))
|
|
? 0
|
|
: (strip_newline(p->name), p->price = strtof(buf, NULL), 1);
|
|
}
|
|
|
|
int main() {
|
|
char needle[SIZE], needle_norm[SIZE];
|
|
Pizza p;
|
|
int total = 0;
|
|
|
|
printf("Zadaj hladanu surovinu:\n");
|
|
if (!fgets(needle, SIZE, stdin)) return printf("Nacitanych 0 poloziek.\n"), 0;
|
|
|
|
strip_newline(needle);
|
|
normal_str(needle, needle_norm, SIZE);
|
|
|
|
printf("Zadaj jedalny listok:\n");
|
|
while (read_pizza(&p)) {
|
|
total++;
|
|
char name_norm[SIZE];
|
|
normal_str(p.name, name_norm, SIZE);
|
|
contains(name_norm, needle_norm) ? printf("%s\n%.2f\n", p.name, p.price) : 0;
|
|
}
|
|
|
|
printf("Nacitanych %d poloziek.\n", total);
|
|
return 0;
|
|
}
|
|
|