139 lines
3.0 KiB
C
139 lines
3.0 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
|
|
#define LINESIZE 100
|
|
#define MAX_PIZZAS 100
|
|
|
|
struct pizza
|
|
{
|
|
float prize;
|
|
char name[LINESIZE];
|
|
};
|
|
|
|
char hacker_script(char c)
|
|
{
|
|
if (isupper(c))
|
|
{
|
|
return 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* heap, const char* needle)
|
|
{
|
|
int heap_len=strlen(heap);
|
|
int needle_len=strlen(needle);
|
|
if (needle_len>heap_len || needle_len==0)
|
|
{
|
|
return -1;
|
|
}
|
|
for (int i = 0; i <= heap_len - needle_len; i++)
|
|
{
|
|
int found=1;
|
|
for (int j = 0; j < needle_len; j++) {
|
|
char h_char=hacker_script(heap[i+j]);
|
|
char n_char=hacker_script(needle[j]);
|
|
if (h_char!=n_char)
|
|
{
|
|
found = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (found)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int read_pizza(struct pizza* item)
|
|
{
|
|
if (item==NULL)
|
|
{
|
|
return 0;
|
|
}
|
|
char line[LINESIZE];
|
|
char line2[LINESIZE];
|
|
if (fgets(line, LINESIZE, stdin)==NULL)
|
|
{
|
|
return 0;
|
|
}
|
|
line[strcspn(line, "\n")]=0;
|
|
if (fgets(line2, LINESIZE, stdin)==NULL)
|
|
{
|
|
return 0;
|
|
}
|
|
char* endptr;
|
|
float value=strtof(line2, &endptr);
|
|
if (value<=0.0f || endptr==line2)
|
|
{
|
|
return 0;
|
|
}
|
|
item->prize=value;
|
|
strncpy(item->name, line, LINESIZE-1);
|
|
item->name[LINESIZE-1]='\0';
|
|
return 1;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
char search_string_input[LINESIZE];
|
|
struct pizza jedalny_listok[MAX_PIZZAS];
|
|
int pocet_poloziek=0;
|
|
printf("Zadaj hladanu surovinu:\n");
|
|
if (fgets(search_string_input, LINESIZE, stdin)==NULL)
|
|
{
|
|
return 1;
|
|
}
|
|
search_string_input[strcspn(search_string_input, "\n")]=0;
|
|
printf("Zadaj jedalny listok:\n");
|
|
char line[LINESIZE];
|
|
while (pocet_poloziek < MAX_PIZZAS)
|
|
{
|
|
if (fgets(line, LINESIZE, stdin)==NULL)
|
|
{
|
|
break;
|
|
}
|
|
if (line[0] == '\n')
|
|
{
|
|
break;
|
|
}
|
|
line[strcspn(line, "\n")]=0;
|
|
strncpy(jedalny_listok[pocet_poloziek].name, line, LINESIZE-1);
|
|
jedalny_listok[pocet_poloziek].name[LINESIZE-1]='\0';
|
|
if (fgets(line, LINESIZE, stdin)==NULL)
|
|
{
|
|
break;
|
|
}
|
|
char* endptr;
|
|
float value=strtof(line, &endptr);
|
|
if (value<=0.0f || endptr==line)
|
|
{
|
|
break;
|
|
}
|
|
jedalny_listok[pocet_poloziek].prize=value;
|
|
pocet_poloziek++;
|
|
}
|
|
for (int i = 0; i < pocet_poloziek; i++)
|
|
{
|
|
if (search_string(jedalny_listok[i].name, search_string_input)!=-1)
|
|
{
|
|
printf("%s\n", jedalny_listok[i].name);
|
|
printf("%.2f\n", jedalny_listok[i].prize);
|
|
}
|
|
}
|
|
printf("Nacitanych %d poloziek.\n", pocet_poloziek);
|
|
return 0;
|
|
}
|