Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions data.txt
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions keys.txt
Original file line number Diff line number Diff line change
@@ -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
90 changes: 90 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
51 changes: 51 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -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;
}
}
}



6 changes: 6 additions & 0 deletions sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __SORT__
#define __SORT__
#include "table.h"

void QuickSort(Table *t);
#endif
76 changes: 76 additions & 0 deletions table.c
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef __TABLE__
#define __TABLE__
#define KEY_SIZE 4
#define MAX_LEN 256
#define MIN_CAPACITY 16

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

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