usaa19cv1/program.c

159 lines
3.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define LINE_SIZE 100
struct pizza {
float prize;
char name[LINE_SIZE];
};
int read_pizza(struct pizza* item);
int search_string(const char* heap, const char* needle);
char hacker_script(char c);
int main(int argc, char *argv[]) {
printf("Zadaj hladanu surovinu:\n");
printf("Zadaj jedalny listok:\n");
char string[LINE_SIZE];
memset(string,0,LINE_SIZE);
char *r = fgets(string,LINE_SIZE,stdin);
if(r != NULL && string[1] != 0){
char *pos;
if ((pos=strchr(string, '\n')) != NULL)
*pos = '\0';
//printf("Hladane jedlo: %s",string);
struct pizza item;
int counter = 0;
while(read_pizza(&item)){
counter += 1;
int a;
for(a=0;a<strlen(string);a++){
char y = hacker_script(string[a]);
if(y!='0'){
string[a] = y;
}
}
//int x = strlen(item.name);
//printf("Dlzka %d\n",x);
/*int i;
for(i=0;i<strlen(item.name)-1;i++){
char y = hacker_script(item.name[i]);
if(y!='0'){
item.name[i] = y;
}
}*/
//printf("Hladane jedlo: %s\n",string);
int z = search_string(string,item.name);
//printf("Vraciam %d\n",z);
if(z!=-1){
//printf("Vraciam %d\n",z);
printf("%s",item.name);
printf("%.2f\n",item.prize);
}
}
printf("Nacitanych %d poloziek.\n",counter);
return 0;
}
else{
return 1;
}
}
int read_pizza(struct pizza* item){
char line[LINE_SIZE];
char line2[LINE_SIZE];
memset(line,0,LINE_SIZE);
memset(line2,0,LINE_SIZE);
while (1){
char *r = fgets(line,LINE_SIZE,stdin);
if(r != NULL && line[1] != 0){
char *r2 = fgets(line2,LINE_SIZE,stdin);
if(r2 != NULL && line2[1] != 0){
float value = strtof(line2,NULL);
if (value == 0.0F){
return 0;
}
item->prize = value;
strcpy(item->name, line);
return 1;
}
else{
return 0;
}
}
else{
return 0;
}
}
}
char hacker_script(char c){
char big_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char small_letters[] = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 26; i++){
if (c == big_letters[i]){
//printf(" Nahradzujem %c za %c\n",big_letters[i],small_letters[i]);
return small_letters[i];
}
}
char numbers[] = "0123456789";
char letters[] = "oizeasbtbq";
for (int i = 0; i < 10; i++){
if (c == numbers[i]){
//printf(" Nahradzujem %c za %c\n",numbers[i],letters[i]);
return letters[i];
}
}
return '0';
}
int search_string(const char* heap, const char* needle){
char needle_2[strlen(needle)];
strcpy(needle_2,needle);
int a;
for(a=0;a<strlen(needle)-1;a++){
char y = hacker_script(needle_2[a]);
if(y!='0'){
needle_2[a] = y;
}
}
//printf("%s\n",heap);
//printf("%s",needle_2);
int D = strlen(heap);
int G = strlen(needle_2)-1;
//printf("Dlzka hladaneho= %d\n",D);
//printf("Dlzka celeho je %d\n",G);
/* A loop to slide pat[] one by one */
for (int i = 0; i <= G - D; i++) {
int j;
/* For current index i, check for pattern match */
for (j = 0; j <= D; j++){
//printf("J=%d D=%d\n",j,D);
if(j==D){
//printf("Vraciam sa!");
return i;}
//printf("%c %c\n",needle_2[i + j],heap[j]);
if (needle_2[i + j] != heap[j])
break;
}
}
return -1;
}