Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************/
- /* Author: Trisha Badlu */
- /* Creation Date: 21 March 2017 */
- /* Due Date: 3 March 2017 */
- /* Course: CSC136 - 020 */
- /* Professor Name: Dr. Spiegel */
- /* Assignment: #2 */
- /* Filename: WordRec.cpp */
- /* Purpose: This file is used in order to retrieve words */
- /* from the text file and set them as unique */
- /* WordRecs, along with setting and retrieving */
- /* the counts of each WordRec. This file also */
- /* allows the p2.cpp program to use overloaded */
- /* functions to compare and print WordRecs */
- /* directly, instead of having to call the */
- /* getWord() function every time. */
- /************************************************************/
- #include <iostream>
- #include <iomanip>
- #include "WordRec.h"
- using namespace std;
- WordRec::WordRec(string w, int c){
- word = w;
- count = c;
- }
- /*************************************************************************/
- /* */
- /* Function name: setWord */
- /* Description: Sets the object's word to the specified string */
- /* Parameters: string - w */
- /* Return Value: none */
- /* */
- /*************************************************************************/
- void WordRec::setWord(string w){
- word = w;
- }
- /*************************************************************************/
- /* */
- /* Function name: setCount */
- /* Description: Sets the object's count to the specified integer */
- /* Parameters: int - c */
- /* Return Value: none */
- /* */
- /*************************************************************************/
- void WordRec::setCount(int c){
- count = c;
- }
- /*************************************************************************/
- /* */
- /* Function name: getWord */
- /* Description: Returns the word stored in the specified WordRec */
- /* Parameters: None */
- /* Return Value: string - word */
- /* */
- /*************************************************************************/
- string WordRec::getWord() const {
- return word;
- }
- /*************************************************************************/
- /* */
- /* Function name: getCount */
- /* Description: Returns the count stored in the specified WordRec */
- /* Parameters: None */
- /* Return Value: int - count */
- /* */
- /*************************************************************************/
- int WordRec::getCount() const {
- return count;
- }
- /*************************************************************************/
- /* */
- /* Function name: operator++ */
- /* Description: Returns the pre-incremented word count */
- /* Parameters: None */
- /* Return Value: WordRec - *this */
- /* */
- /*************************************************************************/
- WordRec &WordRec::operator++(){
- setCount(getCount() + 1);
- return *this;
- }
- /*************************************************************************/
- /* */
- /* Function name: operator++ */
- /* Description: Returns the post-incremented word count */
- /* Parameters: None */
- /* Return Value: WordRec - temp */
- /* */
- /*************************************************************************/
- WordRec WordRec::operator++(int){
- WordRec temp = *this;
- setCount(getCount() + 1);
- return temp;
- }
- /*************************************************************************/
- /* */
- /* Function name: operator< */
- /* Description: Checks if a WordRec's word is less than another WordRec's*/
- /* word */
- /* Parameters: const WordRec& - right */
- /* Return Value: bool - true (if less than) */
- /* false (if equal to or greater than) */
- /* */
- /*************************************************************************/
- bool WordRec::operator<(const WordRec& right) const {
- return(getWord() < right.getWord());
- }
- /*************************************************************************/
- /* */
- /* Function name: operator== */
- /* Description: Checks if a WordRec's word is equal to another WordRec's */
- /* word */
- /* Parameters: const WordRec& - right */
- /* Return Value: bool - true (if equal to) */
- /* false (if less than or greater than) */
- /* */
- /*************************************************************************/
- bool WordRec::operator==(const WordRec& right) const {
- return(getWord() == right.getWord());
- }
- /*************************************************************************/
- /* */
- /* Function name: operator<< */
- /* Description: Prints out the word and it's count of each WordRec */
- /* Parameters: ostream& - out */
- /* const WordRec& - right */
- /* Return Value: ostream& - out */
- /* */
- /*************************************************************************/
- ostream &operator<<(ostream &out, const WordRec &right){
- while(right.getWord() != ""){
- out << setw(20) << left << right.getWord() << right.getCount();
- return out;
- }
- }
- /*************************************************************************/
- /* */
- /* Function name: operator>> */
- /* Description: Reads the file's input as a WordRec instead of a string */
- /* Parameters: ifstream& - inf */
- /* WordRec& - right */
- /* Return Value: ifstream& - inf */
- /* */
- /*************************************************************************/
- ifstream &operator>>(ifstream &inf, WordRec &right){
- string word;
- inf >> word;
- right.setWord(word);
- return inf;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement