134 lines
3.0 KiB
C
134 lines
3.0 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
|
|
#define LINESIZE 100
|
|
|
|
struct pizza {
|
|
float price;
|
|
char name[LINESIZE];
|
|
};
|
|
|
|
//void trim(char* str);
|
|
int search_string(const char* a, const char* b);
|
|
int read_pizza(struct pizza* element);
|
|
void transform(char* str);
|
|
|
|
int main() {
|
|
struct pizza jedalny_listok[5]; // Použité priamo číslo 5 namiesto POCET_JEDAL
|
|
memset(jedalny_listok, 0, sizeof(struct pizza) * 5);
|
|
|
|
int counter = 0;
|
|
|
|
char line[LINESIZE];
|
|
memset(line, 0, LINESIZE);
|
|
|
|
printf("Zadaj hladanu surovinu:\n");
|
|
fgets(line, LINESIZE, stdin);
|
|
//trim(line);
|
|
|
|
if (strlen(line) > 1) {
|
|
printf("Zadaj jedalny listok:\n");
|
|
struct pizza element;
|
|
|
|
while (read_pizza(&element)) {
|
|
strcpy(jedalny_listok[counter].name, element.name);
|
|
jedalny_listok[counter].price = element.price;
|
|
counter++;
|
|
|
|
if (counter >= 5) { // Použité priamo číslo 5 namiesto POCET_JEDAL
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < counter; i++) {
|
|
char transformed_name[LINESIZE];
|
|
strcpy(transformed_name, jedalny_listok[i].name);
|
|
transform(transformed_name);
|
|
|
|
if (search_string(transformed_name, line) != -1) {
|
|
printf("%s\n", jedalny_listok[i].name);
|
|
printf("%.2f\n", jedalny_listok[i].price);
|
|
}
|
|
}
|
|
|
|
printf("Nacitanych %d poloziek.\n", counter);
|
|
|
|
return 0;
|
|
}
|
|
|
|
//void trim(char* str) {
|
|
//int i = 0;
|
|
//while (str[i] != '\n' && str[i] != 0) {
|
|
//i++;
|
|
//}
|
|
//str[i] = 0;
|
|
//}
|
|
|
|
int search_string(const char* a, const char* b) {
|
|
int c = strlen(b);
|
|
int d = strlen(a);
|
|
|
|
for (int i = 0; i <= d - c; i++) {
|
|
int j;
|
|
for (j = 0; j < c; j++) {
|
|
if (a[i + j] != b[j]) {
|
|
break;
|
|
}
|
|
}
|
|
if (j == c) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int read_pizza(struct pizza* element) {
|
|
char line[LINESIZE];
|
|
memset(line, 0, LINESIZE);
|
|
|
|
if (fgets(line, LINESIZE, stdin) == NULL) {
|
|
return 0;
|
|
}
|
|
if (line[1] == 0) {
|
|
return 0;
|
|
}
|
|
|
|
char line2[LINESIZE];
|
|
memset(line2, 0, LINESIZE);
|
|
|
|
if (fgets(line2, LINESIZE, stdin) == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
float value = strtof(line2, NULL);
|
|
|
|
if (value == 0.0F) {
|
|
return 0;
|
|
}
|
|
|
|
element->price = value;
|
|
strcpy(element->name, line);
|
|
return 1;
|
|
}
|
|
|
|
void transform(char* str) {
|
|
for (int i = 0; str[i]; i++) {
|
|
switch (str[i]) {
|
|
case '0': str[i] = 'o'; break;
|
|
case '1': str[i] = 'i'; break;
|
|
case '2': str[i] = 'z'; break;
|
|
case '3': str[i] = 'e'; break;
|
|
case '4': str[i] = 'a'; break;
|
|
case '5': str[i] = 's'; break;
|
|
case '6': str[i] = 'b'; break;
|
|
case '7': str[i] = 't'; break;
|
|
case '8': str[i] = 'b'; break;
|
|
case '9': str[i] = 'q'; break;
|
|
default: break;
|
|
}
|
|
}
|
|
}
|