From 45b2121b5f50d81bf849a015c2117e91e6998b6e Mon Sep 17 00:00:00 2001 From: Oleksandr Lytvyn Date: Tue, 17 Dec 2019 16:38:15 +0100 Subject: [PATCH] code for exam 2019 --- hashtable.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++ rlc.c | 105 ++++++++++++++++++++++++++++++++++++++ search_pizza.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++ sort_pizza.c | 119 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 488 insertions(+) create mode 100644 hashtable.c create mode 100644 rlc.c create mode 100644 search_pizza.c create mode 100644 sort_pizza.c diff --git a/hashtable.c b/hashtable.c new file mode 100644 index 0000000..cf68755 --- /dev/null +++ b/hashtable.c @@ -0,0 +1,136 @@ + +#include +#include +#include +#include + +#define SIZE 20 + +struct DataItem { + int data; + int key; +}; + +struct DataItem* hashArray[SIZE]; +struct DataItem* dummyItem; +struct DataItem* item; + +int hashCode(int key) { + return key % SIZE; +} + +struct DataItem *search(int key) { + //get the hash + int hashIndex = hashCode(key); + + //move in array until an empty + while(hashArray[hashIndex] != NULL) { + + if(hashArray[hashIndex]->key == key) + return hashArray[hashIndex]; + + //go to next cell + ++hashIndex; + + //wrap around the table + hashIndex %= SIZE; + } + + return NULL; +} + +void insert(int key,int data) { + + struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); + item->data = data; + item->key = key; + + //get the hash + int hashIndex = hashCode(key); + + //move in array until an empty or deleted cell + while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { + //go to next cell + ++hashIndex; + + //wrap around the table + hashIndex %= SIZE; + } + + hashArray[hashIndex] = item; +} + +struct DataItem* delete(struct DataItem* item) { + int key = item->key; + + //get the hash + int hashIndex = hashCode(key); + + //move in array until an empty + while(hashArray[hashIndex] != NULL) { + + if(hashArray[hashIndex]->key == key) { + struct DataItem* temp = hashArray[hashIndex]; + + //assign a dummy item at deleted position + hashArray[hashIndex] = dummyItem; + return temp; + } + + //go to next cell + ++hashIndex; + + //wrap around the table + hashIndex %= SIZE; + } + + return NULL; +} + +void display() { + int i = 0; + + for(i = 0; ikey,hashArray[i]->data); + else + printf(" ~~ "); + } + + printf("\n"); +} + +int main() { + dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); + dummyItem->data = -1; + dummyItem->key = -1; + + insert(1, 20); + insert(2, 70); + insert(42, 80); + insert(4, 25); + insert(12, 44); + insert(14, 32); + insert(17, 11); + insert(13, 78); + insert(37, 97); + + display(); + item = search(37); + + if(item != NULL) { + printf("Element found: %d\n", item->data); + } else { + printf("Element not found\n"); + } + + delete(item); + item = search(37); + + if(item != NULL) { + printf("Element found: %d\n", item->data); + } else { + printf("Element not found\n"); + } +} \ No newline at end of file diff --git a/rlc.c b/rlc.c new file mode 100644 index 0000000..b8fcb45 --- /dev/null +++ b/rlc.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#define LINE_SIZE 150 +#define MAX_RLEN 50 + +char* read_line(); +char* runLengthCod(char* line); +char* encode(char* src); +char* decode(char* line); + +int main(){ + char* line = read_line(); + //char* compressedline = runLengthCod(line); + char* compressedline = encode(line); + char* decompressedline = decode(compressedline); + printf("line = %s\n", line); + printf("encoded line = %s\n", compressedline); + printf("decoded line = %s\n", decompressedline); + + free(line); free(compressedline); + free(decompressedline); + return 1; +} + +char* encode(char* src) +{ + int rLen; + char count[MAX_RLEN]; + int len = strlen(src); + + char* dest = (char*)malloc(sizeof(char) * (len * 2 + 1)); + + int i, j = 0, k; + char ch; + + for (i = 0; i < len; i++) { + //dest[j++] = src[i]; + + ch = src[i]; + rLen = 1; + while (i + 1 < len && src[i] == src[i + 1]) { + rLen++; + i++; + } + sprintf(count, "%d", rLen); + + for (k = 0; *(count + k); k++, j++) { + dest[j] = count[k]; + } + dest[j++]=ch; + + } + + dest[j] = '\0'; + return dest; +} + +char* decode(char* line){ + char* result = (char*)malloc(LINE_SIZE); + memset(result,0,LINE_SIZE); + int enlen = strlen(line); + int count=0; + int j=0; + + for(int i=0; i 0){ + result[j] = line[i]; + j++; + count--; + } + } + + } + + return result; +} + +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{ + int len = strlen(line); + line[len-1] = '\0'; + return line; + } +} \ No newline at end of file diff --git a/search_pizza.c b/search_pizza.c new file mode 100644 index 0000000..fead41c --- /dev/null +++ b/search_pizza.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +#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; + +} \ No newline at end of file diff --git a/sort_pizza.c b/sort_pizza.c new file mode 100644 index 0000000..2a12868 --- /dev/null +++ b/sort_pizza.c @@ -0,0 +1,119 @@ + +#include +#include +#include +#include + +#define LINE_SIZE 100 +#define LIST_SIZE 100 + + +struct pizza { + char name[LINE_SIZE]; + float prize; +}; + +char* read_line(); + +int read_pizza(struct pizza* item); +int read_pizza_list(struct pizza* list); +int compare(const void* a, const void* b); + +int main(){ + + //struct pizza **list_of_pizzas =(struct pizza**)malloc(LIST_SIZE*sizeof(struct pizza *)); + struct pizza list_of_pizzas[LIST_SIZE]; + memset(list_of_pizzas,0,LIST_SIZE*sizeof(struct pizza)); + + int number_of_pizzas = read_pizza_list(list_of_pizzas); + + qsort(list_of_pizzas,number_of_pizzas,sizeof(struct pizza), compare); + + for(int i = 0; i < number_of_pizzas; i++){ + printf("%s", list_of_pizzas[i].name); + printf("%.6f\n", list_of_pizzas[i].prize); + } + + + + + return 0; +} + + +int read_pizza(struct pizza* item){ + char name_of_dish[LIST_SIZE]; + memset(name_of_dish,0,LINE_SIZE); + char *name_help = fgets(name_of_dish,LINE_SIZE,stdin); + + if(name_help == NULL || strlen(name_of_dish) == 1){ + return 0; + }else if (strlen(name_of_dish) == LIST_SIZE - 1 && name_of_dish[LIST_SIZE]!= '\n'){ + printf("name of pizza is too long\n"); + return 0; + } + + + if(name_of_dish!= 0){ + strcpy(item->name,name_of_dish); + + char value_help[LIST_SIZE]; + memset(value_help,0,LINE_SIZE); + char *value_help1 = fgets(value_help,LIST_SIZE,stdin); + if(value_help1 == NULL || strlen(value_help) == 1){ + return 0; + } + + float value = strtof(value_help,NULL); + if(value!=0.0F){ + item->prize=value; + return 1; + } + else return 0; + } + else return 0; +} + +int read_pizza_list(struct pizza *list){ + + int counter = 0; + + for (int i=0; i< LIST_SIZE; i++){ + struct pizza item; + memset(&item,0,sizeof(struct pizza)); + + int r = read_pizza(&item); + + if (r){ + // Ak sa nacitanie podarilo, skopirujte polozku do pola + memcpy(&list[i],&item,sizeof(struct pizza)); + counter += 1; + }else{ + //free(); + // Ak sa nacitanie nepodarilo, nasli sme poslednu polozku + // Prerusim nacitanie + break; + } + } + return counter; + +} +int compare(const void *a, const void *b){ + + // a = (struct pizza *)a; + // b = (struct pizza *)b; + struct pizza *first =(struct pizza *) a; + struct pizza *second =(struct pizza *) b; + if(first->prize == second->prize){ + return strcmp(first->name,second->name); + + }else{ + // printf("%0.6f\n",(first->prize) - (second->prize)); + float r = (first->prize) - (second->prize); + if (r > 0 ) return 1; + else if ( r < 0) return -1; + else return 0; + + } + +}