Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1.  
  2. /* ------------------------------------------------
  3. * Basic Calculator with + - / *
  4. *
  5. * Class: CS 251, Fall 2014. Thur 10 am lab.
  6. * System: Xcode on Mac OSX
  7. * Author: Quan Zheng
  8. *
  9. * ToDo: Program 5 Rhymes With
  10. * -------------------------------------------------
  11. */
  12. #include <iostream>
  13. #include <fstream>
  14. #include <cassert>
  15. #include <cctype>
  16. #include <stdlib.h>
  17. using namespace std;
  18. const int Max_letter = 27; // Constant 26 letters
  19. struct Node{
  20. char c;
  21. int isWord;
  22. Node *letter[27];
  23. };
  24. int Get_Char_Pos(char c)
  25. {
  26. int temp = c;
  27. if(temp == '\'')
  28. return 27;
  29. else
  30. return c - 97;
  31. }
  32. //reverse the array
  33. void reverse_a(char x[], char *&y)
  34. {
  35. int count = 0;
  36. for(int i = 0; i < 26; i++)
  37. {
  38. count++;
  39. //int temp = x[i];
  40. if(x[i] == '\0')
  41. {
  42. break;
  43. }
  44. }
  45. y = new char[count-1];
  46.  
  47. for(int i = 0; i <= count; i++)
  48. {
  49. y[i] = x[count-i-2];
  50. }
  51. }
  52. void reverse_b(char x[], char *&y,int count)
  53. {
  54.  
  55. y = new char[count-1];
  56.  
  57. for(int i = 0; i <= count; i++)
  58. {
  59. y[i] = x[count-i-2];
  60. }
  61. }
  62. int Get_Char_Len(char *x)
  63. {
  64. int count = 0;
  65. for(int i = 0; i <26; i++)
  66. {
  67. //int temp = x[i];
  68. if(x[i] == '\0')
  69. {
  70. break;
  71. }
  72. count++;
  73. }
  74. return count;
  75. }
  76. //No string
  77. void insertNode(Node *&pHead,char c, int isWord)
  78. {
  79. Node *pTemp = new Node;
  80.  
  81. pTemp->c = c;
  82. if(isWord == 1)
  83. pTemp->isWord = 1;
  84. pTemp->letter[Get_Char_Pos(c)] = NULL;
  85.  
  86. // Reset pointers depending if this is the first node
  87. if( pHead != NULL) {
  88. pTemp->letter[Get_Char_Pos(c)] = pHead; // Connect into list
  89. }
  90. // Reset head pointer
  91. pHead = pTemp;
  92. }
  93. //put words into array of linkedList
  94. void insertLinkedList(Node **&dictionary,char temp[])
  95. {
  96. int len = 100;
  97. int pos = Get_Char_Pos(temp[0]);
  98. insertNode(dictionary[pos],temp[0],1);
  99. int i;
  100. for(i = 1; i <len; i++)
  101. {
  102. if(temp[i] > 96 && temp[i] < 123)
  103. insertNode(dictionary[pos],temp[i],0);
  104. else if(temp[i] == 39)
  105. insertNode(dictionary[27],temp[i],0);
  106. else
  107. break;
  108. }
  109. }
  110. void readDictionary(Node **&dictionary)
  111. {
  112. ifstream inStream; // input file stream
  113. char fileName[] = "dictionary.txt"; // Input file name
  114. char tempString[100]; // stores a single string
  115. inStream.open( fileName);
  116. assert( ! inStream.fail() ); // make sure file open was OK
  117. while ( inStream >> tempString ) {
  118. insertLinkedList(dictionary,tempString);
  119. }
  120. inStream.close(); // close the input file stream
  121. }
  122. void Menu()
  123. {
  124. cout<< "Program 5 : Rhymes With "<<endl;
  125. cout<< "\t Using a trie to find rhyming words."<<endl;
  126. cout<< "Autho: Quan Zheng " <<endl;
  127. cout<< "Class: CS 251, Fall 2014"<<endl;
  128. cout<< "System: Xcode on MacBook Pro"<<endl<<endl;
  129. cout<< "Enter the suffix to find rhyming words[max = 25]: ";
  130. }
  131. void displayList( Node* pHead, char *word, int *number_of_word)
  132. {
  133. char iWord[26];
  134. int len = Get_Char_Len(word);
  135. char *temp = new char[26];
  136. int is_equal = 0;
  137. int count = 0;
  138. while( pHead != NULL) {
  139. temp[count] = pHead->c;
  140. count++;
  141. int is = pHead->isWord;
  142. if(is)
  143. {
  144. //cout<<"*";
  145. for(int i = 0 ; i < len ;i ++)
  146. {
  147. if(temp[i] == word[i])
  148. is_equal = 1;
  149. else{
  150. is_equal=0;
  151. break;
  152. }
  153. // cout<<" "<<temp[i];
  154. }
  155.  
  156. if(is_equal ==1)
  157. {
  158. for(int i = 1 ; i <=count ; i++)
  159. {
  160. cout<<temp[count-i];
  161. }
  162. //cout<<t;
  163. delete(temp);
  164. cout<<endl;
  165. *number_of_word +=1;
  166. }
  167.  
  168. count = 0;
  169. temp = new char[26];
  170. }
  171. char temp_c = pHead->c;
  172. int i = Get_Char_Pos(temp_c);
  173. if(i <0)
  174. i = 0;
  175. //cout<<i<<endl;
  176. pHead = pHead->letter[i];
  177.  
  178. }
  179. }
  180. int main()
  181. {
  182. int a = '\'';
  183. cout << a <<endl;
  184. int count = 0;
  185. Node **dictionary;
  186. char iWord[26];
  187. char *iWord_rev = new char[26];
  188. //string iWord,iWord_rev; //get word & reverse word
  189. Menu();
  190. //allocate dynamic array for size of 26 linkedlist
  191. dictionary =(Node**)malloc(sizeof(Node*)* Max_letter);
  192. for(int i = 0; i < Max_letter ; i++)
  193. dictionary[i] = (Node*)malloc(sizeof(Node));
  194.  
  195. //End of allocating,, dictionary[1] is not allocated in loop
  196. readDictionary(dictionary);
  197. cin >>iWord; //get word
  198. reverse_a(iWord,iWord_rev);
  199. //cout<<iWord_rev<<endl;
  200. int c = 0;
  201. for(int i = 0 ; i < Max_letter ; i++)
  202. {
  203. displayList(dictionary[i],iWord_rev, &c);
  204. }
  205. cout<<"Found "<<c<<" words which are above:"<<endl;
  206. cout<<endl<<endl;
  207. cout<<"Exiting program..."<<endl;
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement