Guest User

Untitled

a guest
Apr 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5. #include <cerrno>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <sstream>
  10. #include <map>
  11. using namespace std;
  12.  
  13. //Function Declarations
  14. void menu();
  15. void display();
  16. void option( bool& quit );
  17. void index();
  18. bool sorter ( string a, string b);
  19.  
  20. //Main menus
  21. int main()
  22. {
  23. //display main menu
  24. menu();
  25. return 0;
  26. }
  27.  
  28. //menu
  29. void menu()
  30. {
  31. //quit bool
  32. bool quit = false;
  33.  
  34. //menu loop
  35. do{
  36. display();
  37. option( quit );
  38. } while ( quit == false );
  39.  
  40. //return to main to end
  41. return;
  42. }
  43.  
  44. //display
  45. void display()
  46. {
  47. //displays
  48.  
  49. cout << setw(80) << setfill('=') << "=\n"
  50. << setw(40) << "WELCOME TO LAB 6"<< setw(40) << "=\n"
  51. << setw(80) << setfill('=') << "=\n" << endl;
  52.  
  53. cout << "Available options:\n\
  54. 0. INDEX - Display the first 1k index words & line numbers from a file\n\
  55. 1. QUIT - Exit the program\n\
  56. Enter keyword or option index: ";
  57. return;
  58. }
  59.  
  60. //option
  61. void option( bool& quit )
  62. {
  63. string optionInput;
  64. char firstChar;
  65.  
  66. getline( cin, optionInput);
  67.  
  68. //fix for blank input
  69. if( optionInput.length() != 0 )
  70. {
  71. firstChar = optionInput.at(0);
  72. }else{
  73. firstChar = '2';
  74. }
  75.  
  76. switch( firstChar )
  77. {
  78. case '0':
  79. case 'I':
  80. case 'i':
  81. index();
  82. break;
  83. case '1':
  84. case 'Q':
  85. case 'q':
  86. quit = true;
  87. break;
  88. default:
  89. cout << "Option not available.";
  90. break;
  91. }
  92.  
  93. return;
  94. }
  95.  
  96. //index
  97. void index()
  98. {
  99. string symbols = "\"'().,;0123456789";
  100. ifstream inputFile;
  101. vector<string> words;
  102. map<string, vector<int> > wordsCounts;
  103. int count = 0, i, idx;
  104. int currentLine = 0;
  105. string buffer, line, inputLine;
  106. stringstream ss(line);
  107.  
  108. //loop for taking in a file
  109. do
  110. {
  111. inputFile.clear();
  112. cout << "Enter input file name: ";
  113. getline( cin, inputLine);
  114. inputFile.open( inputLine.c_str() );
  115. if( inputFile.good() == false )
  116. {
  117. cout << "Failure to open " << inputLine << "." << endl;
  118. }
  119. } while ( inputFile.good() == false );//Is the file good?
  120.  
  121. //search the file
  122. while ( inputFile.good() )
  123. {
  124. //grab evrey line
  125. getline( inputFile, line);
  126.  
  127. //scan the line
  128. while ( ss.good() && ( count++ < 1000 ) )
  129. {
  130. ++currentLine;// post incriment the line count
  131.  
  132. ss >> buffer;//read the word from the line
  133.  
  134. //Remove all the symbols
  135. for ( i = 0; i < symbols.size(); i++)
  136. {
  137. while ( ( idx = buffer.find( symbols[i] ) ) != string::npos)
  138. {
  139. buffer.erase(idx, 1);
  140. }
  141. }
  142.  
  143. //make sure the word is not blank
  144. if ( buffer == "" )
  145. {
  146. count--;
  147. continue;
  148. }
  149.  
  150. //If the word already exists then decriment
  151. if ( find(words.begin(), words.end(), buffer) != words.end() )
  152. {
  153. count--;
  154. wordsCounts[buffer].push_back(currentLine); // Current line
  155. continue;
  156. }
  157.  
  158. words.push_back(buffer); //word
  159. wordsCounts[buffer].push_back(currentLine); // Current line
  160. }
  161. }
  162.  
  163. //Alphabetize and sort length
  164. sort( words.begin(), words.end(), sorter);
  165.  
  166. //print all of the words and line numbers
  167. for ( vector<string>::iterator i = words.begin(); i != words.end(); i++)
  168. {
  169. cout << *i << " | ";
  170. for (vector<int>::iterator j = wordsCounts[*i].begin();
  171. j != wordsCounts[*i].end(); j++)
  172. {
  173. cout << *j << ", ";
  174. }
  175. cout << endl;
  176. }
  177.  
  178. //close our file
  179. inputFile.clear();
  180. inputFile.close();
  181. if( inputFile.good() == false )
  182. {
  183. cout << "Failure to close input file." << endl;
  184. }
  185.  
  186. cout << "\n" << endl;
  187.  
  188. return;
  189. }
  190.  
  191. //sort
  192. bool sorter ( string a, string b) {
  193. if ( a.size() < b.size() )
  194. {
  195. return true;
  196. } else if (b.size() < a.size()) {
  197. return false;
  198. } else {
  199. return a < b;
  200. }
  201. }
Add Comment
Please, Sign In to add comment