code for exam 2019

This commit is contained in:
Oleksandr Lytvyn 2019-12-17 16:38:15 +01:00
commit 45b2121b5f
4 changed files with 488 additions and 0 deletions

136
hashtable.c Normal file
View File

@ -0,0 +1,136 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#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; i<SIZE; i++) {
if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,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");
}
}

105
rlc.c Normal file
View File

@ -0,0 +1,105 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#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<enlen; i++){
if(isdigit(line[i])){
count = count*10 + line[i]-'0';
}else{
while(count > 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;
}
}

128
search_pizza.c Normal file
View File

@ -0,0 +1,128 @@
#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;
}

119
sort_pizza.c Normal file
View File

@ -0,0 +1,119 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
}