Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1.  
  2. /*-------------------------------------------------------------
  3. * Program 5: Rhymes With
  4. *
  5. * Class: CS 251, Fall 2014.
  6. * System: Xcode on Mac OSX
  7. * Author: Laith Sarhan, Dale Reed (various use of sample codes)
  8. *
  9. * This program
  10. * -------------------------------------------------------------
  11. */
  12. /* Running this program looks like
  13. Author: Laith Sarhan
  14. Lab: Thursday 12 pm
  15. Program: #3, Display Tree (BINARY HEAP VERSION)
  16. Starting Heap program
  17.  
  18. Exiting program...
  19.  
  20. */
  21. #include <iostream>
  22. #include <fstream>
  23. #include <cassert>
  24. #include <cstring>
  25. #include <cctype>
  26. using namespace std;
  27.  
  28.  
  29. // read in words from dictinary, character by character, DFS
  30.  
  31. // define global constants and structures
  32. #define MAX_SIZE 1000+1 // Add 1 because we start at position 1, not 0
  33.  
  34.  
  35. // struct to hold nodes
  36. struct node{
  37. char c;
  38. node *letters[26];
  39. bool isWord;
  40. int vertex;
  41. node *pLink;
  42. };
  43.  
  44. // Function prototypes
  45. void progInfo();
  46.  
  47. char convert(int i)
  48. {
  49. return (i%26) + 'a';
  50. }
  51.  
  52. // Depending on the computer, if the values below are too large,
  53. // your program will cause a "segmentation fault." I suggest
  54. // you make the value of MaxLength something smaller in this case.
  55. const int MinLength = 3; // minimum word size
  56. const int MaxLength = 10; // maximum word size
  57. const int Rows = 260000; // max array size to handle all words on Unix system
  58.  
  59. //-----------------------------------------------------------------------------------
  60. // Convert word to lower case
  61. void convertToLowerCase( char theWord[], const int size)
  62. {
  63. // convert dictionary word to lower case
  64. for (int i=0; i<size; i++) {
  65. theWord[i] = tolower(theWord[i]);
  66. }
  67. }
  68.  
  69.  
  70. //-----------------------------------------------------------------------------------
  71. // Reverses the string Note:changing the orginal string
  72. char * strReverse(char *str)
  73. {
  74. char *p1, *p2;
  75.  
  76. if (! str || ! *str)
  77. return str;
  78. for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  79. {
  80. *p1 ^= *p2;
  81. *p2 ^= *p1;
  82. *p1 ^= *p2;
  83. }
  84. return str;
  85. }
  86.  
  87.  
  88. void init(node *&head){
  89. head = new node();
  90. head->c = '\0';
  91. head->isWord = false;
  92.  
  93. }
  94.  
  95. void insert (node *&head, char word[]){
  96. node *current = head;
  97.  
  98. for(int i = 0; i < strlen(word); i++){
  99. int pos = (int)word[i] - (int)'a';
  100.  
  101. if (current -> letters[pos] == NULL){
  102. current->letters[pos] = new node();
  103.  
  104. // current ->letters[pos]->c = word[i];
  105. // current =current->letters[pos];
  106. // break;
  107. }
  108.  
  109. current ->letters[pos]->c = word[i];
  110. current =current->letters[pos];
  111. }
  112.  
  113.  
  114. current-> isWord = true;
  115.  
  116. }
  117.  
  118.  
  119.  
  120. bool search (node *&head, char word[]){
  121. node *current = head;
  122. for (int i = 0; i < strlen(word); i++){
  123. if(current->letters[((int)word[i] - (int)'a')] == NULL){
  124. return false;
  125. }
  126.  
  127. current = current->letters[((int)word[i] - (int)'a')];
  128. }
  129. return current->isWord;
  130. }
  131.  
  132. ////Function to visit nodes in Preorder
  133. void dfs( node *&rot){//, char word[]) {
  134. static int counter = 0;
  135. static char tempString[50];
  136. tempString[counter + 1] = '\0';
  137. cout << "root is: " << rot->c <<endl;
  138. tempString[counter] = rot->c;
  139.  
  140. node *current = rot;
  141.  
  142.  
  143. for (int i = 0; i < 26; i++){
  144. if(current->letters[i] != NULL){
  145.  
  146. if (current ->isWord == true){
  147. cout << "the word is: " << strReverse(tempString) << "eed" << endl;
  148. strReverse(tempString);
  149. }
  150.  
  151.  
  152. counter++;
  153.  
  154. dfs(current->letters[i]);
  155. counter--;
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
  162.  
  163.  
  164. void searchNprint (node *&head, char word[]){
  165. node *current = head;
  166. char tempString[50];
  167. for (int i = 0; i < strlen(word); i++){
  168. // int letter = (int)word[i] - (int)'a';
  169. if(current->letters[((int)word[i] - (int)'a')] == NULL){
  170. cout << "You hit a null brehh.." << endl;
  171. return;
  172. }
  173.  
  174. cout << convert((int)word[i] - (int)'a') << endl;
  175. tempString[i] = convert((int)word[i] - (int)'a');
  176.  
  177. //if (i != strlen(word)-1)
  178. //{
  179. //cout << "current is at: " << current->c << endl;
  180. current = current->letters[((int)word[i] - (int)'a')];
  181. //}
  182.  
  183. }
  184.  
  185. // cout << " the full word is " << strReverse(tempString) << endl;
  186. cout << "passing in : " << current->c << endl;
  187. //do dfs for all children of suffix
  188. dfs(current);
  189. }
  190.  
  191. //-----------------------------------------------------------------------------------
  192. // Read from the dictionary file into an array, only keeping words that are the correct
  193. // length.
  194. void readDictionary(
  195. char words[][ MaxLength+1], // array of dictionary words
  196. int &currentRow, node *& head) // number of words in dictionary
  197. {
  198. ifstream inStream; // input file stream
  199. char fileName[] = "dictionary.txt"; // Input file name
  200. char tempString[MaxLength+1]; // stores a single string
  201. int size;
  202. int counter = 0;
  203. int alphaCheck = 0; // string size
  204.  
  205. inStream.open( fileName);
  206. assert( ! inStream.fail() ); // make sure file open was OK
  207.  
  208. cout << "\n Reading dictionary file from " << fileName << "\n";
  209. currentRow = 0;
  210.  
  211.  
  212.  
  213. while ( inStream >> tempString ) {
  214. size = strlen( tempString);
  215. convertToLowerCase( tempString, size);
  216.  
  217. for (int i = 0; i < size; i++){
  218. if(isalpha(tempString[i])){
  219. alphaCheck = 1;
  220. }
  221. else{
  222. alphaCheck = 0;
  223. break;
  224. }
  225. }
  226.  
  227. if (alphaCheck == 1){
  228. // Only store words that aren't too long or too short
  229. // if ( (size <= MaxLength ) && (size >= MinLength) ) {
  230. insert(head, strReverse(tempString));
  231. // strcpy( strReverse(words[ currentRow++]), tempString);
  232. // }
  233. counter++;
  234. }
  235.  
  236. // Verify that we still have room in array
  237. if (currentRow >= Rows) {
  238. cout << "*** Error, just read word " << tempString
  239. << " and currentRow is now at: " << currentRow
  240. << endl;
  241. }
  242. assert( currentRow < Rows); // halt program if error
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. inStream.close(); // close the input file stream
  250. cout << " the number of valid words are: " << counter << endl;
  251. }
  252.  
  253.  
  254. //-----------------------------------------------------------------------------------
  255. // Display words within the boundaries of a certain size.
  256. // This should only be used for debugging.
  257. void displayWords(
  258. char theWords[][ MaxLength+1], // array of dictionary words
  259. int limit) // number of words in dictionary
  260. {
  261. // int displaySizeMax; // Max size string to display
  262. // int displaySizeMin; // Min size string to display
  263. int size; // stores size of strings
  264.  
  265. cout << "\nWords stored in array are: \n";
  266. for (int i=0; i<limit; i++) {
  267. size = strlen( theWords[ i]);
  268. // if ( (size >= displaySizeMin) && (size <= displaySizeMax) )
  269. cout << theWords[ i] << " ";
  270. }
  271. cout << " \n"; // leave an extra line at the end
  272. }
  273.  
  274.  
  275. //-----------------------------------------------------------------------------------
  276. // Look up the word in the dictionary array, returning true if found, false otherwise
  277. bool wordWasFound(
  278. char pWord[], // the word for which we are searching
  279. char dictionary[][ MaxLength + 1], // dictionary array
  280. int numberOfDictionaryWords) // number of words in dictionary array
  281. {
  282. printf("\nWord that I am about to compare %s\n", pWord);
  283. for (int i=0; i< numberOfDictionaryWords; i++)
  284. {
  285. //printf("Dictionary[%d] = %s BEFORE strncmp\n", i, dictionary[i]);//EDIT THIS CODE
  286. if( strncmp( pWord, strReverse(dictionary[i]), 3) == 0)
  287. {
  288. strReverse(dictionary[i]); //Reverse word back to original
  289. // printf("Dictionary[%d] = %s AFTER strncmp\n", i, dictionary[i]);
  290. cout << dictionary[i]<< " is rhyming with the given word. \n";
  291. // return true; // Found the word!
  292. }
  293. }
  294. return false; // Did not find the word
  295.  
  296. //cout<< "word wasnt found. \n";
  297. }
  298.  
  299.  
  300. //-----------------------------------------------------------------------------
  301. //-----------------------------------------------------------------------------
  302. // Perform a depth-first search (DFS)
  303. void depthFirstSearch(int currentVertex, node *graph[], int visited[ ])
  304. {
  305. node * pTemp;
  306. visited[ currentVertex] = 1; // set visited value to true for this vertex
  307.  
  308. cout << currentVertex << " "; // display vertex being visited
  309.  
  310. // Examine in turn each vertex on adjacency list
  311. for (pTemp = graph[ currentVertex]; pTemp!=NULL; pTemp = pTemp->pLink) {
  312. // If it hasn't already been visited, recursively visit it
  313. if( !visited[ pTemp->vertex]) {
  314. depthFirstSearch( pTemp->vertex, graph, visited);
  315. }
  316. }//end for( pTemp = ...
  317.  
  318. }//end depthFirstSearch(...
  319.  
  320.  
  321. // Main function of program that takes user's input as queue for a binary heap,
  322. // inserts and sorts it in a min heap, than displays the levels as a tree in
  323. // ascii characters
  324. int main() {
  325. // Clears console screen, not necessary/used for tidiness
  326. system("clear");
  327.  
  328. // Author & program info
  329. progInfo();
  330.  
  331. node *head = NULL;
  332. int visited[ Rows]; // Which nodes have been visited so far on traversal
  333.  
  334. init(head);
  335.  
  336. char dictionary[ Rows][MaxLength+1]; // array of dictionary words
  337. int numberOfWords= 0; // number of dictionary words
  338.  
  339. // Read words from input file into dictionary array
  340. readDictionary( dictionary, numberOfWords, head);
  341.  
  342. // Selectively display words from dictionary array
  343. //displayWords( dictionary, numberOfWords);
  344.  
  345. // Lookup a word in the dictionary using binary search
  346. char aWord[ MaxLength];
  347. cout << "Enter a word for which to search. (Max length is " << MaxLength << ") ->";
  348. cin >> aWord;
  349. //strReverse(aWord); //String reversal nevermind!!!
  350.  
  351. cout << aWord << endl;
  352.  
  353. convertToLowerCase( aWord, strlen( aWord) ); // Ensure input is lower case
  354. strReverse(aWord);//reverses the c-string
  355.  
  356. //display appropriate message depending on whether or not the word is found
  357. // if ( wordWasFound( aWord, dictionary, numberOfWords) ) {
  358. // cout << "This rhyming word IS in the dictionary. \n";
  359. // }
  360. // else {
  361. // cout << "This rhyming word is NOT in the dictionary. \n";
  362. // }
  363.  
  364. //searchNprint(head, aWord);
  365.  
  366.  
  367. // dfs(head, aWord);
  368. // if( search(head, aWord) == true){
  369. // cout << "This word IS in the trie. \n";
  370. // }
  371. // else {
  372. // cout << "This word is NOT in the trie. \n";
  373. // }
  374.  
  375.  
  376.  
  377. // Do a depth-first search (DFS).
  378. //
  379. // First initialize visited[] array to all 0s. Once visited, elements
  380. // are set to 1.
  381. //for( int i=0; i<Rows; i++) {
  382. // visited[ i] = 0;
  383. //}
  384. //cout << "Depth First Search gives: ";
  385. //depthFirstSearch( 1, head, visited); // start at node 1. 0 is unused.
  386. //cout << endl;
  387.  
  388. searchNprint(head, aWord);
  389.  
  390. cout << "\n\nExiting program...\n";
  391. return 0;
  392. }
  393.  
  394. // This function displays author of program, lab time, and program name
  395. void progInfo()
  396. {
  397. cout<<"Author: Laith Sarhan\n"
  398. "Lab: Thursday 12 pm\n"
  399. "Program: #5, Rhymes With\n"
  400. ;
  401.  
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement