Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- //#include <cs50.h>
- #include <string.h>
- #include <stdbool.h>
- typedef struct dllist
- #define LENGTH 26
- {
- char word[26];
- struct dllist *next;
- struct dllist *prev;
- }
- dllnode;
- bool find(dllnode *head, char *string);
- unsigned int hash(const char *word);
- bool load(const char *dictionary)
- {
- char word[LENGTH + 1]; // an array of characters
- FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
- if (dictr == NULL) // if there's nothing there
- {
- return false;
- }
- char wordnow[LENGTH+1]; // create an array
- //node *myword = NULL;
- while (fscanf(dictr,"%s", word) != EOF)
- //scan the file stored in dictr and store the string in word
- {
- dllnode *myword = malloc(sizeof(dllnode));
- if (myword == NULL)
- {
- return false;
- }
- strcpy(myword->word, word);
- // copy word into the string word
- // int the word field
- // of the myword node
- }
- // malloc?
- // int *dictr = malloc(3*sizeof(int));
- //store the pointer to a space of memory
- //called myword at the table index of the hash value of word
- // walkthrough instructions
- /*
- for each word you will
- create a new node that will
- store that word inside the hash function
- */
- //fscanf(dictr,"%s",word);
- /*
- scan words from the pointer to the file dict and send them as a string to word
- */
- return true;
- }
- unsigned int hash(const char *word)//const indicates it can't be changed
- {
- char c;
- int val = 0;
- //int cval;
- for (int i = 0; word[i] != '\0'; i++)
- {
- if (i < 3)
- {
- c = word[i];
- val += c;
- }
- else
- {
- continue;
- }
- }
- return val%26;
- }
- bool find(dllnode *head, char *string)
- {
- dllnode *trav = head;
- bool found = false;
- for (int i = 0; trav != NULL; i++)
- {
- for (int j = 0; trav->word[j] != '\0'; j++)
- {
- if (trav->word[j] == string[j]) // this returns true if any of the characters match
- {
- found = true;
- //continue;?
- }
- else
- found = false;
- }
- trav=trav->next;
- }
- return found;
- }
- //unsigned int size(void)
- unsigned int size(dllnode *innode)
- {
- dllnode *nownode = innode;
- int count = 0;
- int i = 0;
- while (nownode != NULL)
- {
- count++;
- i++;
- nownode = nownode->next;
- }
- return count;
- }
- // size table[1] returns 2... why not 3?
- //because you were askining of nownode-> is not NULL
- /*
- return the number of words in the dictionary
- you might iterate over every linked list in
- the hashtable counting the number of
- nodes in the linked list
- or
- as you are loading the hash table
- you could keep track of the number
- of words loaded so farin the linked list and return
- the value in the size function
- */
- dllnode *insert(dllnode *head, char *string)
- {
- //dllnode *tmp = head;
- dllnode *input = malloc(sizeof(dllnode));
- for (int i = 0; string[i] != '\0'; i++)
- {
- input->word[i] = string[i];
- }
- //input->next = head;
- //input->prev = NULL;
- head->prev = input;
- input->prev = NULL;
- input->next = head;
- return input;
- }
- void erase(dllnode *target)
- {
- target->prev->next = target->next;
- target->next->prev = target->prev;
- free(target);
- }
- void destroy(dllnode *head)
- {
- dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
- //while (cursor != NULL)
- while (cursor->next != NULL)
- // the different between destroy and unload is cursor->next
- {
- dllnode *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- }
- return;
- }
- //bool unload(void)
- bool unload(dllnode *nownode)
- {
- //int i = 0;
- dllnode *cursor = nownode;
- //while (cursor->next != NULL)
- while (cursor != NULL)
- {
- dllnode *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- //i++;
- }
- return false;
- }
- int main(int argc, char *argv[])
- {
- dllnode *fruit1 = malloc(sizeof(dllnode));
- dllnode *fruit2 = malloc(sizeof(dllnode));
- dllnode *fruit3 = malloc(sizeof(dllnode));
- char *f1 = "apple";
- char *f2 = "avocado";
- char *f3 = "papaya";
- char *f4 = "avocado";
- bool cmp = strcmp(f4, f3);
- //printf("comparing strings:\n");
- //printf("string f2 prints: %s\n", f2);
- //printf("string f4 prints: %s\n", f4);
- //printf("strcmp(f4, f2) prints: ");
- //printf("%d ",strcmp(f4,f2));
- //if(strcmp(f4, f2) == 0)
- //{
- //printf("Match!\n");
- //}
- //else
- //{
- //printf("strings don't match:( \n");
- //}
- /*
- for (int i = 0; f1[i] != '\0'; i++)
- {
- fruit1->word[i] = f1[i];
- }
- */
- //printf("fruit1->word prints %s\n", fruit1->word);
- //printf("applying strcpy: \n");
- strcpy(fruit1->word,f1);
- fruit1->next = fruit2;
- fruit1->prev = NULL;
- strcpy(fruit2->word,f2);
- fruit2->next = fruit3;
- fruit2->prev = fruit1;
- strcpy(fruit3->word, f3);
- fruit3->next = NULL;
- fruit3->prev = fruit2;
- dllnode *band1 = malloc(sizeof(dllnode));
- dllnode *band2 = malloc(sizeof(dllnode));
- dllnode *band3 = malloc(sizeof(dllnode));
- char *b2 = "Nirvana"; // points to mudhoney next
- char *b3 = "Mudhoney";
- strcpy(band1->word,"Soundgarden"/*b1*/);
- band1->next = band2;
- band1->prev = NULL;
- //printf("now band1->word prints %s\n",band1->word);
- //printf("band2->word prints %s\n", band2->word);
- //printf("applying strcpy: \n");
- strcpy(band2->word,b2);
- band2->next = band3;
- band2->prev = band1;
- //printf("band3->word prints %s\n", band3->word);
- //printf("applying strcpy: \n");
- strcpy(band3->word,b3);
- band3->next = NULL;
- band3->prev = band2;
- //printf("band1->word prints %s\n", band1->word);
- //printf("band1->next->word prints %s\n", band1->next->word);
- dllnode *prog1 = malloc(sizeof(dllnode));
- dllnode *prog2 = malloc(sizeof(dllnode));
- dllnode *prog3 = malloc(sizeof(dllnode));
- char *p1 = "Photoshop";
- char *p2 = "Maya";
- char *p3 = "Zbrush";
- strcpy(prog1->word, p1);
- prog1->next = prog2;
- prog1->prev = NULL;
- strcpy(prog2->word, p2);
- prog2->next = prog3;
- prog2->prev = prog1;
- strcpy(prog3->word, p3);
- prog3->next = NULL;
- prog3->prev = prog2;
- //for the speller assignment you will want to have N = 26*3 compartments
- dllnode *table[3];
- table[0] = fruit1;
- table[1] = band1;
- table[2] = prog1;
- printf("all the words each each node of table before unload print: \n");
- for (int i = 0; i < 3; i++)
- {
- dllnode *travel = table[i];
- while (travel != NULL)
- //while (travel->next != NULL)
- {
- printf("%s ",travel->word);
- travel = travel->next;
- }
- //printf("all the words each each node of table print: ");
- }
- printf("\n\n");
- printf("all the words in table after unload: \n");
- printf("unloading table:\n");
- for (int i = 0; i < 3; i++)
- {
- unload(table[i]);
- }
- printf("printing table after unload\n");
- for (int j = 0; j < 3; j++)
- {
- dllnode *travel = table[j];
- while (travel != NULL)
- {
- printf("%s ",travel->word);
- travel = travel->next;
- }
- }
- printf("\n");
- printf("testing the load function to load a dictionary into word: \n");
- //char word[1500000];
- FILE *dictr = fopen("large","r"); // open the large dictionary
- char ch;
- //int i = 0;
- char *buffer = malloc(sizeof(char)*LENGTH+1);
- //char buffer[LENGTH+1];
- printf("testing fscan to read a string from a file: ");
- while (fscanf(dictr, "%s", buffer) == 1)
- {
- fscanf(dictr, "%s", buffer);
- printf("%s\n", buffer);
- //i++;
- }
- //fscanf(dictr, "%s\n", buffer);
- //printf("%s\n", buffer);
- /*
- while (buffer != EOF)
- {
- printf("%s", buffer);
- }
- while ((ch = fgetc(dictr)) != EOF)
- {
- fscanf(dictr, "%s", buffer);
- printf("%s\n", buffer);
- }
- */
- fclose(dictr);
- free(buffer);
- //while ((buffer = fscanf(dictr,"%s", buffer)) != EOF);
- /*if (dictr != NULL)
- {
- char buffer[9];
- fscanf(dictr, "%s", buffer);
- fclose(dictr);
- printf("%s", buffer);
- }
- */
- //while ((ch = fgetc(dictr)) != EOF)
- // get characters from the large dictionary and store them in ch
- //{
- //char *nowword = malloc(26*sizeof(char));
- //create a pointer to a character, 26 chars long
- //while (ch != '\0')
- // as long as the char is not a null terminating char
- //{
- //nowword[i] = ch;
- // store the character at the ith index of nowword
- //i++; // increment i
- //}
- //if (ch == '\0')
- // if the character at ch is a null terminating character
- //{
- //nowword[i] = '\0';
- // set the charaacter at the ith index of nowword to
- // the null terminating character
- //i = 0;
- // reset i to 0
- }
- //printf("%s\n", nowword);
- //print the word
- //}
- //char c;
- //while ((c = fgetc(dictr)) != EOF)
- //int i = 0;
- //while ((c = fgetc(dictr)) != EOF)
- //{
- //char *myword = malloc(sizeof(char)*26);
- //char myword[26];
- //while (c != '\0')
- //{
- //char myword[26];
- //char *myword = malloc(sizeof(char)*26);
- //int i = 0;
- //myword[i] = c;
- //i++;
- //}
- //if (c =='\0')
- //{
- //myword[i] = '\0';
- //}
- //printf("mywordprints: %s",myword);
- //}
- //fclose(dictr);
- /*
- char ch;
- while ((ch = fgetc(dictr)) != EOF)//(dictr))
- {
- printf("%c", ch);
- //exit;
- }
- //printf("%c",'\0');
- fclose(dictr);
- //this works... save it!
- fread(word,sizeof(char), 1500000, dictr);
- printf("%s\n", word);
- free(dictr);
- */
- //char *read = fread(word,1,1,dictr);
- //fread(word,1,100,dictr);
- // read 1 byte of data
- //100 bytesat once from dictr into word
- //int ranum = (random()/((double)RAND_MAX + 1)) * 1000;
- //fread(word,sizeof(char), ranum, dictr);
- //fread(word,sizeof(char), random(1000, 10000), dictr);
- // print 8007 characters
- /*
- printf("word now prints:\n");
- for (int i = 0; i < 10; i++)
- {
- fread(word,sizeof(char),i,dictr);
- printf("%s\n", word);
- }
- */
- //fread(word,sizeof(char), 1000, dictr); prints seg fault
- //fread(word,sizeof(char), 8002, dictr); prints seg fault
- //fread(word,sizeof(char), 8003, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8004, dictr); prints seg fault
- //fread(word,sizeof(char), 8005, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8006, dictr); prints seg fault
- //fread(word,sizeof(char), 8007, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8008, dictr); prints seg fault
- //fread(word,sizeof(char), 8009, dictr); prints seg fault
- //fread(word,sizeof(char), 8010, dictr); prints seg fault
- //fread(word,sizeof(char), 1001, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 700, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 799, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8999, dictr); prints seg fault
- //fread(word,sizeof(char), 9001, dictr); prints seg fault
- //fread(word,sizeof(char), 9000, dictr); prints seg fault
- //fread(word,sizeof(char), 7998, dictr); print seg fault
- //fread(word,sizeof(char), 7997, dictr); print seg fault
- //fread(word,sizeof(char), 8452, dictr); print seg fault
- //fread(word,sizeof(char), 12000, dictr); print seg fault
- //fread(word,sizeof(char), 8000, dictr); print seg fault
- //fread(word,sizeof(char), 8001, dictr); doesnt print seg fault
- //fread(word,sizeof(char), 8002, dictr); print seg fault
- //fread(word,sizeof(char), 1999, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 2000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 3000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 4000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 4652, dictr); prints seg fault
- //fread(word,6*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,5*sizeof(char), 1000, dictr);// characters doesn't print seg fault
- //fread(word,4*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,2*sizeof(char), 1000, dictr); doesn't give seg fault..prints 2000 chars...is '\0' included?
- //fread(word,15*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,20*sizeof(char), 1000, dictr);// prints seg fault
- //fread(word,sizeof(char), 999, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 1000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 3457, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 10000, dictr); prints seg fault
- //fread(word,sizeof(char), 10001, dictr); prints seg fault
- //fread(word,sizeof(char), 10002, dictr); prints seg fault
- //fread(word,sizeof(char), 10003, dictr); prints seg fault
- /*
- printf("the characters in word are:\n");
- for (int i = 0; word[i] != '\0';i++)
- {
- printf("%c ",word[i]);
- }
- */
- //printf("\n");
- //free(prog1);
- //free(prog2);
- //free(prog3);
- //free(band1);
- //free(band2);
- //free(band3);
- //free(fruit1);
- //free(fruit2);
- //free(fruit3);
- //free(input);
- //bool load(const char *dictionary)
- //{
- //FILE *dictr = fopen(dictionary,"r");
- //}
- /*
- take as agrument the dictionary and load
- it into a hash table
- take char *dictionary.. the dictionary file you will
- open and read from in order to load all the data
- into the hash table
- it will return a boolean value, true if you
- were successful at loading all the data into the
- hash table, and false if there was some kind
- of memory error, or if you run out of memory to allocate
- or a file can't be opened
- when you open the file and store all the words, they
- will be stored at a hash table.
- a hash table is an array of linked lists
- to determine which of the linked lists to insert
- the word is based on a hash function.
- a function that will take a string and output an integer
- which will correspond to which of the linked lists
- you want to place the word into
- char int N = 1;
- node *tab
- */
Advertisement
Add Comment
Please, Sign In to add comment