Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5. #include <cerrno>
  6. #include <vector>
  7. #include <ostream>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <sstream>
  11. using namespace std;
  12.  
  13. //Data Dec
  14. typedef struct {
  15. string word;
  16. vector<int> line;
  17. } wordData;
  18.  
  19. //overide for cout vector of wordData
  20. ostream & operator<<( ostream & out, wordData &data ) {
  21. int count = 0;
  22.  
  23. out << data.word << " | ";
  24.  
  25. for( count = 0; count < data.line.size(); count++ )
  26. {
  27. if( count == 0 ){
  28. out << data.line[count];
  29. }else{
  30. out << ", " << data.line[count];
  31. }
  32. }
  33.  
  34. out << "\n";
  35.  
  36. return out;
  37. }
  38.  
  39. //overide for cout vector of wordData
  40. bool operator<( wordData &dataA, wordData &dataB ) {
  41.  
  42. if( dataA.word.length() < dataB.word.length() )
  43. {
  44. return true;
  45. }else if( dataA.word.length() == dataB.word.length() ){
  46. if( toupper( dataA.word.at(0) ) < toupper( dataB.word.at(0) ) )
  47. {
  48. return true;
  49. }else{
  50. return false;
  51. }
  52. }else{
  53. return false;
  54. }
  55.  
  56. return false;
  57. }
  58.  
  59. //my tree
  60. struct TreeNode {
  61. wordData item;
  62. TreeNode *left;
  63. TreeNode *right;
  64. TreeNode( wordData incert )
  65. {
  66. item = incert;
  67. left = NULL;
  68. right = NULL;
  69. }
  70. }; // end Treenode
  71.  
  72. //Function Declarations
  73. void menu();
  74. void display();
  75. void option( bool& quit );
  76. void index();
  77. void treeInsert(TreeNode *&root, wordData newItem);
  78. void treePrint( TreeNode *root );
  79.  
  80. //Main menus
  81. int main()
  82. {
  83. //display main menu
  84. menu();
  85. return 0;
  86. }
  87.  
  88. //menu
  89. void menu()
  90. {
  91. //quit bool
  92. bool quit = false;
  93.  
  94. //menu loop
  95. do{
  96. display();
  97. option( quit );
  98. } while ( quit == false );
  99.  
  100. //return to main to end
  101. return;
  102. }
  103.  
  104. //display
  105. void display()
  106. {
  107. //displays
  108.  
  109. cout << setw(80) << setfill('=') << "=\n"
  110. << setw(40) << "WELCOME TO LAB 6"<< setw(40) << "=\n"
  111. << setw(80) << setfill('=') << "=\n" << endl;
  112.  
  113. cout << "Available options:\n\
  114. 0. INDEX - Display the first 1k index words & line numbers from a file\n\
  115. 1. QUIT - Exit the program\n\
  116. Enter keyword or option index: ";
  117. return;
  118. }
  119.  
  120. //option
  121. void option( bool& quit )
  122. {
  123. string optionInput;
  124. char firstChar;
  125.  
  126. getline( cin, optionInput);
  127.  
  128. //fix for blank input
  129. if( optionInput.length() != 0 )
  130. {
  131. firstChar = optionInput.at(0);
  132. }else{
  133. firstChar = '2';
  134. }
  135.  
  136. switch( firstChar )
  137. {
  138. case '0':
  139. case 'I':
  140. case 'i':
  141. index();
  142. break;
  143. case '1':
  144. case 'Q':
  145. case 'q':
  146. quit = true;
  147. break;
  148. default:
  149. cout << "Option not available.";
  150. break;
  151. }
  152.  
  153. return;
  154. }
  155.  
  156. //index
  157. void index()
  158. {
  159. //Variables
  160. TreeNode *root; // Our root node
  161. root = NULL; //Null for now
  162. ifstream inFile;
  163. string inputLine = "", inLine, nextWord, nextLine;
  164. // stringstream inString;
  165. int line = 1, count = 0, wordCount = 0, size = 0, wordsSize;
  166. string words[999];
  167. vector<int> lines;
  168. wordData temp;
  169.  
  170. //Declare the opperation
  171. cout << "KEY WORD INDEX\n"
  172. << setw(21) << setfill('=') << "=\n" << endl;
  173.  
  174. //loop for taking in a file
  175. do
  176. {
  177. inFile.clear();
  178. cout << "Enter input file name: ";
  179. getline( cin, inputLine);
  180. inFile.open( inputLine.c_str() );
  181. if( inFile.good() == false )
  182. {
  183. cout << "Failure to open " << inputLine << "." << endl;
  184. }
  185. } while ( inFile.good() == false );//Is the file good?
  186.  
  187. while( inFile.peek() != EOF )
  188. {
  189. if( wordCount <= 1000 )
  190. {
  191. inFile >> nextWord;
  192. for( count = 0; count < 1000; count++ )
  193. {
  194. if( ( words[count] != nextWord ) && nextWord != "" )
  195. {
  196. words[wordCount] = nextWord;
  197. wordCount++;
  198. break;
  199. }else{
  200. break;
  201. }
  202. }
  203. }else{
  204. break;
  205. }
  206. }
  207.  
  208. for( count = 0; count < 1000; count++ )
  209. {
  210. if( words[count] != "" )
  211. {
  212. wordsSize = count;
  213. break;
  214. }
  215. }
  216.  
  217. for( count = 0; count < wordsSize; count++ )
  218. {
  219. inFile.clear();
  220. inFile.seekg( 0 );
  221. line = 1;
  222. lines.clear();
  223.  
  224. while( inFile.peek() != EOF )
  225. {
  226. getline( inFile, nextLine);
  227. size = nextLine.find( words[count] );
  228. if( size != nextLine.length() )
  229. {
  230. lines.push_back( line);
  231. }
  232. line++;
  233. }
  234.  
  235. temp.word = words[count];
  236. temp.line = lines;
  237.  
  238. treeInsert( root, temp );
  239. }
  240.  
  241. treePrint( root );
  242.  
  243. //close our file
  244. inFile.clear();
  245. inFile.close();
  246. if( inFile.good() == false )
  247. {
  248. cout << "Failure to close input file." << endl;
  249. }
  250.  
  251. cout << "\n" << endl;
  252.  
  253. return;
  254. }
  255.  
  256. //insert to tree
  257. void treeInsert(TreeNode *&root, wordData newItem) {
  258. if ( root == NULL ) {
  259. root = new TreeNode( newItem );
  260. }else if ( newItem < root->item ) {
  261. treeInsert( root->left, newItem );
  262. }else{
  263. treeInsert( root->right, newItem );
  264. }
  265. return;
  266. }
  267.  
  268. //tree print
  269. void treePrint( TreeNode *root ) {
  270. if ( root != NULL )
  271. {
  272. treePrint( root->left );
  273. cout << root->item;
  274. treePrint( root->right );
  275. }
  276. return;
  277. }
Add Comment
Please, Sign In to add comment