usaa19exam/search_pizza.c

128 lines
2.4 KiB
C
Raw Normal View History

2019-12-17 15:38:15 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINE_SIZE 100
struct pizza{
float prize;
char name[LINE_SIZE];
};
char* read_line();
int read_pizza(struct pizza* item);
int search_string(const char* heap, const char* needle);
char hacker_script(char c);
int main(){
struct pizza* new_pizza= malloc(sizeof(struct pizza));
printf("Zadaj hladanu surovinu:\n");
char* name_of_cheque=read_line();
for(int i = 0; i < strlen(name_of_cheque); i++){
name_of_cheque[i]=hacker_script(name_of_cheque[i]);
}
name_of_cheque[strcspn(name_of_cheque,"\n")]=0;
if(name_of_cheque!=0){
int counter=0;
printf("Zadaj jedalny listok:\n");
while(read_pizza(new_pizza)){
counter++;
if(search_string(new_pizza->name,name_of_cheque)!=-1){
printf("%s",new_pizza->name);
printf("%.2f\n",new_pizza->prize);
}
}
printf("Nacitanych %d poloziek.\n", counter);
}
free(new_pizza);
free(name_of_cheque);
return 0;
}
char* read_line(){
char* line=(char*)malloc(LINE_SIZE);
memset(line,0,LINE_SIZE);
char* r = fgets(line,LINE_SIZE,stdin);
if(r==NULL||strlen(line)==1){
free(line);
return 0;
}
else if (strlen(line)==LINE_SIZE-1 && line[LINE_SIZE]!='\n')
{
printf("string is too long\n");
}
else{
return line;
}
}
int read_pizza(struct pizza* item){
char* name_of_dish= read_line();
if(name_of_dish!=0){
strcpy(item->name,name_of_dish);
float value = strtof(read_line(),NULL);
if(value!=0.0F){
item->prize=value;
return 1;
}
else return 0;
}
else return 0;
}
int search_string(const char* heap, const char* needle){
int heap_lenght = strlen(heap);
int needle_lenght = strlen(needle);
for(int i = 0; i <= heap_lenght-needle_lenght; i++){
int j;
for(j = 0; j <= needle_lenght; j++){
if( heap[i+j] != needle[j] && hacker_script(heap[i+j]) != needle[j]) break;
if(j+1 == needle_lenght){
return i;
}
}
}
return -1;
}
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;
}