SHARE
TWEET

Untitled

a guest Apr 18th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************************
  2. Author:     Dr. Daniel Spiegel, Updated by: Trisha Badlu
  3. Creation Date:  19 April 2017                            
  4. Due Date:   26 April 2017
  5. Assignment:     #4
  6. Filename:       p4.cpp                                  
  7. Course:     CSC136 - 020                                    
  8. Professor Name: Dr. Spiegel  
  9. Purpose:    The purpose of this file is to read a file
  10.         into a Linked List and give the user an option
  11.         to print all words with their appearance, print
  12.         words with a specified amount of appearances,
  13.         show the first N characters of each word, find
  14.         a word, or remove a word.
  15. ***************************************************************/
  16. // File: p4.cpp
  17. // Prepared by Dr. Spiegel with thanks to Jamie Mason
  18. // Application to test out the functionality of the LinkedList class
  19. // Permits user to print info on words of a file that are stored with the number
  20. //  of times they appeared.
  21.  
  22. #include <iostream>
  23. #include <string>
  24. #include <fstream>
  25. #include <iomanip>
  26. #include "LinkedList.h"
  27. #include "WordRec.h"
  28.  
  29. using namespace std;
  30.  
  31. // Saves a lot of redundant typing.
  32. typedef LinkedList<WordRec> WordList;  
  33. /***************************************************************
  34. Function name: openFile (facilitator)          
  35. Description: Opens the specified file        
  36. Parameters: ifstream& - inf (import/export)        
  37. Return Value: bool - true (if file exists and opens)
  38.              false (if file doesn't exist)
  39. ***************************************************************/
  40. bool openFile(ifstream &inf);
  41. /***************************************************************
  42. Function name: menu (facilitator)          
  43. Description: Displays prompt asking for a character        
  44. Parameters: none                                              
  45. Return Value: char - SelectedCharacter
  46. ***************************************************************/
  47. char menu();
  48. /***************************************************************
  49. Function name: numberPrompt (facilitator)          
  50. Description: Displays prompt asking for a number        
  51. Parameters: none                                              
  52. Return Value: int - numberValue
  53. ***************************************************************/
  54. int numberPrompt();
  55. /***************************************************************
  56. Function name: stringPrompt (facilitator)          
  57. Description: Displays prompt asking for a string      
  58. Parameters: none                                              
  59. Return Value: string - word
  60. ***************************************************************/
  61. string stringPrompt();
  62. /***************************************************************
  63. Function name: find (facilitator)          
  64. Description: Searches for a word from file that's the same as
  65.          user's input
  66. Parameters: const WordList& - TheWords (import/export)            
  67. Return Value: none
  68. ***************************************************************/
  69. void find(const WordList &TheWords);
  70. /***************************************************************
  71. Function name: substring (facilitator)          
  72. Description: Output the first n characters of each word
  73. Parameters: const WordList& - TheWords (import/export)  
  74. Return Value: none
  75. ***************************************************************/
  76. void substring(const WordList &TheWords);
  77. /***************************************************************
  78. Function name: mult (facilitator)          
  79. Description: Print the words from the file with a given
  80.          multiplicity
  81. Parameters: const WordList& - TheWords (import/export)        
  82. Return Value: none
  83. ***************************************************************/      
  84. void mult(const WordList &TheWords);
  85. /***************************************************************
  86. Function name: multMchars (facilitator)          
  87. Description: Prints the first m characters of words that
  88.          appeared n times
  89. Parameters: const WordList& - TheWords (import/export)  
  90. Return Value: none
  91. ***************************************************************/
  92. void multMchars(const WordList &TheWords);
  93. /***************************************************************
  94. Function name: removeWord (facilitator)          
  95. Description: Remove occurrences of a word in the list
  96. Parameters: const WordList& - TheWords (import/export)      
  97. Return Value: none
  98. ***************************************************************/
  99. void removeWord(WordList &TheWords);
  100. /***************************************************************
  101. Function name: regulateOutput (facilitator)          
  102. Description: Regulates the output based on user input
  103. Parameters: const WordList& - TheWords (import/export)    
  104. Return Value: none
  105. ***************************************************************/
  106. void regulateOutput(WordList &TheWords);
  107. /***************************************************************
  108. Function name: operator>> (inspector)          
  109. Description: Inputs file into a LinkedList
  110. Parameters: ifstream& - inf (import/export)
  111.         WordList& - right (import/export)
  112. Return Value: none
  113. ***************************************************************/
  114.  ifstream &operator>>(ifstream &inf, WordList &right);
  115.  
  116.  
  117. int main()
  118. {
  119.   //Handle for accessing the data file
  120.   ifstream inf;
  121.   //Variable that holds the character the the user input
  122.   char character;
  123.   //Variable that contains a true/false value pertaining to whether the
  124.   //file was opened or not
  125.   bool result;
  126.   //Instantiate a WordList object
  127.   LinkedList<WordRec> TheWords;
  128.  
  129.   string word;
  130.   //Open File
  131.   result=openFile(inf);
  132.   //Output if the file does not exist
  133.   if(!result)
  134.     {
  135.       cout<<"Error:File does not exist.\n";
  136.       return 0;
  137.     }
  138.   //Input file information into thew WordList
  139.   inf >> TheWords;  
  140.   //Regulate Output
  141.   regulateOutput(TheWords);
  142.   //Close the file
  143.   inf.close();
  144.   return 0;
  145. }
  146.  
  147. //Functions
  148.  
  149. //Opens the file
  150. bool openFile(ifstream &inf)
  151. { //Variable to hold the file name
  152.   string filename;
  153.   //Ask for File Name
  154.   cout<<"Enter the data file name >";
  155.   cin>>filename;
  156.   inf.open(filename.c_str());
  157.   return(inf);
  158. }
  159.  
  160. //Prompt asking for a character
  161. char menu()
  162. {
  163.   char SelectedCharacter;
  164.   cout<<"\n";
  165.   cout<<"A)ll Words in File with their Multiplicity."<<endl;
  166.   cout<<"P)rint all Words Appearing N Times"<<endl;
  167.   cout<<"S)ubstring: First N letters of Each Word"<<endl;
  168.   cout<<"F)ind a Word "<<endl;
  169.   cout<<"M)Characters of Words Appearing N Times"<< endl;
  170.   cout<<"R)emove a word"<<endl;
  171.   cout<<"Q)uit"<<endl;
  172.   cin>>SelectedCharacter;
  173.   return SelectedCharacter;
  174. }
  175.  
  176. //Prompt asking for a number
  177. int numberPrompt()
  178. {
  179.   int numberValue=0;
  180.   cout<<"\n";
  181.   cout<<"Enter an integer value >";
  182.   cin>>numberValue;
  183.   return numberValue;
  184. }
  185.  
  186. //Prompt asking for a string
  187. string stringPrompt()
  188. { string word;
  189.   cout<<"\n";
  190.   cout<<"Enter a word >" ;
  191.   cin>>word;
  192.   return word;
  193. }
  194.  
  195. //Searches for a word from the file using the iterator interface in the WordList
  196. void find(const WordList &TheWords)
  197. { string findWord=stringPrompt();
  198.   listItr<WordRec> it(TheWords);
  199.   bool found=false;
  200.   cout << endl;
  201.   for (it.start();it.more();it.next())
  202.   {
  203.       if (it.value().getWord()==findWord)
  204.       { cout<< it.value().getWord() << " appeared " <<
  205.     it.value().getCount() << " times in the file\n";
  206.     found=true;
  207.       }
  208.   }
  209.   if (!found)
  210.     cout<<"The word " << findWord <<" does not exist in the file." << endl;
  211. }
  212.  
  213. //Outputs substrings
  214. void substring(const WordList &TheWords)
  215. { int number=numberPrompt();
  216.   while (number<=0)
  217.   { cout<<"\n";
  218.     cout<<"Error: Invalid Entry"<<endl;
  219.     number=numberPrompt();
  220.   }
  221.   listItr<WordRec> it(TheWords);
  222.   cout<<"\n";
  223.   cout<<"Substring:\n\n";
  224.   cout<<setw(15)<<"Words"<<endl;
  225.   for (it.start();it.more();it.next())
  226.     cout<< setw(15) << it.value()(number) << endl;
  227. }
  228.        
  229. //Print the multiplicity of the words from the file
  230. void mult(const WordList &TheWords)
  231. { int number=numberPrompt();
  232.   while (number<=0)
  233.   {
  234.       cout<<"\n";
  235.       cout<<"Error: Invalid Entry"<<endl;
  236.       number=numberPrompt();
  237.   }
  238.   listItr<WordRec> it(TheWords);
  239.   bool print=false;
  240.   for (it.start();it.more();it.next())
  241.   { if (it.value().getCount()==number)
  242.     { if(!print)
  243.       { cout<<"\n";
  244.     cout<<"Words appearing "<<number<<" times:\n\n";
  245.     cout<<setw(15)<<"Words"<<endl;
  246.     print=true;
  247.       }
  248.       cout<<it.value() << endl;
  249.     }
  250.   }
  251.   if(!print)
  252.   { cout<<"\nNo word in the file has this multiplicity.\n";
  253.   }
  254. }
  255.  
  256. //Print the first n characters of the words from the file
  257. //  that appeared m times
  258. void multMchars(const WordList &TheWords)
  259. { listItr<WordRec> it(TheWords);
  260.   double number;
  261.   cout << "Enter double in form n.m >";
  262.   cin >>  number;
  263.   int mult=int(number),numChars=((number+0.001-mult)*10);
  264.   bool print=false;
  265.   for (it.start();it.more();it.next())
  266.   { if (it.value().getCount()==mult)
  267.     { if(!print)
  268.       { cout<<"\n";
  269.     cout<<"First "<<numChars<<" characters of words appearing "
  270.         <<mult<<" times:\n";
  271.     cout<<setw(15)<<"Words"<<endl;
  272.     cout<<"\n";
  273.     print=true;
  274.       }
  275.       cout<<setw(15)<<it.value()(numChars) << endl;
  276.     }
  277.   }
  278.   if(!print)
  279.   { cout<<"\n";
  280.     cout<<"No word in the file has this multiplicity.";
  281.   }
  282. }
  283.  
  284. // Find a word in the list. Return how many times it was added, 0 if not found.
  285. WordRec* findWordInList(const WordList &TheWords,string word)
  286. { listItr<WordRec> it(TheWords);
  287.   bool flag=false;
  288.   for (it.start();it.more();it.next())
  289.     if (it.value().getWord()==word)
  290.       return(&it.value());    // Return a pointer to the found WordRec
  291.   return(0);              // Return NULL
  292. }
  293.  
  294. // ********************** Must complete this function *******************
  295. // Remove occurrences of a word in the list
  296. void removeWord(WordList &TheWords)
  297. { listItr<WordRec> it(TheWords);
  298.   string word;
  299.   int num;
  300.   cout << "\n";
  301.   cout << "Enter the Word to Remove >";
  302.   cin >> word;
  303.  
  304.   for (it.start();it.more();it.next())
  305.     if (it.value().getWord()==word){
  306.        if(it.value().getCount() == 1)
  307.          TheWords.remove(word);
  308.        else{
  309.          cout << endl << it.value().getWord() << " has appeared "
  310.          << it.value().getCount() << " times" << endl << endl;
  311.          cout << "How many to delete >";
  312.          cin >> num;
  313.      if(num <= 0 || num > it.value().getCount())
  314.        cout << "Error: Invalid Entry" << endl;
  315.      else if(num == it.value().getCount())
  316.        TheWords.remove(word);
  317.      else
  318.         for(int count = 0; count < num; count++)
  319.         it.value()--;
  320.        }
  321.        break;
  322.     }
  323. }
  324.  
  325. //Regulates the output based on user input
  326. void regulateOutput(WordList &TheWords)
  327. { int number;
  328.   string wordFind;
  329.   char inputCharacter=menu();
  330.   while (inputCharacter!='Q' && inputCharacter!='q')
  331.   {
  332.       switch(inputCharacter)
  333.       {
  334.     case 'a':
  335.         case 'A':
  336.           cout << setw(15) << "Word" << setw(18) << "Appearances\n" << TheWords;
  337.           break;
  338.         case 'p':
  339.         case 'P':
  340.       mult(TheWords);
  341.           break;
  342.     case 's':
  343.     case 'S':
  344.       substring(TheWords);
  345.       break;
  346.         case 'f':
  347.         case 'F':
  348.       find(TheWords);
  349.           break;
  350.     case 'M':
  351.     case 'm':
  352.       multMchars(TheWords);
  353.       break;
  354.     case 'R':
  355.     case 'r':
  356.       removeWord(TheWords);
  357.       break;
  358.         default:
  359.       cout<<"\n";
  360.           cout<<"Error: Invalid Entry"<<endl;
  361.           break;
  362.       }
  363.       inputCharacter=menu();
  364.   }
  365. }
  366.  
  367. // *************** Must complete this function *******************
  368. // Inputs file into the LinkedList<WordRec>
  369. // If the word read is present, orderedInsert returns a pointer to its WordRec.
  370. //  In that case, the word was NOT inserted. We increment the WordRec's counter.
  371. //  Otherwise, the word was inserted.
  372. ifstream &operator>>(ifstream &inf, WordList &right)
  373. { WordRec next;
  374.   listItr<WordRec> it(right);
  375.   while(inf >> next){
  376.     if(right.orderedInsert(next) != NULL){
  377.     for (it.start();it.more();it.next())
  378.     if (it.value().getWord()==next.getWord())
  379.        it.value()++;
  380.     }
  381.   }
  382.   return(inf);
  383. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top