program.c
This commit is contained in:
parent
c951ff4d94
commit
836a695681
163
cv1/program.c
163
cv1/program.c
@ -1,16 +1,120 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define LINESIZE 100
|
||||||
|
|
||||||
|
struct pizza {
|
||||||
//#define LINESIZE 100
|
float price;
|
||||||
|
char name[LINESIZE];
|
||||||
struct Menu{
|
|
||||||
char name[100];
|
|
||||||
float price;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void transform(char *str) {
|
//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++) {
|
for (int i = 0; str[i]; i++) {
|
||||||
switch (str[i]) {
|
switch (str[i]) {
|
||||||
case '0': str[i] = 'o'; break;
|
case '0': str[i] = 'o'; break;
|
||||||
@ -27,48 +131,3 @@ void transform(char *str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
|
||||||
char search[100];
|
|
||||||
int pizza = 0;
|
|
||||||
|
|
||||||
printf("Zadaj hladanu surovinu:\n");
|
|
||||||
fgets(search, sizeof(search), stdin);
|
|
||||||
search[strlen(search) - 1] = '\0';
|
|
||||||
|
|
||||||
transform(search);
|
|
||||||
|
|
||||||
printf("Zadaj jedalny listok:\n");
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
struct Menu menu;
|
|
||||||
char pizza_name[100];
|
|
||||||
|
|
||||||
if (fgets(pizza_name, sizeof(pizza_name), stdin) == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
pizza_name[strlen(pizza_name) - 1] = '\0';
|
|
||||||
|
|
||||||
char transformed_pizza_name[100];
|
|
||||||
|
|
||||||
strcpy(transformed_pizza_name, pizza_name);
|
|
||||||
transform(transformed_pizza_name);
|
|
||||||
|
|
||||||
if (strstr(transformed_pizza_name, search) != NULL) {
|
|
||||||
// Načítanie ceny jedla
|
|
||||||
if (scanf("%f", &menu.price) != 1) {
|
|
||||||
printf("Chyba pri nacitani ceny pre jedlo: %s\n", pizza_name);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n", pizza_name);
|
|
||||||
printf("%.2f\n", menu.price);
|
|
||||||
pizza++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Nacitanych %d poloziek.\n", pizza);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user