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 <ostream>
- #include <algorithm>
- #include <vector>
- #include <sstream>
- using namespace std;
- //Data Dec
- typedef struct {
- string word;
- vector<int> line;
- } wordData;
- //overide for cout vector of wordData
- ostream & operator<<( ostream & out, wordData &data ) {
- int count = 0;
- out << data.word << " | ";
- for( count = 0; count < data.line.size(); count++ )
- {
- if( count == 0 ){
- out << data.line[count];
- }else{
- out << ", " << data.line[count];
- }
- }
- out << "\n";
- return out;
- }
- //overide for cout vector of wordData
- bool operator<( wordData &dataA, wordData &dataB ) {
- if( dataA.word.length() < dataB.word.length() )
- {
- return true;
- }else if( dataA.word.length() == dataB.word.length() ){
- if( toupper( dataA.word.at(0) ) < toupper( dataB.word.at(0) ) )
- {
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- return false;
- }
- //my tree
- struct TreeNode {
- wordData item;
- TreeNode *left;
- TreeNode *right;
- TreeNode( wordData incert )
- {
- item = incert;
- left = NULL;
- right = NULL;
- }
- }; // end Treenode
- //Function Declarations
- void menu();
- void display();
- void option( bool& quit );
- void index();
- void treeInsert(TreeNode *&root, wordData newItem);
- void treePrint( TreeNode *root );
- //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()
- {
- //Variables
- TreeNode *root; // Our root node
- root = NULL; //Null for now
- ifstream inFile;
- string inputLine = "", inLine, nextWord, nextLine;
- // stringstream inString;
- int line = 1, count = 0, wordCount = 0, size = 0, wordsSize;
- string words[999];
- vector<int> lines;
- wordData temp;
- //Declare the opperation
- cout << "KEY WORD INDEX\n"
- << setw(21) << setfill('=') << "=\n" << endl;
- //loop for taking in a file
- do
- {
- inFile.clear();
- cout << "Enter input file name: ";
- getline( cin, inputLine);
- inFile.open( inputLine.c_str() );
- if( inFile.good() == false )
- {
- cout << "Failure to open " << inputLine << "." << endl;
- }
- } while ( inFile.good() == false );//Is the file good?
- while( inFile.peek() != EOF )
- {
- if( wordCount <= 1000 )
- {
- inFile >> nextWord;
- for( count = 0; count < 1000; count++ )
- {
- if( ( words[count] != nextWord ) && nextWord != "" )
- {
- words[wordCount] = nextWord;
- wordCount++;
- break;
- }else{
- break;
- }
- }
- }else{
- break;
- }
- }
- for( count = 0; count < 1000; count++ )
- {
- if( words[count] != "" )
- {
- wordsSize = count;
- break;
- }
- }
- for( count = 0; count < wordsSize; count++ )
- {
- inFile.clear();
- inFile.seekg( 0 );
- line = 1;
- lines.clear();
- while( inFile.peek() != EOF )
- {
- getline( inFile, nextLine);
- size = nextLine.find( words[count] );
- if( size != nextLine.length() )
- {
- lines.push_back( line);
- }
- line++;
- }
- temp.word = words[count];
- temp.line = lines;
- treeInsert( root, temp );
- }
- treePrint( root );
- //close our file
- inFile.clear();
- inFile.close();
- if( inFile.good() == false )
- {
- cout << "Failure to close input file." << endl;
- }
- cout << "\n" << endl;
- return;
- }
- //insert to tree
- void treeInsert(TreeNode *&root, wordData newItem) {
- if ( root == NULL ) {
- root = new TreeNode( newItem );
- }else if ( newItem < root->item ) {
- treeInsert( root->left, newItem );
- }else{
- treeInsert( root->right, newItem );
- }
- return;
- }
- //tree print
- void treePrint( TreeNode *root ) {
- if ( root != NULL )
- {
- treePrint( root->left );
- cout << root->item;
- treePrint( root->right );
- }
- return;
- }
Add Comment
Please, Sign In to add comment