diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..038cf233 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC=gcc -g -Wall -std=c99 -pedantic +all: main +default: main +main: main.o table.o sort.o + gcc main.o table.o sort.o + +sort.o: sort.c sort.h + gcc sort.c -c -o sort.o + +table.o: table.c table.h + gcc table.c -c -o table.o + +clean: + rm -rf main.o \ No newline at end of file diff --git a/data.txt b/data.txt new file mode 100644 index 00000000..4239a56c --- /dev/null +++ b/data.txt @@ -0,0 +1,16 @@ +zzz 5.2 +vvv 0 +russia 6 +ulrick 1.1 +jorno 2.2 +da 20 +net 3 +sad 4.3 +session 8 +jack 11 +crash 1 +bandicoot 52 +f 7.9 +o 9.9 +p 6.5 +x 5.6 diff --git a/keys.txt b/keys.txt new file mode 100644 index 00000000..8c06efba --- /dev/null +++ b/keys.txt @@ -0,0 +1,16 @@ +5.2 +0 +6 +1.1 +2.2 +20 +3 +4.3 +2.9 +11 +1 +8 +7.9 +9.9 +6.5 +5.6 diff --git a/main.c b/main.c new file mode 100644 index 00000000..1f219773 --- /dev/null +++ b/main.c @@ -0,0 +1,90 @@ +#include "table.h" +#include "sort.h" + + +int main() { + FILE* d = fopen("data.txt", "r"); + if (!d) { + exit(-1); + } + Table* table = CreateTable(); + if(!table) { + Destroy(table); + return 0; + } + + item nr; + float digits; + while (fscanf(d, "%s %f", nr.val, &digits) == 2) { + nr.val[strcspn(nr.val, "\n")] = 0; + nr.key = digits; + Add(table, nr); + nr.key = 0; + } + + + fclose(d); + if(!table) { + exit(-1); + } + + printf("\nTable\n"); + Print(table); + char input[20]; + char printTable[10] = "print"; + char sortTable[15] = "sort"; + char exitMenu[10] = "exit"; + char search[10] = "search"; + char command[10] = ""; + + while(strcmp(command, exitMenu) != 0) { + printf("\n"); + printf("---------------------------------------------------------\n"); + printf("sort - sort table\n"); + printf("exit - exit programm\n"); + printf("search KEY - search element for key in sorted table\n"); + printf("print - print table\n"); + printf("---------------------------------------------------------\n"); + + char command[15] = ""; + fgets(input, sizeof(char) * 20, stdin); + if (feof(stdin)) { + break; + } + float num; + int ind = 0; + char forNum[20] = ""; + for (int i = 0; input[i] != '\n'; ++i) { + if (input[i] != ' ' && input[i] !='\n' && input[i] > '9') { + command[i] = input[i]; + input[i] = ' '; + } + else { + if (input[i] != ' ' && input[i] !='\n') { + forNum[ind] = input[i]; + ind++; + } + } + } + sscanf(forNum, "%f", &num); + + if (strcmp(command, printTable) == 0) { + Print(table); + } else if (strcmp(command, sortTable) == 0) { + QuickSort(table); + Print(table); + } else if (strcmp(command, search) == 0) { + int res = Search(table, num); + if (res == -1) { + printf("Not found\n"); + } else { + printf("[%f] -> (%s)\n", num, table->rows[res].val); + } + } else if (strcmp(command, exitMenu) == 0) { + Destroy(table); + break; + } else { + printf("-Wrong command\n"); + } + } +} diff --git a/sort.c b/sort.c new file mode 100644 index 00000000..d0d0fe5f --- /dev/null +++ b/sort.c @@ -0,0 +1,51 @@ +#include "sort.h" +#include "stdlib.h" + +void QuickSort(Table *t) { + int low = 0; + int high = t->size - 1; + int stack[high - low + 1]; + int top = -1; + stack[++top] = low; + stack[++top] = high; + + while (top >= 0) { + high = stack[top--]; + low = stack[top--]; + + double pivot = t->rows[high].key; + int i = low; + int j = high; + + while (i < j) { + while (t->rows[i].key < pivot) { + i++; + } + + while (t->rows[j].key > pivot) { + j--; + } + + if (i <= j) { + item tmp = t->rows[i]; + t->rows[i] = t->rows[j]; + t->rows[j] = tmp; + i++; + j--; + } + } + + if (low < j) { + stack[++top] = low; + stack[++top] = j; + } + + if (high > i) { + stack[++top] = i; + stack[++top] = high; + } + } +} + + + diff --git a/sort.h b/sort.h new file mode 100644 index 00000000..cca219b0 --- /dev/null +++ b/sort.h @@ -0,0 +1,6 @@ +#ifndef __SORT__ +#define __SORT__ +#include "table.h" + +void QuickSort(Table *t); +#endif \ No newline at end of file diff --git a/table.c b/table.c new file mode 100644 index 00000000..47a62eec --- /dev/null +++ b/table.c @@ -0,0 +1,76 @@ +#include "table.h" +#include "sort.h" + + + +Table* CreateTable() { + Table* tb = (Table*)malloc(sizeof(Table)); + if(!tb) { + return NULL; + } + item* data = (item*)malloc(MIN_CAPACITY * sizeof(item)); + if(!data) { + free(tb); + return NULL; + } + tb->capacity = MIN_CAPACITY; + tb->rows = data; + tb->size = 0; + return tb; +} + +bool Buffer(Table* t) { + if(t->size < t->capacity) + return true; + int new_cap = 2 * t->capacity; + item* tmp = (item*)realloc(t->rows, sizeof(item) * new_cap); + if(!tmp) { + return false; + } + t->capacity = new_cap; + t->rows = tmp; + return true; +} + +bool Add(Table* t, item r) { + if(!Buffer(t)) + return false; + t->rows[t->size] = r; + t->size++; + return true; +} + +void Print(Table* t) { + for(int i = 0; i < t->size; ++i) + printf("%2d | %.3f | %s\n", i, t->rows[i].key, t->rows[i].val); +} + +void Destroy(Table* t) { + free(t->rows); + free(t); +} + +int Search(Table* t, float k) { + int l = 0; + int r = t->size - 1; + int result = -1; + while(l <= r) { + int x = (l + r) >> 1; + int c; + if (t->rows[x].key == k) { + c = 0; + } else if (t->rows[x].key > k) { + c = 1; + } else { + c = -1; + } + if(c == 0) { + result = x; + r = x - 1; + } else if(c > 0) + r = x - 1; + else + l = x + 1; + } + return result; +} \ No newline at end of file diff --git a/table.h b/table.h new file mode 100644 index 00000000..7ffec50b --- /dev/null +++ b/table.h @@ -0,0 +1,30 @@ +#ifndef __TABLE__ +#define __TABLE__ +#define KEY_SIZE 4 +#define MAX_LEN 256 +#define MIN_CAPACITY 16 + +#include +#include +#include +#include + +typedef struct item { + float key; + char val[MAX_LEN]; +} item; + +typedef struct Table { + item* rows; + int size; + int capacity; +} Table; + +Table* CreateTable(); +bool Add(Table* t, item r); +bool Buffer(Table* t); +void Print(Table* t); +void Destroy(Table* t); +int Search(Table* t, float k); + +#endif \ No newline at end of file