Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- #include <cerrno>
- #include <vector>
- #include <algorithm>
- #include <vector>
- #include <sstream>
- #include <map>
- using namespace std;
- //Function Declarations
- void menu();
- void display();
- void option( bool& quit );
- void index();
- bool sorter ( string a, string b);
- //Main menus
- int main()
- {
- //display main menu
- menu();
- return 0;
- }
- //menu
- void menu()
- {
- //quit bool
- bool quit = false;
- //menu loop
- do{
- display();
- option( quit );
- } while ( quit == false );
- //return to main to end
- return;
- }
- //display
- void display()
- {
- //displays
- cout << setw(80) << setfill('=') << "=\n"
- << setw(40) << "WELCOME TO LAB 6"<< setw(40) << "=\n"
- << setw(80) << setfill('=') << "=\n" << endl;
- cout << "Available options:\n\
- 0. INDEX - Display the first 1k index words & line numbers from a file\n\
- 1. QUIT - Exit the program\n\
- Enter keyword or option index: ";
- return;
- }
- //option
- void option( bool& quit )
- {
- string optionInput;
- char firstChar;
- getline( cin, optionInput);
- //fix for blank input
- if( optionInput.length() != 0 )
- {
- firstChar = optionInput.at(0);
- }else{
- firstChar = '2';
- }
- switch( firstChar )
- {
- case '0':
- case 'I':
- case 'i':
- index();
- break;
- case '1':
- case 'Q':
- case 'q':
- quit = true;
- break;
- default:
- cout << "Option not available.";
- break;
- }
- return;
- }
- //index
- void index()
- {
- string symbols = "\"'().,;0123456789";
- ifstream inputFile;
- vector<string> words;
- map<string, vector<int> > wordsCounts;
- int count = 0, i, idx;
- int currentLine = 0;
- string buffer, line, inputLine;
- stringstream ss(line);
- //loop for taking in a file
- do
- {
- inputFile.clear();
- cout << "Enter input file name: ";
- getline( cin, inputLine);
- inputFile.open( inputLine.c_str() );
- if( inputFile.good() == false )
- {
- cout << "Failure to open " << inputLine << "." << endl;
- }
- } while ( inputFile.good() == false );//Is the file good?
- //search the file
- while ( inputFile.good() )
- {
- //grab evrey line
- getline( inputFile, line);
- //scan the line
- while ( ss.good() && ( count++ < 1000 ) )
- {
- ++currentLine;// post incriment the line count
- ss >> buffer;//read the word from the line
- //Remove all the symbols
- for ( i = 0; i < symbols.size(); i++)
- {
- while ( ( idx = buffer.find( symbols[i] ) ) != string::npos)
- {
- buffer.erase(idx, 1);
- }
- }
- //make sure the word is not blank
- if ( buffer == "" )
- {
- count--;
- continue;
- }
- //If the word already exists then decriment
- if ( find(words.begin(), words.end(), buffer) != words.end() )
- {
- count--;
- wordsCounts[buffer].push_back(currentLine); // Current line
- continue;
- }
- words.push_back(buffer); //word
- wordsCounts[buffer].push_back(currentLine); // Current line
- }
- }
- //Alphabetize and sort length
- sort( words.begin(), words.end(), sorter);
- //print all of the words and line numbers
- for ( vector<string>::iterator i = words.begin(); i != words.end(); i++)
- {
- cout << *i << " | ";
- for (vector<int>::iterator j = wordsCounts[*i].begin();
- j != wordsCounts[*i].end(); j++)
- {
- cout << *j << ", ";
- }
- cout << endl;
- }
- //close our file
- inputFile.clear();
- inputFile.close();
- if( inputFile.good() == false )
- {
- cout << "Failure to close input file." << endl;
- }
- cout << "\n" << endl;
- return;
- }
- //sort
- bool sorter ( string a, string b) {
- if ( a.size() < b.size() )
- {
- return true;
- } else if (b.size() < a.size()) {
- return false;
- } else {
- return a < b;
- }
- }
Add Comment
Please, Sign In to add comment