Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************
- Author: Dr. Daniel Spiegel, Updated by: Trisha Badlu
- Creation Date: 19 April 2017
- Due Date: 26 April 2017
- Assignment: #4
- Filename: p4.cpp
- Course: CSC136 - 020
- Professor Name: Dr. Spiegel
- Purpose: The purpose of this file is to read a file
- into a Linked List and give the user an option
- to print all words with their appearance, print
- words with a specified amount of appearances,
- show the first N characters of each word, find
- a word, or remove a word.
- ***************************************************************/
- // File: p4.cpp
- // Prepared by Dr. Spiegel with thanks to Jamie Mason
- // Application to test out the functionality of the LinkedList class
- // Permits user to print info on words of a file that are stored with the number
- // of times they appeared.
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- #include "LinkedList.h"
- #include "WordRec.h"
- using namespace std;
- // Saves a lot of redundant typing.
- typedef LinkedList<WordRec> WordList;
- /***************************************************************
- Function name: openFile (facilitator)
- Description: Opens the specified file
- Parameters: ifstream& - inf (import/export)
- Return Value: bool - true (if file exists and opens)
- false (if file doesn't exist)
- ***************************************************************/
- bool openFile(ifstream &inf);
- /***************************************************************
- Function name: menu (facilitator)
- Description: Displays prompt asking for a character
- Parameters: none
- Return Value: char - SelectedCharacter
- ***************************************************************/
- char menu();
- /***************************************************************
- Function name: numberPrompt (facilitator)
- Description: Displays prompt asking for a number
- Parameters: none
- Return Value: int - numberValue
- ***************************************************************/
- int numberPrompt();
- /***************************************************************
- Function name: stringPrompt (facilitator)
- Description: Displays prompt asking for a string
- Parameters: none
- Return Value: string - word
- ***************************************************************/
- string stringPrompt();
- /***************************************************************
- Function name: find (facilitator)
- Description: Searches for a word from file that's the same as
- user's input
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void find(const WordList &TheWords);
- /***************************************************************
- Function name: substring (facilitator)
- Description: Output the first n characters of each word
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void substring(const WordList &TheWords);
- /***************************************************************
- Function name: mult (facilitator)
- Description: Print the words from the file with a given
- multiplicity
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void mult(const WordList &TheWords);
- /***************************************************************
- Function name: multMchars (facilitator)
- Description: Prints the first m characters of words that
- appeared n times
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void multMchars(const WordList &TheWords);
- /***************************************************************
- Function name: removeWord (facilitator)
- Description: Remove occurrences of a word in the list
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void removeWord(WordList &TheWords);
- /***************************************************************
- Function name: regulateOutput (facilitator)
- Description: Regulates the output based on user input
- Parameters: const WordList& - TheWords (import/export)
- Return Value: none
- ***************************************************************/
- void regulateOutput(WordList &TheWords);
- /***************************************************************
- Function name: operator>> (inspector)
- Description: Inputs file into a LinkedList
- Parameters: ifstream& - inf (import/export)
- WordList& - right (import/export)
- Return Value: none
- ***************************************************************/
- ifstream &operator>>(ifstream &inf, WordList &right);
- int main()
- {
- //Handle for accessing the data file
- ifstream inf;
- //Variable that holds the character the the user input
- char character;
- //Variable that contains a true/false value pertaining to whether the
- //file was opened or not
- bool result;
- //Instantiate a WordList object
- LinkedList<WordRec> TheWords;
- string word;
- //Open File
- result=openFile(inf);
- //Output if the file does not exist
- if(!result)
- {
- cout<<"Error:File does not exist.\n";
- return 0;
- }
- //Input file information into thew WordList
- inf >> TheWords;
- //Regulate Output
- regulateOutput(TheWords);
- //Close the file
- inf.close();
- return 0;
- }
- //Functions
- //Opens the file
- bool openFile(ifstream &inf)
- { //Variable to hold the file name
- string filename;
- //Ask for File Name
- cout<<"Enter the data file name >";
- cin>>filename;
- inf.open(filename.c_str());
- return(inf);
- }
- //Prompt asking for a character
- char menu()
- {
- char SelectedCharacter;
- cout<<"\n";
- cout<<"A)ll Words in File with their Multiplicity."<<endl;
- cout<<"P)rint all Words Appearing N Times"<<endl;
- cout<<"S)ubstring: First N letters of Each Word"<<endl;
- cout<<"F)ind a Word "<<endl;
- cout<<"M)Characters of Words Appearing N Times"<< endl;
- cout<<"R)emove a word"<<endl;
- cout<<"Q)uit"<<endl;
- cin>>SelectedCharacter;
- return SelectedCharacter;
- }
- //Prompt asking for a number
- int numberPrompt()
- {
- int numberValue=0;
- cout<<"\n";
- cout<<"Enter an integer value >";
- cin>>numberValue;
- return numberValue;
- }
- //Prompt asking for a string
- string stringPrompt()
- { string word;
- cout<<"\n";
- cout<<"Enter a word >" ;
- cin>>word;
- return word;
- }
- //Searches for a word from the file using the iterator interface in the WordList
- void find(const WordList &TheWords)
- { string findWord=stringPrompt();
- listItr<WordRec> it(TheWords);
- bool found=false;
- cout << endl;
- for (it.start();it.more();it.next())
- {
- if (it.value().getWord()==findWord)
- { cout<< it.value().getWord() << " appeared " <<
- it.value().getCount() << " times in the file\n";
- found=true;
- }
- }
- if (!found)
- cout<<"The word " << findWord <<" does not exist in the file." << endl;
- }
- //Outputs substrings
- void substring(const WordList &TheWords)
- { int number=numberPrompt();
- while (number<=0)
- { cout<<"\n";
- cout<<"Error: Invalid Entry"<<endl;
- number=numberPrompt();
- }
- listItr<WordRec> it(TheWords);
- cout<<"\n";
- cout<<"Substring:\n\n";
- cout<<setw(15)<<"Words"<<endl;
- for (it.start();it.more();it.next())
- cout<< setw(15) << it.value()(number) << endl;
- }
- //Print the multiplicity of the words from the file
- void mult(const WordList &TheWords)
- { int number=numberPrompt();
- while (number<=0)
- {
- cout<<"\n";
- cout<<"Error: Invalid Entry"<<endl;
- number=numberPrompt();
- }
- listItr<WordRec> it(TheWords);
- bool print=false;
- for (it.start();it.more();it.next())
- { if (it.value().getCount()==number)
- { if(!print)
- { cout<<"\n";
- cout<<"Words appearing "<<number<<" times:\n\n";
- cout<<setw(15)<<"Words"<<endl;
- print=true;
- }
- cout<<it.value() << endl;
- }
- }
- if(!print)
- { cout<<"\nNo word in the file has this multiplicity.\n";
- }
- }
- //Print the first n characters of the words from the file
- // that appeared m times
- void multMchars(const WordList &TheWords)
- { listItr<WordRec> it(TheWords);
- double number;
- cout << "Enter double in form n.m >";
- cin >> number;
- int mult=int(number),numChars=((number+0.001-mult)*10);
- bool print=false;
- for (it.start();it.more();it.next())
- { if (it.value().getCount()==mult)
- { if(!print)
- { cout<<"\n";
- cout<<"First "<<numChars<<" characters of words appearing "
- <<mult<<" times:\n";
- cout<<setw(15)<<"Words"<<endl;
- cout<<"\n";
- print=true;
- }
- cout<<setw(15)<<it.value()(numChars) << endl;
- }
- }
- if(!print)
- { cout<<"\n";
- cout<<"No word in the file has this multiplicity.";
- }
- }
- // Find a word in the list. Return how many times it was added, 0 if not found.
- WordRec* findWordInList(const WordList &TheWords,string word)
- { listItr<WordRec> it(TheWords);
- bool flag=false;
- for (it.start();it.more();it.next())
- if (it.value().getWord()==word)
- return(&it.value()); // Return a pointer to the found WordRec
- return(0); // Return NULL
- }
- // ********************** Must complete this function *******************
- // Remove occurrences of a word in the list
- void removeWord(WordList &TheWords)
- { listItr<WordRec> it(TheWords);
- string word;
- int num;
- cout << "\n";
- cout << "Enter the Word to Remove >";
- cin >> word;
- for (it.start();it.more();it.next())
- if (it.value().getWord()==word){
- if(it.value().getCount() == 1)
- TheWords.remove(word);
- else{
- cout << endl << it.value().getWord() << " has appeared "
- << it.value().getCount() << " times" << endl << endl;
- cout << "How many to delete >";
- cin >> num;
- if(num <= 0 || num > it.value().getCount())
- cout << "Error: Invalid Entry" << endl;
- else if(num == it.value().getCount())
- TheWords.remove(word);
- else
- for(int count = 0; count < num; count++)
- it.value()--;
- }
- break;
- }
- }
- //Regulates the output based on user input
- void regulateOutput(WordList &TheWords)
- { int number;
- string wordFind;
- char inputCharacter=menu();
- while (inputCharacter!='Q' && inputCharacter!='q')
- {
- switch(inputCharacter)
- {
- case 'a':
- case 'A':
- cout << setw(15) << "Word" << setw(18) << "Appearances\n" << TheWords;
- break;
- case 'p':
- case 'P':
- mult(TheWords);
- break;
- case 's':
- case 'S':
- substring(TheWords);
- break;
- case 'f':
- case 'F':
- find(TheWords);
- break;
- case 'M':
- case 'm':
- multMchars(TheWords);
- break;
- case 'R':
- case 'r':
- removeWord(TheWords);
- break;
- default:
- cout<<"\n";
- cout<<"Error: Invalid Entry"<<endl;
- break;
- }
- inputCharacter=menu();
- }
- }
- // *************** Must complete this function *******************
- // Inputs file into the LinkedList<WordRec>
- // If the word read is present, orderedInsert returns a pointer to its WordRec.
- // In that case, the word was NOT inserted. We increment the WordRec's counter.
- // Otherwise, the word was inserted.
- ifstream &operator>>(ifstream &inf, WordList &right)
- { WordRec next;
- listItr<WordRec> it(right);
- while(inf >> next){
- if(right.orderedInsert(next) != NULL){
- for (it.start();it.more();it.next())
- if (it.value().getWord()==next.getWord())
- it.value()++;
- }
- }
- return(inf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement