Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cassert>
  6. #include <cstring>
  7. #include <cctype>
  8. using namespace std;
  9.  
  10.  
  11. struct node{
  12. char letter;
  13. bool isWord;
  14. node* child;
  15. node* sibling;
  16. };
  17.  
  18. struct Rhymes{
  19. char** words;
  20. int length;
  21. };
  22.  
  23. char* removeFirstChar(char* word)
  24. {
  25. // cout << word << endl;
  26. char* retWord = new char[strlen(word)];
  27. for(int i =1; i < strlen(word)+1; i++)
  28. {
  29. retWord[i-1] = word[i];
  30. }
  31. // cout << retWord << endl;
  32. return retWord;
  33. }
  34.  
  35. node* createChild(char letter, int length)
  36. {
  37. node* child = new node;
  38. child->letter = letter;
  39. child->child = NULL;
  40. if(length > 1)
  41. {
  42.  
  43. child->isWord = false;
  44. }
  45. else
  46. {
  47. child->isWord = true;
  48. }
  49.  
  50. }
  51.  
  52. void insertLetter(node* head, char* word, int length)
  53. {
  54.  
  55. if(head->child == NULL)
  56. {
  57. node* child = createChild(word[0], length);
  58.  
  59. node* sibling = createChild('\0', 2);
  60.  
  61. sibling->sibling = head;
  62. child->sibling = sibling;
  63. head->child = child;
  64. if(length > 1)
  65. insertLetter(child,removeFirstChar(word), length-1);
  66. }
  67. else if(head->child->letter == word[0])
  68. {
  69. node* loc = head->child;
  70. if(length == 1)
  71. {
  72. loc->isWord = true;
  73. }
  74. else
  75. {
  76. insertLetter(loc, removeFirstChar(word), length-1);
  77. }
  78. }
  79. else
  80. {
  81. node* child = createChild(word[0], length);
  82. if(word[0] < head->child->letter)
  83. {
  84. child->sibling = head->child;
  85. head->child = child;
  86. }
  87. else
  88. {
  89.  
  90. head = head->child;
  91. while(word[0] > head->sibling->letter && head->sibling->letter != '\0')
  92. {
  93. head = head->sibling;
  94. }
  95. if(word[0] != head->sibling->letter)
  96. {
  97. child->sibling = head->sibling;
  98. head->sibling = child;
  99. }
  100. }
  101. if(length > 1)
  102. insertLetter(child,removeFirstChar(word), length-1);
  103.  
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. void findWords(node* head, char* suffix, Rhymes &words, int length)
  112. {
  113. char* tempSuffix = new char[25];
  114. while(head->letter != '\0')
  115. {
  116. words.words = (char **) realloc(words.words, sizeof(char **) * words.length+1);
  117. words.words[words.length] = (char *) malloc(25 * sizeof(char));
  118.  
  119. if(head->isWord == true)
  120. {
  121. int i;
  122. for(i=0; i < length; i++)
  123. {
  124. tempSuffix[i] = suffix[i];
  125. }
  126. tempSuffix[i+1] = head->letter;
  127. tempSuffix[i+2] = '\0';
  128. strcpy(words.words[words.length], tempSuffix);
  129. cout << words.words[words.length] << endl;
  130. words.length++;
  131. }
  132. if(head->child != NULL)
  133. {
  134.  
  135. findWords(head->child, tempSuffix, words, length+1);
  136. }
  137. head = head->sibling;
  138.  
  139. }
  140. cout << &words << endl;
  141.  
  142. }
  143. void locateRhyme(char* suffix, node* head, Rhymes &words)
  144. {
  145. int length = strlen(suffix);
  146. int depth = 0;
  147. node* temp = new node;
  148. temp = head->child;
  149. while(depth != length)
  150. {
  151. if(suffix[depth] != temp->letter && temp->letter != '\0')
  152. {
  153. temp = temp->sibling;
  154. }
  155. else if(temp->letter == '\0')
  156. {
  157. cout << "There are no matching words" << endl;
  158. break;
  159.  
  160. }
  161. else
  162. {
  163. depth++;
  164. temp = temp->child;
  165. }
  166. }
  167. findWords(temp,suffix,words, length);
  168.  
  169.  
  170. }
  171.  
  172.  
  173. //-----------------------------------------------------------------------------------
  174. // Convert word to lower case
  175. void convertToLowerCase( char theWord[], const int size)
  176. {
  177. // convert dictionary word to lower case
  178. for (int i=0; i<size; i++) {
  179. theWord[i] = tolower(theWord[i]);
  180. }
  181. }
  182.  
  183.  
  184. //-----------------------------------------------------------------------------------
  185. // Reverses the string Note:changing the orginal string
  186. char * strReverse(char *str)
  187. {
  188. char *p1, *p2;
  189.  
  190. if (! str || ! *str)
  191. return str;
  192. for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  193. {
  194. *p1 ^= *p2;
  195. *p2 ^= *p1;
  196. *p1 ^= *p2;
  197. }
  198. return str;
  199. }
  200.  
  201.  
  202. //-----------------------------------------------------------------------------------
  203. // Read from the dictionary file into an array, only keeping words that are the correct
  204. // length.
  205. void readDictionary( node* head ) // pointer to dictionary
  206. {
  207. ifstream inStream; // input file stream
  208. char fileName[] = "dictionary.txt"; // Input file name
  209. char tempString[25]; // stores a single string
  210. int size; // string size
  211. bool insert = true;
  212. inStream.open( fileName);
  213. assert( ! inStream.fail() ); // make sure file open was OK
  214.  
  215. cout << "\n Reading dictionary file from " << fileName << "\n";
  216. int currentRow = 0;
  217. int i = 0;
  218. while ( inStream >> tempString ) {
  219. size = strlen( tempString);
  220. insert = true;
  221. while(tempString[i])
  222. {
  223. if(!isalpha(tempString[i]))
  224. insert = false;
  225. i++;
  226. }
  227. if(insert)
  228. {
  229. convertToLowerCase( tempString, size); // convert word to lower case
  230. // Only store words that aren't too long or too short
  231. strcpy( strReverse(tempString), tempString);
  232. insertLetter(head,tempString,size);
  233. }
  234. }
  235. // Verify that we still have room in array
  236.  
  237. inStream.close(); // close the input file stream
  238.  
  239. }
  240.  
  241.  
  242. //-----------------------------------------------------------------------------------
  243. // Display words within the boundaries of a certain size.
  244. // This should only be used for debugging.
  245. void displayWords(
  246. // char theWords[][ MaxLength+1], // array of dictionary words
  247. int limit) // number of words in dictionary
  248. {
  249. int displaySizeMax; // Max size string to display
  250. int displaySizeMin; // Min size string to display
  251. int size; // stores size of strings
  252.  
  253. //cout << "(Stored words are of size " << MinLength << " to " << MaxLength << ")\n";
  254. cout << "Please enter min. and max. sizes of strings to display: ";
  255. cin >> displaySizeMin >> displaySizeMax;
  256.  
  257. cout << "\nWords stored in array are: \n";
  258. for (int i=0; i<limit; i++) {
  259. // size = strlen( theWords[ i]);
  260. // if ( (size >= displaySizeMin) && (size <= displaySizeMax) )
  261. // cout << theWords[ i] << " ";
  262. }
  263. cout << " \n"; // leave an extra line at the end
  264. }
  265.  
  266.  
  267. int main()
  268. {
  269. int numberOfWords= 0; // number of dictionary words
  270.  
  271. // Read words from input file into dictionary array
  272. // readDictionary( dictionary, numberOfWords);
  273.  
  274. // Selectively display words from dictionary array
  275. // displayWords( dictionary, numberOfWords);
  276.  
  277. // Lookup a word in the dictionary using binary search
  278. node* head = new node;
  279. head->letter = '\0';
  280. head->isWord = false;
  281. head->sibling = NULL;
  282. head->child = NULL;
  283. readDictionary( head );
  284. char aWord[ 25];
  285. string bWord;
  286. cout << "Enter a word for which to search. (Max length is " << 25 << ") ->";
  287. cin >> aWord;
  288. //strReverse(aWord); //String reversal nevermind!!!
  289.  
  290. cout << aWord << endl;
  291.  
  292. convertToLowerCase( aWord, strlen( aWord) ); // Ensure input is lower case
  293. strReverse(aWord);//reverses the c-string
  294. //string tmpStr(aWord);//Converts into string
  295. Rhymes words;
  296. cout << &words << endl;
  297. words.length = 1;
  298. words.words = (char **) malloc(sizeof(char **) * 1);
  299. words.words[0] = (char *) malloc(25 * sizeof(char));
  300.  
  301. // words.words = (char**)malloc()
  302. //display appropriate message depending on whether or not the word is found
  303. //bWord=tmpStr.substr(0,3);// takes in the first three letters of the reversed string...viz. the last three letters of the original string
  304. locateRhyme(aWord, head, words);
  305. cout << bWord << endl;
  306. for(int i = 0; i < words.length; i++)
  307. {
  308. cout << words.words[i] << endl;
  309. }
  310. return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement