joseleeph

Untitled

Nov 5th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. //#include <cs50.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. typedef struct dllist
  8. #define LENGTH 26
  9. {
  10. char word[26];
  11. struct dllist *next;
  12. struct dllist *prev;
  13. }
  14. dllnode;
  15.  
  16.  
  17. bool find(dllnode *head, char *string);
  18. unsigned int hash(const char *word);
  19. bool load(const char *dictionary)
  20. {
  21.  
  22.  
  23. char word[LENGTH + 1]; // an array of characters
  24. FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
  25.  
  26. if (dictr == NULL) // if there's nothing there
  27. {
  28. return false;
  29. }
  30. char wordnow[LENGTH+1]; // create an array
  31.  
  32. //node *myword = NULL;
  33.  
  34. while (fscanf(dictr,"%s", word) != EOF)
  35. //scan the file stored in dictr and store the string in word
  36.  
  37. {
  38. dllnode *myword = malloc(sizeof(dllnode));
  39. if (myword == NULL)
  40. {
  41. return false;
  42. }
  43. //int hword = hash(word);
  44. //table[hash(word)] = myword;
  45. //myword->next = table[hash(word)];// should point to the original linked list
  46.  
  47. strcpy(myword->word, word);
  48. /*
  49. copy the string in the word field of myword into word
  50. */
  51.  
  52. }
  53.  
  54.  
  55. // malloc?
  56. // int *dictr = malloc(3*sizeof(int));
  57.  
  58.  
  59.  
  60. //store the pointer to a space of memory
  61. //called myword at the table index of the hash value of word
  62.  
  63. // walkthrough instructions
  64. /*
  65. for each word you will
  66. create a new node that will
  67. store that word inside the hash function
  68.  
  69. */
  70.  
  71. //fscanf(dictr,"%s",word);
  72. /*
  73. scan words from the pointer to the file dict and send them as a string to word
  74. */
  75.  
  76.  
  77. return true;
  78. }
  79.  
  80. unsigned int hash(const char *word)//const indicates it can't be changed
  81. {
  82.  
  83. char c;
  84. int val = 0;
  85. //int cval;
  86. for (int i = 0; word[i] != '\0'; i++)
  87. {
  88. if (i < 3)
  89. {
  90. c = word[i];
  91. val += c;
  92. }
  93. else
  94. {
  95. continue;
  96. }
  97. }
  98. return val%26;
  99. }
  100. bool find(dllnode *head, char *string)
  101. {
  102. dllnode *trav = head;
  103. bool found = false;
  104.  
  105. for (int i = 0; trav != NULL; i++)
  106. {
  107. for (int j = 0; trav->word[j] != '\0'; j++)
  108. {
  109. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  110. {
  111. found = true;
  112. //continue;?
  113. }
  114. else
  115. found = false;
  116. }
  117. trav=trav->next;
  118. }
  119. return found;
  120. }
  121.  
  122.  
  123. unsigned int size(dllnode *innode)
  124. {
  125. dllnode *nownode = innode;
  126. int count = 0;
  127. int i = 0;
  128. while (nownode != NULL)
  129. {
  130. count++;
  131. i++;
  132. nownode = nownode->next;
  133. }
  134. return count;
  135. }
  136. dllnode *insert(dllnode *head, char *string)
  137. {
  138. //dllnode *tmp = head;
  139.  
  140. dllnode *input = malloc(sizeof(dllnode));
  141. for (int i = 0; string[i] != '\0'; i++)
  142. {
  143. input->word[i] = string[i];
  144. }
  145.  
  146.  
  147. //input->next = head;
  148. //input->prev = NULL;
  149. head->prev = input;
  150. input->prev = NULL;
  151. input->next = head;
  152.  
  153. return input;
  154.  
  155. }
  156.  
  157. void erase(dllnode *target)
  158. {
  159. target->prev->next = target->next;
  160. target->next->prev = target->prev;
  161. free(target);
  162. }
  163.  
  164.  
  165. void destroy(dllnode *head)
  166. {
  167. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  168. //while (cursor != NULL)
  169. while (cursor->next != NULL)
  170. // the different between destroy and unload is cursor->next
  171. {
  172. dllnode *tmp = cursor;
  173. cursor = cursor->next;
  174. free(tmp);
  175. }
  176. return;
  177. }
  178.  
  179. //bool unload(void)
  180. bool unload(dllnode *nownode)
  181. {
  182. //int i = 0;
  183. dllnode *cursor = nownode;
  184. //while (cursor->next != NULL)
  185. while (cursor != NULL)
  186. {
  187. dllnode *tmp = cursor;
  188. cursor = cursor->next;
  189. free(tmp);
  190. //i++;
  191. }
  192. return false;
  193. }
  194.  
  195. int main(int argc, char *argv[])
  196. {
  197.  
  198. dllnode *fruit1 = malloc(sizeof(dllnode));
  199. dllnode *fruit2 = malloc(sizeof(dllnode));
  200. dllnode *fruit3 = malloc(sizeof(dllnode));
  201.  
  202. char *f1 = "apple";
  203. char *f2 = "avocado";
  204. char *f3 = "papaya";
  205.  
  206. char *f4 = "avocado";
  207. bool cmp = strcmp(f4, f3);
  208.  
  209.  
  210.  
  211. strcpy(fruit1->word,f1);
  212. fruit1->next = fruit2;
  213. fruit1->prev = NULL;
  214.  
  215. strcpy(fruit2->word,f2);
  216. fruit2->next = fruit3;
  217. fruit2->prev = fruit1;
  218.  
  219. strcpy(fruit3->word, f3);
  220. fruit3->next = NULL;
  221. fruit3->prev = fruit2;
  222.  
  223.  
  224.  
  225. dllnode *band1 = malloc(sizeof(dllnode));
  226. dllnode *band2 = malloc(sizeof(dllnode));
  227. dllnode *band3 = malloc(sizeof(dllnode));
  228.  
  229. char *b2 = "Nirvana"; // points to mudhoney next
  230. char *b3 = "Mudhoney";
  231.  
  232.  
  233. strcpy(band1->word,"Soundgarden"/*b1*/);
  234. band1->next = band2;
  235. band1->prev = NULL;
  236.  
  237. strcpy(band2->word,b2);
  238. band2->next = band3;
  239. band2->prev = band1;
  240.  
  241. strcpy(band3->word,b3);
  242. band3->next = NULL;
  243. band3->prev = band2;
  244.  
  245. dllnode *prog1 = malloc(sizeof(dllnode));
  246. dllnode *prog2 = malloc(sizeof(dllnode));
  247. dllnode *prog3 = malloc(sizeof(dllnode));
  248.  
  249. char *p1 = "Photoshop";
  250. char *p2 = "Maya";
  251. char *p3 = "Zbrush";
  252.  
  253. strcpy(prog1->word, p1);
  254. prog1->next = prog2;
  255. prog1->prev = NULL;
  256.  
  257. strcpy(prog2->word, p2);
  258. prog2->next = prog3;
  259. prog2->prev = prog1;
  260.  
  261. strcpy(prog3->word, p3);
  262. prog3->next = NULL;
  263. prog3->prev = prog2;
  264.  
  265. //for the speller assignment you will want to have N = 26*3 compartments
  266. dllnode *table[3];
  267. table[0] = fruit1;
  268. table[1] = band1;
  269. table[2] = prog1;
  270.  
  271.  
  272.  
  273.  
  274. printf("all the words each each node of table before unload print: \n");
  275. for (int i = 0; i < 3; i++)
  276. {
  277. dllnode *travel = table[i];
  278. while (travel != NULL)
  279. //while (travel->next != NULL)
  280. {
  281. printf("%s ",travel->word);
  282. travel = travel->next;
  283. }
  284.  
  285. //printf("all the words each each node of table print: ");
  286. }
  287. printf("\n\n");
  288.  
  289.  
  290. printf("all the words in table after unload: \n");
  291. printf("unloading table:\n");
  292.  
  293. for (int i = 0; i < 3; i++)
  294. {
  295. unload(table[i]);
  296. }
  297.  
  298. printf("printing table after unload\n");
  299. for (int i = 0; i < 3; i++)
  300. {
  301. dllnode *travel = table[i];
  302. while (travel != NULL)
  303. {
  304. printf("%s ",travel->word);
  305. travel = travel->next;
  306. }
  307. }
  308. printf("\n");
  309.  
  310. printf("testing the load function to load a dictionary into word: \n");
  311. char word[26];
  312. FILE *dictr = fopen("large","read");
  313.  
  314. fread(word,sizeof(char), 8007, dictr);// print 5000 characters
  315. printf("word now prints:\n");
  316.  
  317. printf("%s\n", word);
  318.  
  319. //fread(word,sizeof(char), 1000, dictr); prints seg fault
  320. //fread(word,sizeof(char), 8002, dictr); prints seg fault
  321. //fread(word,sizeof(char), 8003, dictr); doesn't print seg fault
  322. //fread(word,sizeof(char), 8004, dictr); prints seg fault
  323. //fread(word,sizeof(char), 8005, dictr); doesn't print seg fault
  324. //fread(word,sizeof(char), 8006, dictr); prints seg fault
  325. //fread(word,sizeof(char), 8007, dictr); doesn't print seg fault
  326. //fread(word,sizeof(char), 8008, dictr); prints seg fault
  327. //fread(word,sizeof(char), 8009, dictr); prints seg fault
  328. //fread(word,sizeof(char), 8010, dictr); prints seg fault
  329. //fread(word,sizeof(char), 1001, dictr); doesn't print seg fault
  330. //fread(word,sizeof(char), 700, dictr); doesn't print seg fault
  331. //fread(word,sizeof(char), 799, dictr); doesn't print seg fault
  332. //fread(word,sizeof(char), 8999, dictr); prints seg fault
  333. //fread(word,sizeof(char), 9001, dictr); prints seg fault
  334. //fread(word,sizeof(char), 9000, dictr); prints seg fault
  335. //fread(word,sizeof(char), 7998, dictr); print seg fault
  336. //fread(word,sizeof(char), 7997, dictr); print seg fault
  337. //fread(word,sizeof(char), 8452, dictr); print seg fault
  338. //fread(word,sizeof(char), 12000, dictr); print seg fault
  339. //fread(word,sizeof(char), 8000, dictr); print seg fault
  340. //fread(word,sizeof(char), 8001, dictr); doesnt print seg fault
  341. //fread(word,sizeof(char), 8002, dictr); print seg fault
  342. //fread(word,sizeof(char), 1999, dictr); doesn't print seg fault
  343. //fread(word,sizeof(char), 2000, dictr); doesn't print seg fault
  344. //fread(word,sizeof(char), 3000, dictr); doesn't print seg fault
  345. //fread(word,sizeof(char), 4000, dictr); doesn't print seg fault
  346. //fread(word,sizeof(char), 4652, dictr); prints seg fault
  347. //fread(word,6*sizeof(char), 1000, dictr); prints seg fault
  348. //fread(word,5*sizeof(char), 1000, dictr);// characters doesn't print seg fault
  349. //fread(word,4*sizeof(char), 1000, dictr); prints seg fault
  350. //fread(word,2*sizeof(char), 1000, dictr); doesn't give seg fault..prints 2000 chars...is '\0' included?
  351. //fread(word,15*sizeof(char), 1000, dictr); prints seg fault
  352. //fread(word,20*sizeof(char), 1000, dictr);// prints seg fault
  353. //fread(word,sizeof(char), 999, dictr); doesn't print seg fault
  354. //fread(word,sizeof(char), 1000, dictr); doesn't print seg fault
  355. //fread(word,sizeof(char), 3457, dictr); doesn't print seg fault
  356. //fread(word,sizeof(char), 10000, dictr); prints seg fault
  357. //fread(word,sizeof(char), 10001, dictr); prints seg fault
  358. //fread(word,sizeof(char), 10002, dictr); prints seg fault
  359. //fread(word,sizeof(char), 10003, dictr); prints seg fault
  360. /*
  361. printf("the characters in word are:\n");
  362. for (int i = 0; word[i] != '\0';i++)
  363. {
  364. printf("%c ",word[i]);
  365. }
  366. */
  367.  
  368. printf("\n");
  369. free(prog1);
  370. free(prog2);
  371. free(prog3);
  372. free(band1);
  373. free(band2);
  374. free(band3);
  375. free(fruit1);
  376. free(fruit2);
  377. free(fruit3);
  378. //free(input);
  379. }
  380.  
  381. //bool load(const char *dictionary)
  382. //{
  383. //FILE *dictr = fopen(dictionary,"r");
  384. //}
  385. /*
  386. take as agrument the dictionary and load
  387. it into a hash table
  388. take char *dictionary.. the dictionary file you will
  389. open and read from in order to load all the data
  390. into the hash table
  391. it will return a boolean value, true if you
  392. were successful at loading all the data into the
  393. hash table, and false if there was some kind
  394. of memory error, or if you run out of memory to allocate
  395. or a file can't be opened
  396.  
  397. when you open the file and store all the words, they
  398. will be stored at a hash table.
  399. a hash table is an array of linked lists
  400. to determine which of the linked lists to insert
  401. the word is based on a hash function.
  402. a function that will take a string and output an integer
  403. which will correspond to which of the linked lists
  404. you want to place the word into
  405.  
  406. char int N = 1;
  407. node *tab
  408. */
Advertisement
Add Comment
Please, Sign In to add comment