From 11de310a1f322594ea2414fb5f59e827305397ef Mon Sep 17 00:00:00 2001 From: Nathan McCloud Date: Sun, 26 Nov 2017 14:33:27 -0500 Subject: [PATCH] write to file fixed --- project4/mydatabase.c | 123 +++++++++++++++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 25 deletions(-) diff --git a/project4/mydatabase.c b/project4/mydatabase.c index 9e00f9c..6c81855 100644 --- a/project4/mydatabase.c +++ b/project4/mydatabase.c @@ -56,7 +56,6 @@ int matchSpecs(Tuple ct, Tuple lt) { } return 1; } - } int hash(char* key) { @@ -65,7 +64,7 @@ int hash(char* key) { for (int i = 0; key[i] != '\0'; i++) { sum += key[i]; } - + return sum % NBUCKETS; } @@ -101,10 +100,10 @@ void insert(Tuple t, Hashtable h) { printf("inserting...\n"); bucketInsert(t, &(h[hash(t.key)])); } - + } -int cmpTuple(Tuple a, Tuple b) { +/*int cmpTuple(Tuple a, Tuple b) { if(a.nAttr == b.nAttr && a.key == b.key) { for(int i=0; i< a.nAttr; i++) { //returns false if any of the attributes do not match @@ -112,21 +111,35 @@ int cmpTuple(Tuple a, Tuple b) { return 0; } } - //if the above condition is + //if the above condition is //never satisfied in the for loop, we return 1 return 1; } else { printf("Number of attributes or keys don't match\n"); } - + return 0; -} +}*/ void printTuple(Tuple t){ printf("tuple(%s, %d, %s, %s, %s)\n",t.key, t.nAttr, t.attr[0], t.attr[1], t.attr[2]); } +/* +void filePrintTuple(Tuple t, char *filename){ + FILE *fp; + fp = fopen(filename, "a"); + fseek(fp, 0, SEEK_END); // goto end of file + { + if (fp == NULL) { + perror("Can't open file"); + } else if (ftell(fp) == 0) + { + fprintf(fp, "tuple(%s, %d, %s, %s, %s)\n",t.key, t.nAttr, t.attr[0], t.attr[1], t.attr[2]);} + fclose(fp); + }}*/ + void printTable(Hashtable h) { for(int i=0; ikey, curr->nAttr, curr->attr[0], curr->attr[1], curr->attr[2]);} + void bucketDelete(Tuple t, List *head) { @@ -145,7 +175,7 @@ void bucketDelete(Tuple t, List *head) { } else { Tuple *curr = *head; Tuple *trail = NULL; - + while(curr != NULL) { if(matchSpecs(t, *curr)) { break; @@ -161,56 +191,99 @@ void bucketDelete(Tuple t, List *head) { //case 3 delete from head node if(*head == curr) { *head = (*head)->next; - + } else {//case 4 delete beyond head trail->next = curr->next; - + } free(curr); } - + } } -void delete(Tuple t, Hashtable h ) { +void bucketLookup(Tuple t, List *head) { + if(*head == NULL) { + printf("Cannot lookup from empty list\n"); + } else { + Tuple *curr = *head; + Tuple *trail = NULL; + + while(curr != NULL) { + if(matchSpecs(t, *curr)) { + printTuple(t); + break; + } else { + trail = curr; + curr = curr->next; + } + } + //case 2 not found tuple + if(curr == NULL) { + printf("tuple was not found\n"); + } + free(curr); + } + + } + + +void tupleDelete(Tuple t, Hashtable h ) { printf("deleting...\n"); bucketDelete(t, &h[hash(t.key)]); } +void lookup(Tuple t, Hashtable h){ + printf("finding tuple...\n"); + bucketLookup(t, &h[hash(t.key)]); +} + int main(void) { Hashtable csg; for(int i = 0; i< NBUCKETS; i++) { - + csg[i] = NULL; } - + Tuple c = tuple_new("John Lennon", 3, "CSC282", "John Lennon", "A-"); insert(c, csg); - - Tuple a = tuple_new("Luke Skywalker", 3,"CSC173", "Luke Skywalker", "A"); insert(a, csg); - Tuple b = tuple_new("Luke Skywalker", 3,"MTH201", "Luke Skywalker", "C-"); - insert(b, csg); - + /*Tuple x = tuple_new("Luke Skywalker", 3,"*", "*", "*"); + insert(x, csg);*/ + + /*Tuple b = tuple_new("Luke Skywalker", 3,"MTH201", "Luke Skywalker", "C-"); + insert(b, csg);*/ + Tuple d = tuple_new("George Eastman", 3, "MUR110", "George Eastman", "A+"); insert(d, csg); - - Tuple e = tuple_new("George Eastman", 3, "*", "*", "*"); - + + Tuple e = tuple_new("George East", 3, "*", "*", "*"); + insert(e,csg); + + Tuple f = tuple_new("George Eastman", 3, "*", "*", "*"); + //printf("a ?= d %d", matchSpecs(e,a)); printTable(csg); - delete(e,csg); - printTable(csg); + //lookup(f,csg); + //tupleDelete(e,csg); + //lookup(e,csg); + //lookup(f,csg); + filePrintTable(csg,"Test0.txt"); + tupleDelete(f,csg); + //lookup(d,csg); + //lookup(f,csg); + //lookup(e,csg); + //printTable(csg); + filePrintTable(csg,"Test.txt");//working now return 0; } -