joseleeph

Untitled

Nov 10th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.06 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. strcpy(myword->word, word);
  44. // copy word into the string word
  45. // int the word field
  46. // of the myword node
  47. }
  48.  
  49.  
  50. // malloc?
  51. // int *dictr = malloc(3*sizeof(int));
  52.  
  53.  
  54.  
  55. //store the pointer to a space of memory
  56. //called myword at the table index of the hash value of word
  57.  
  58. // walkthrough instructions
  59. /*
  60. for each word you will
  61. create a new node that will
  62. store that word inside the hash function
  63.  
  64. */
  65.  
  66. //fscanf(dictr,"%s",word);
  67. /*
  68. scan words from the pointer to the file dict and send them as a string to word
  69. */
  70.  
  71.  
  72. return true;
  73. }
  74.  
  75. unsigned int hash(const char *word)//const indicates it can't be changed
  76. {
  77.  
  78. char c;
  79. int val = 0;
  80. //int cval;
  81. for (int i = 0; word[i] != '\0'; i++)
  82. {
  83. if (i < 3)
  84. {
  85. c = word[i];
  86. val += c;
  87. }
  88. else
  89. {
  90. continue;
  91. }
  92. }
  93. return val%26;
  94. }
  95. bool find(dllnode *head, char *string)
  96. {
  97. dllnode *trav = head;
  98. bool found = false;
  99.  
  100. for (int i = 0; trav != NULL; i++)
  101. {
  102. for (int j = 0; trav->word[j] != '\0'; j++)
  103. {
  104. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  105. {
  106. found = true;
  107. //continue;?
  108. }
  109. else
  110. found = false;
  111. }
  112. trav=trav->next;
  113. }
  114. return found;
  115. }
  116.  
  117. //unsigned int size(void)
  118. unsigned int size(dllnode *innode)
  119. {
  120. dllnode *nownode = innode;
  121. int count = 0;
  122. int i = 0;
  123. while (nownode != NULL)
  124. {
  125. count++;
  126. i++;
  127. nownode = nownode->next;
  128. }
  129. return count;
  130. }
  131. // size table[1] returns 2... why not 3?
  132. //because you were askining of nownode-> is not NULL
  133.  
  134. /*
  135. return the number of words in the dictionary
  136. you might iterate over every linked list in
  137. the hashtable counting the number of
  138. nodes in the linked list
  139.  
  140. or
  141. as you are loading the hash table
  142. you could keep track of the number
  143. of words loaded so farin the linked list and return
  144. the value in the size function
  145.  
  146. */
  147. dllnode *insert(dllnode *head, char *string)
  148. {
  149. //dllnode *tmp = head;
  150.  
  151. dllnode *input = malloc(sizeof(dllnode));
  152. for (int i = 0; string[i] != '\0'; i++)
  153. {
  154. input->word[i] = string[i];
  155. }
  156.  
  157.  
  158. //input->next = head;
  159. //input->prev = NULL;
  160. head->prev = input;
  161. input->prev = NULL;
  162. input->next = head;
  163.  
  164. return input;
  165.  
  166. }
  167.  
  168. void erase(dllnode *target)
  169. {
  170. target->prev->next = target->next;
  171. target->next->prev = target->prev;
  172. free(target);
  173. }
  174.  
  175.  
  176. void destroy(dllnode *head)
  177. {
  178. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  179. //while (cursor != NULL)
  180. while (cursor->next != NULL)
  181. // the different between destroy and unload is cursor->next
  182. {
  183. dllnode *tmp = cursor;
  184. cursor = cursor->next;
  185. free(tmp);
  186. }
  187. return;
  188. }
  189.  
  190. //bool unload(void)
  191. bool unload(dllnode *nownode)
  192. {
  193. //int i = 0;
  194. dllnode *cursor = nownode;
  195. //while (cursor->next != NULL)
  196. while (cursor != NULL)
  197. {
  198. dllnode *tmp = cursor;
  199. cursor = cursor->next;
  200. free(tmp);
  201. //i++;
  202. }
  203. return false;
  204. }
  205.  
  206. int main(int argc, char *argv[])
  207. {
  208.  
  209. dllnode *fruit1 = malloc(sizeof(dllnode));
  210. dllnode *fruit2 = malloc(sizeof(dllnode));
  211. dllnode *fruit3 = malloc(sizeof(dllnode));
  212.  
  213. char *f1 = "apple";
  214. char *f2 = "avocado";
  215. char *f3 = "papaya";
  216.  
  217. char *f4 = "avocado";
  218. bool cmp = strcmp(f4, f3);
  219. //printf("comparing strings:\n");
  220. //printf("string f2 prints: %s\n", f2);
  221. //printf("string f4 prints: %s\n", f4);
  222. //printf("strcmp(f4, f2) prints: ");
  223. //printf("%d ",strcmp(f4,f2));
  224. //if(strcmp(f4, f2) == 0)
  225. //{
  226. //printf("Match!\n");
  227. //}
  228. //else
  229. //{
  230. //printf("strings don't match:( \n");
  231. //}
  232.  
  233. /*
  234. for (int i = 0; f1[i] != '\0'; i++)
  235. {
  236. fruit1->word[i] = f1[i];
  237. }
  238. */
  239. //printf("fruit1->word prints %s\n", fruit1->word);
  240. //printf("applying strcpy: \n");
  241. strcpy(fruit1->word,f1);
  242. fruit1->next = fruit2;
  243. fruit1->prev = NULL;
  244.  
  245. strcpy(fruit2->word,f2);
  246. fruit2->next = fruit3;
  247. fruit2->prev = fruit1;
  248.  
  249. strcpy(fruit3->word, f3);
  250. fruit3->next = NULL;
  251. fruit3->prev = fruit2;
  252.  
  253.  
  254.  
  255. dllnode *band1 = malloc(sizeof(dllnode));
  256. dllnode *band2 = malloc(sizeof(dllnode));
  257. dllnode *band3 = malloc(sizeof(dllnode));
  258.  
  259. char *b2 = "Nirvana"; // points to mudhoney next
  260. char *b3 = "Mudhoney";
  261.  
  262.  
  263. strcpy(band1->word,"Soundgarden"/*b1*/);
  264. band1->next = band2;
  265. band1->prev = NULL;
  266.  
  267. //printf("now band1->word prints %s\n",band1->word);
  268.  
  269.  
  270. //printf("band2->word prints %s\n", band2->word);
  271. //printf("applying strcpy: \n");
  272. strcpy(band2->word,b2);
  273. band2->next = band3;
  274. band2->prev = band1;
  275.  
  276. //printf("band3->word prints %s\n", band3->word);
  277. //printf("applying strcpy: \n");
  278. strcpy(band3->word,b3);
  279. band3->next = NULL;
  280. band3->prev = band2;
  281.  
  282. //printf("band1->word prints %s\n", band1->word);
  283. //printf("band1->next->word prints %s\n", band1->next->word);
  284.  
  285. dllnode *prog1 = malloc(sizeof(dllnode));
  286. dllnode *prog2 = malloc(sizeof(dllnode));
  287. dllnode *prog3 = malloc(sizeof(dllnode));
  288.  
  289. char *p1 = "Photoshop";
  290. char *p2 = "Maya";
  291. char *p3 = "Zbrush";
  292.  
  293. strcpy(prog1->word, p1);
  294. prog1->next = prog2;
  295. prog1->prev = NULL;
  296.  
  297. strcpy(prog2->word, p2);
  298. prog2->next = prog3;
  299. prog2->prev = prog1;
  300.  
  301. strcpy(prog3->word, p3);
  302. prog3->next = NULL;
  303. prog3->prev = prog2;
  304.  
  305. //for the speller assignment you will want to have N = 26*3 compartments
  306. dllnode *table[3];
  307. table[0] = fruit1;
  308. table[1] = band1;
  309. table[2] = prog1;
  310.  
  311.  
  312.  
  313.  
  314. printf("all the words each each node of table before unload print: \n");
  315. for (int i = 0; i < 3; i++)
  316. {
  317. dllnode *travel = table[i];
  318. while (travel != NULL)
  319. //while (travel->next != NULL)
  320. {
  321. printf("%s ",travel->word);
  322. travel = travel->next;
  323. }
  324.  
  325. //printf("all the words each each node of table print: ");
  326. }
  327. printf("\n\n");
  328.  
  329.  
  330. printf("all the words in table after unload: \n");
  331. printf("unloading table:\n");
  332.  
  333. for (int i = 0; i < 3; i++)
  334. {
  335. unload(table[i]);
  336. }
  337.  
  338. printf("printing table after unload\n");
  339. for (int j = 0; j < 3; j++)
  340. {
  341. dllnode *travel = table[j];
  342. while (travel != NULL)
  343. {
  344. printf("%s ",travel->word);
  345. travel = travel->next;
  346. }
  347. }
  348. printf("\n");
  349.  
  350. printf("testing the load function to load a dictionary into word: \n");
  351. //char word[1500000];
  352. FILE *dictr = fopen("large","r"); // open the large dictionary
  353. char ch;
  354. //int i = 0;
  355. char *buffer = malloc(sizeof(char)*LENGTH+1);
  356. //char buffer[LENGTH+1];
  357. printf("testing fscan to read a string from a file: ");
  358.  
  359. while (fscanf(dictr, "%s", buffer) == 1)
  360. {
  361. fscanf(dictr, "%s", buffer);
  362. printf("%s\n", buffer);
  363. //i++;
  364. }
  365. //fscanf(dictr, "%s\n", buffer);
  366. //printf("%s\n", buffer);
  367. /*
  368. while (buffer != EOF)
  369. {
  370. printf("%s", buffer);
  371. }
  372.  
  373. while ((ch = fgetc(dictr)) != EOF)
  374. {
  375. fscanf(dictr, "%s", buffer);
  376. printf("%s\n", buffer);
  377. }
  378. */
  379. fclose(dictr);
  380. free(buffer);
  381. //while ((buffer = fscanf(dictr,"%s", buffer)) != EOF);
  382. /*if (dictr != NULL)
  383. {
  384. char buffer[9];
  385. fscanf(dictr, "%s", buffer);
  386. fclose(dictr);
  387. printf("%s", buffer);
  388. }
  389. */
  390. //while ((ch = fgetc(dictr)) != EOF)
  391. // get characters from the large dictionary and store them in ch
  392. //{
  393. //char *nowword = malloc(26*sizeof(char));
  394. //create a pointer to a character, 26 chars long
  395. //while (ch != '\0')
  396. // as long as the char is not a null terminating char
  397. //{
  398. //nowword[i] = ch;
  399. // store the character at the ith index of nowword
  400. //i++; // increment i
  401. //}
  402. //if (ch == '\0')
  403. // if the character at ch is a null terminating character
  404. //{
  405. //nowword[i] = '\0';
  406. // set the charaacter at the ith index of nowword to
  407. // the null terminating character
  408. //i = 0;
  409. // reset i to 0
  410.  
  411.  
  412. }
  413. //printf("%s\n", nowword);
  414. //print the word
  415. //}
  416.  
  417. //char c;
  418. //while ((c = fgetc(dictr)) != EOF)
  419. //int i = 0;
  420. //while ((c = fgetc(dictr)) != EOF)
  421. //{
  422. //char *myword = malloc(sizeof(char)*26);
  423. //char myword[26];
  424. //while (c != '\0')
  425. //{
  426. //char myword[26];
  427. //char *myword = malloc(sizeof(char)*26);
  428. //int i = 0;
  429. //myword[i] = c;
  430. //i++;
  431. //}
  432. //if (c =='\0')
  433. //{
  434. //myword[i] = '\0';
  435. //}
  436. //printf("mywordprints: %s",myword);
  437.  
  438. //}
  439. //fclose(dictr);
  440.  
  441. /*
  442.  
  443. char ch;
  444. while ((ch = fgetc(dictr)) != EOF)//(dictr))
  445. {
  446. printf("%c", ch);
  447. //exit;
  448. }
  449. //printf("%c",'\0');
  450. fclose(dictr);
  451.  
  452.  
  453.  
  454. //this works... save it!
  455. fread(word,sizeof(char), 1500000, dictr);
  456. printf("%s\n", word);
  457. free(dictr);
  458. */
  459.  
  460.  
  461. //char *read = fread(word,1,1,dictr);
  462. //fread(word,1,100,dictr);
  463. // read 1 byte of data
  464. //100 bytesat once from dictr into word
  465.  
  466. //int ranum = (random()/((double)RAND_MAX + 1)) * 1000;
  467. //fread(word,sizeof(char), ranum, dictr);
  468. //fread(word,sizeof(char), random(1000, 10000), dictr);
  469.  
  470. // print 8007 characters
  471.  
  472. /*
  473. printf("word now prints:\n");
  474. for (int i = 0; i < 10; i++)
  475. {
  476. fread(word,sizeof(char),i,dictr);
  477. printf("%s\n", word);
  478. }
  479. */
  480.  
  481.  
  482. //fread(word,sizeof(char), 1000, dictr); prints seg fault
  483. //fread(word,sizeof(char), 8002, dictr); prints seg fault
  484. //fread(word,sizeof(char), 8003, dictr); doesn't print seg fault
  485. //fread(word,sizeof(char), 8004, dictr); prints seg fault
  486. //fread(word,sizeof(char), 8005, dictr); doesn't print seg fault
  487. //fread(word,sizeof(char), 8006, dictr); prints seg fault
  488. //fread(word,sizeof(char), 8007, dictr); doesn't print seg fault
  489. //fread(word,sizeof(char), 8008, dictr); prints seg fault
  490. //fread(word,sizeof(char), 8009, dictr); prints seg fault
  491. //fread(word,sizeof(char), 8010, dictr); prints seg fault
  492. //fread(word,sizeof(char), 1001, dictr); doesn't print seg fault
  493. //fread(word,sizeof(char), 700, dictr); doesn't print seg fault
  494. //fread(word,sizeof(char), 799, dictr); doesn't print seg fault
  495. //fread(word,sizeof(char), 8999, dictr); prints seg fault
  496. //fread(word,sizeof(char), 9001, dictr); prints seg fault
  497. //fread(word,sizeof(char), 9000, dictr); prints seg fault
  498. //fread(word,sizeof(char), 7998, dictr); print seg fault
  499. //fread(word,sizeof(char), 7997, dictr); print seg fault
  500. //fread(word,sizeof(char), 8452, dictr); print seg fault
  501. //fread(word,sizeof(char), 12000, dictr); print seg fault
  502. //fread(word,sizeof(char), 8000, dictr); print seg fault
  503. //fread(word,sizeof(char), 8001, dictr); doesnt print seg fault
  504. //fread(word,sizeof(char), 8002, dictr); print seg fault
  505. //fread(word,sizeof(char), 1999, dictr); doesn't print seg fault
  506. //fread(word,sizeof(char), 2000, dictr); doesn't print seg fault
  507. //fread(word,sizeof(char), 3000, dictr); doesn't print seg fault
  508. //fread(word,sizeof(char), 4000, dictr); doesn't print seg fault
  509. //fread(word,sizeof(char), 4652, dictr); prints seg fault
  510. //fread(word,6*sizeof(char), 1000, dictr); prints seg fault
  511. //fread(word,5*sizeof(char), 1000, dictr);// characters doesn't print seg fault
  512. //fread(word,4*sizeof(char), 1000, dictr); prints seg fault
  513. //fread(word,2*sizeof(char), 1000, dictr); doesn't give seg fault..prints 2000 chars...is '\0' included?
  514. //fread(word,15*sizeof(char), 1000, dictr); prints seg fault
  515. //fread(word,20*sizeof(char), 1000, dictr);// prints seg fault
  516. //fread(word,sizeof(char), 999, dictr); doesn't print seg fault
  517. //fread(word,sizeof(char), 1000, dictr); doesn't print seg fault
  518. //fread(word,sizeof(char), 3457, dictr); doesn't print seg fault
  519. //fread(word,sizeof(char), 10000, dictr); prints seg fault
  520. //fread(word,sizeof(char), 10001, dictr); prints seg fault
  521. //fread(word,sizeof(char), 10002, dictr); prints seg fault
  522. //fread(word,sizeof(char), 10003, dictr); prints seg fault
  523. /*
  524. printf("the characters in word are:\n");
  525. for (int i = 0; word[i] != '\0';i++)
  526. {
  527. printf("%c ",word[i]);
  528. }
  529. */
  530.  
  531. //printf("\n");
  532. //free(prog1);
  533. //free(prog2);
  534. //free(prog3);
  535. //free(band1);
  536. //free(band2);
  537. //free(band3);
  538. //free(fruit1);
  539. //free(fruit2);
  540. //free(fruit3);
  541. //free(input);
  542.  
  543.  
  544. //bool load(const char *dictionary)
  545. //{
  546. //FILE *dictr = fopen(dictionary,"r");
  547. //}
  548. /*
  549. take as agrument the dictionary and load
  550. it into a hash table
  551. take char *dictionary.. the dictionary file you will
  552. open and read from in order to load all the data
  553. into the hash table
  554. it will return a boolean value, true if you
  555. were successful at loading all the data into the
  556. hash table, and false if there was some kind
  557. of memory error, or if you run out of memory to allocate
  558. or a file can't be opened
  559.  
  560. when you open the file and store all the words, they
  561. will be stored at a hash table.
  562. a hash table is an array of linked lists
  563. to determine which of the linked lists to insert
  564. the word is based on a hash function.
  565. a function that will take a string and output an integer
  566. which will correspond to which of the linked lists
  567. you want to place the word into
  568.  
  569. char int N = 1;
  570. node *tab
  571. */
Advertisement
Add Comment
Please, Sign In to add comment