Skip to content

Commit

Permalink
Create util.h & modife main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Direnol committed Apr 11, 2016
1 parent 655ac7f commit 9018e21
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 30 deletions.
95 changes: 65 additions & 30 deletions laba2/src/main.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,75 @@
#include "bstree.h"
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include <time.h>

char *input(char *name, int *size);

int main()
{
BSTree *tree, *look;
char *name;
int n;

name = input(name, &n);
tree = bstree_create(name, n);
printf("Name = %s, value = %d\n", tree->data.name, tree->data.value);

name = input(name, &n);
bstree_add(tree, name, n);
printf("Name = %s, value = %d\n", tree->right->data.name, tree->right->data.value);

name = input(name, &n);
bstree_add(tree, name, n);
printf("Name = %s, value = %d\n", tree->right->right->data.name, tree->right->right->data.value);

printf("Input key\n");
name = input(name, &n);
look = bstree_lookup(tree, name);
printf("Founded: name = %s, value = %d\n",look->data.name, look->data.value);
return 0;
}
if (argc < 2) {
fprintf(stderr, "Missing table size!\n");
return EXIT_FAILURE;
}

FILE *out1 = fopen("dataex1.dat", "a");
if (out1 == NULL) {
fprintf(stderr, "Cant create \"dataex1.dat\" file!\n");
return EXIT_FAILURE;
}

FILE *out2 = fopen("dataex2.dat", "a");
if (out2 == NULL) {
fprintf(stderr, "Cant create \"dataex2.dat\" file!\n");
return EXIT_FAILURE;
}

FILE *dictionary = fopen("task/dictionary.dat", "r");
if (dictionary == NULL) {
fprintf(stderr, "Can't open data file...\n");
return EXIT_FAILURE;
}

int size = atoi(argv[1]);

srand(time(NULL));
int rand_node = rand() % size;

double time;

hashtab_init(hashtab);
BSTree *tree = NULL;

/* Task 1 */
BSTree *lkt = NULL;
time = wtime();
lkt = bstree_lookup(tree, rand_data);
time = wtime() - time;
fprintf(outex1, "%d\t%.8f", table_size, time);
printf("%s %d\n", lkt->data.key, lkt->data.value);

ListNode *lks = NULL;
time = wtime();
lks = hashtab_lookup(hashtab, rand_data);
time = wtime() - time;
fprintf(outex1, "\t%.8f\n", time);
printf("%s %d\n", lks->data.key, lks->data.value);

/* Task 2 */
time = wtime();
bstree_add(tree, test, table_size + 1);
time = wtime() - time;
fprintf(outex2, "%d\t%.8f", table_size, time);

time = wtime();
hashtab_add(hashtab, test, table_size + 1);
time = wtime() - time;
fprintf(outex2, "\t%.8f\n", time);

fclose(outex1);
fclose(outex2);
fclose(dictionary);
return 0;

char *input(char *name, int *size)
{
printf("Enter size of string\n");
scanf("%d%*c", size);
name = malloc(*size * sizeof(char));
printf("Input the string\n");
fgets(name, *size, stdin);
return name;
}
21 changes: 21 additions & 0 deletions laba2/src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef UTIL_H
#define UTIL_H

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>

int getrand(int min, int max)
{
return (double) rand() / (RAND_MAX + 1.0) * (max - min) + min;
}

double wtime()
{
struct timeval t;
gettimeofday(&t, NULL);
return (double) t.tv_sec + (double) t.tv_usec * 1E-6;
}

#endif

0 comments on commit 9018e21

Please sign in to comment.