-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.c
92 lines (81 loc) · 1.85 KB
/
database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct product {
char name[100];
char description[100];
char department[100];
double price;
int total_sale;
int quantity;
int date;
int time;
int key;
};
int hashCode(char * str, int size) {
int key;
int p = 1;
for (int i = strlen(str); i > 0 ; i--) {
key = (str[i] - 'a') * p;
p *= 10;
}
return key % size;
}
struct product* search(char* key, int size, struct product** hashArray) {
//get the hash
int hashIndex = hashCode(key, size);
//move in array until an empty
while (hashArray[hashIndex] != NULL) {
if (hashArray[hashIndex]->key == hashIndex)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= size;
}
return NULL;
}
int main(int argc, char* argv[])
{
struct product *hashArray;
FILE* fptr;
printf("%s\n", argv[1]);
if ((fptr = fopen(argv[1], "r")) == NULL) {
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for (int i = 0; i < 150; i++) {
char temp[1000];
fgets(temp, 1000, fptr);
printf("%s\n", temp);
if (temp[0]!=';') {
struct product *item;
char* token = strtok(temp, ",");
if (token[0] == ' ') {
memcpy(token, &token[1], strlen(token));
hashArray = search(token, strlen(token), hashArray);
}
else {
hashArray = search(token, strlen(token), hashArray);
}
*item->name = token;
token = strtok(NULL, ",");
*item->description = token;
token = strtok(NULL, ",");
*item->department = token;
token = strtok(NULL, ",");
item->date = token;
token = strtok(NULL, ",");
item->quantity = token;
token = strtok(NULL, ",");
item->total_sale = token;
token = strtok(NULL, ",");
item->price = strtod(token, &token);
token = strtok(NULL, ",");
item->time = token;
}
}
}