From 12fb3493ff2d8cafbb141c641c51dcb626b109a7 Mon Sep 17 00:00:00 2001 From: FortuneHunter78 <92946832+FortuneHunter78@users.noreply.github.com> Date: Fri, 31 May 2024 14:25:50 +0300 Subject: [PATCH 1/3] KP_9 --- Makefile | 14 +++++++++ data.txt | 16 ++++++++++ keys.txt | 16 ++++++++++ main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sort.c | 51 +++++++++++++++++++++++++++++++ sort.h | 6 ++++ table.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ table.h | 30 ++++++++++++++++++ 8 files changed, 301 insertions(+) create mode 100644 Makefile create mode 100644 data.txt create mode 100644 keys.txt create mode 100644 main.c create mode 100644 sort.c create mode 100644 sort.h create mode 100644 table.c create mode 100644 table.h 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..b7546353 --- /dev/null +++ b/data.txt @@ -0,0 +1,16 @@ +zzz +vvv +russia +ulrick +jorno +da +net +sad +session +jack +crash +bandicoot +f +o +p +x 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..aa8c9912 --- /dev/null +++ b/main.c @@ -0,0 +1,92 @@ +#include "table.h" +#include "sort.h" + + +int main() { + FILE* d = fopen("data.txt", "r"); + FILE* k = fopen("keys.txt", "r"); + if(!d || !k) + exit(-1); + Table* table = CreateTable(); + if(!table) { + Destroy(table); + return 0; + } + + item nr; + while (fscanf(d, "%s", nr.val) > 0) { + float digits; + fscanf(k, "%f", &digits); + nr.val[strcspn(nr.val, "\n")] = 0; + nr.key = digits; + Add(table, nr); + nr.key = 0; + } + + + fclose(d); + fclose(k); + 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"); + } + } +} \ No newline at end of file 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 From f9f4b38373470cfe4f57ccbad421175b4e64389a Mon Sep 17 00:00:00 2001 From: FortuneHunter78 <92946832+FortuneHunter78@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:24:31 +0300 Subject: [PATCH 2/3] fix --- main.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index aa8c9912..1f219773 100644 --- a/main.c +++ b/main.c @@ -4,9 +4,9 @@ int main() { FILE* d = fopen("data.txt", "r"); - FILE* k = fopen("keys.txt", "r"); - if(!d || !k) + if (!d) { exit(-1); + } Table* table = CreateTable(); if(!table) { Destroy(table); @@ -14,9 +14,8 @@ int main() { } item nr; - while (fscanf(d, "%s", nr.val) > 0) { - float digits; - fscanf(k, "%f", &digits); + float digits; + while (fscanf(d, "%s %f", nr.val, &digits) == 2) { nr.val[strcspn(nr.val, "\n")] = 0; nr.key = digits; Add(table, nr); @@ -25,7 +24,6 @@ int main() { fclose(d); - fclose(k); if(!table) { exit(-1); } @@ -89,4 +87,4 @@ int main() { printf("-Wrong command\n"); } } -} \ No newline at end of file +} From d83b4da89a01c077129fa082a46b577b6e16c73b Mon Sep 17 00:00:00 2001 From: FortuneHunter78 <92946832+FortuneHunter78@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:25:33 +0300 Subject: [PATCH 3/3] data changed --- data.txt | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/data.txt b/data.txt index b7546353..4239a56c 100644 --- a/data.txt +++ b/data.txt @@ -1,16 +1,16 @@ -zzz -vvv -russia -ulrick -jorno -da -net -sad -session -jack -crash -bandicoot -f -o -p -x +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