From cf905eb6862f8f7e8a6f726bbfc1cc0abdba7f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Wed, 22 Jun 2016 14:05:29 +0300 Subject: [PATCH] Uploading a problem for the exam --- exam_problem2.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++ lab04/test.c | 4 +- 2 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 exam_problem2.c diff --git a/exam_problem2.c b/exam_problem2.c new file mode 100644 index 0000000..af55666 --- /dev/null +++ b/exam_problem2.c @@ -0,0 +1,177 @@ +#include +#include +#include + + + +// Sa se genereze un fisier binar cu 100 de numere reale(float/double) arbitrar. +void generate_file(const char * filename) { + srand(time(NULL)); + FILE * file = fopen(filename, "wb"); + if ( !file ) { + perror("Can't open file!"); + return; + } + + double numbers[100]; + for (int i = 0; i < 100; ++i) { + numbers[i] = rand() % 1000000 / 1000.0; + } + + fwrite(numbers, sizeof(double), 100, file); + fseek(file, 0, SEEK_SET); + + if ( fclose(file) ) { + perror("Can't close file."); + } +} + +//Se va citi continutul acestui fisier intr-o lista dublu inlantuita ordonata crescator. +typedef struct _dl_list * List; +struct _dl_list { + double data; + List next; + List prev; +}; + +List init() { + // create dummy list + List new_list = malloc(sizeof(struct _dl_list)); + new_list->data = -1; + new_list->next = new_list; + new_list->prev = new_list; + return new_list; +} + +void add(List list, double data) { + List head = list; + int flag = 0; + while( list->next != head ) { // middle insertion + list = list->next; + if ( data < list->data && !flag) { + List cur_node = list; + List node = malloc(sizeof(struct _dl_list)); + node->data = data; + node->next = cur_node; + node->prev = cur_node->prev; + + cur_node->prev->next = node; + cur_node->next->prev = cur_node; + cur_node->prev = node; + // head ... prev node | wwwti | cur node | next node ... last node + // ^ + // wwwti = where we want to insert. + flag = 1; + } + } + if ( list == head ) { // first insertion + List node = malloc(sizeof(struct _dl_list)); + node->data = data; + node->next = head; + node->prev = head; + list->next = node; + list->prev = node; + list = list->next; + } if ( list->next == head && data > list->data) { // last insertion + // cur_node | wwwti | head + // ^ + List node = malloc(sizeof(struct _dl_list)); + node->data = data; + node->next = head; + node->prev = list; + + list->next = node; + list = list->next; // move to wwwti + } + + // make list circular. + list->next = head; + head->prev = list; +} + +void print_list(List list) { + List head = list; + int counter = 0; + int item_counter = 0; + while ( list->next != head ) { + list = list->next; + ++item_counter; + // \x1b[31m = color red; %03d; \x1b[0m = color reset; + printf("\x1b[31m%03d\x1b[0m: %07.3f ", item_counter, list->data); + ++counter; + if ( counter % 5 == 0) { + printf("\n"); + counter = 0; + } + } + printf("\n"); + printf("next %.3f\n", list->next->data); // -1 is dummy value + printf("next's prev %.3f\n", list->next->prev->data); + printf("prev %.3f\n", list->prev->data); + printf("head prev %.3f\n", head->prev->data); + +} + +void read_from_file(List list, const char * filename) { + FILE * file = fopen(filename, "rb"); + if ( !file ) { + perror("Can't open file!"); + return; + } + + double item; + while ( fread(&item, sizeof(item), 1, file) != 0) { + add(list, item); + } + + if ( fclose(file) ) { + perror("Can't close file!"); + } +} + +// Sa se sorteze fisierul binar in ordine descrescatoare si sa se afisieze continutul listei pe ecran. +// Read the items into a buffer, qsort them, write them to file. +int comparator(const void * a, const void * b) { + double f = *((double *) a); + double s = *((double *) b); + if ( f > s ) { + return -1; + } else if ( f < s) { + return 1; + } + return 0; +} + +void sort_file(const char * filename) { + FILE * file = fopen(filename, "rb+"); + if ( !file ) { + perror("Can't open file!"); + return; + } + + double items[100]; + for ( int i = 0; i < 100; ++i ) { + fread(&items[i], sizeof(double), 1, file); + } + qsort(items, sizeof(items)/sizeof(items[0]), sizeof(double), comparator); + + fseek(file, 0, SEEK_SET); + for ( int i = 0; i < 100; ++i ) { + fwrite(&items[i], sizeof(items[0]), 1, file); + } + + if ( fclose(file) ) { + perror("Can't close file!"); + } +} + +int main() { + + generate_file("numbers.txt"); + List boss = init(); + read_from_file(boss, "numbers.txt"); + print_list(boss); + sort_file("numbers.txt"); + + return 0; +} diff --git a/lab04/test.c b/lab04/test.c index 87c6675..487c76c 100644 --- a/lab04/test.c +++ b/lab04/test.c @@ -5,7 +5,7 @@ enum cardsuit { DIAMONDS = 2 , HEARTS = 3 , SPADES = 4 -} custom_deck [ 52 ] ; +} custom_deck [ 52 ]; struct { unsigned int age : 3; @@ -19,4 +19,4 @@ int main() { if (aux == 0) Info.flagValid = 0; else Info.flagValid = 1; if (aux > 1) fprintf(stderr , "Sir , You should be more careful with your input!\n"); return 0; -} +} \ No newline at end of file