usaa23/cv1/program.c
2023-10-01 15:32:24 +02:00

110 lines
2.2 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_ITEMS 100
#define MAX_NAME_LENGTH 100
char hacker_script(char c) {
if (isupper(c)) {
c = 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* aber, const char* b) {
int aber_l = strlen(aber);
int b_l = strlen(b);
for (int i = 0; i <= aber_l - b_l; i++) {
int j;
for (j = 0; j < b_l; j++) {
if (hacker_script(aber[i + j]) != hacker_script(aber[j])) {
break;
}
}
if (j == b_l) {
return i;
}
}
return -1;
}
struct menu {
char name[MAX_NAME_LENGTH];
float price;
};
int read_pizza(struct menu* pizza) {
char line[MAX_NAME_LENGTH];
char line2[MAX_NAME_LENGTH];
if (fgets(line, sizeof(line), stdin) == NULL) {
return 0;
}
line[strcspn(line, "\n")] = '\0';
if (fgets(line2, sizeof(line2), stdin) == NULL) {
return 0; // Failed to read price
}
line2[strcspn(line2, "\n")] = '\0';
float value = strtof(line2, NULL);
if (value == 0.0F) {
return 0;
}
pizza->price = value;
strcpy(pizza->name, line);
return 1;
}
int main() {
char searchStr[MAX_NAME_LENGTH];
printf("Zadaj hladanu surovinu:\n");
if (fgets(searchStr, sizeof(searchStr), stdin) == NULL) {
return 1;
}
searchStr[strcspn(searchStr, "\n")] = '\0';
struct menu* menu = malloc(MAX_ITEMS * sizeof(struct menu));
if (menu == NULL) {
return 1;
}
int itemCount = 0;
printf("Zadaj jedalny listok:\n");
while (itemCount < MAX_ITEMS) {
struct menu pizza;
if (!read_pizza(&pizza)) {
break;
}
if (search_string(pizza.name, searchStr) != -1) {
printf("%s\n", pizza.name);
printf("%.2f\n", pizza.price);
}
itemCount++;
}
free(menu);
printf("Nacitanych %d poloziek.\n", itemCount);
return 0;
}