Aktualizovat "program.c

This commit is contained in:
Michal Čabra 2019-10-11 09:06:01 +00:00
parent bd5699ab5d
commit 301ac041da

View File

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