Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. /************************************************************/
  2. /* Author: Trisha Badlu */
  3. /* Creation Date: 21 March 2017 */
  4. /* Due Date: 3 March 2017 */
  5. /* Course: CSC136 - 020 */
  6. /* Professor Name: Dr. Spiegel */
  7. /* Assignment: #2 */
  8. /* Filename: WordRec.cpp */
  9. /* Purpose: This file is used in order to retrieve words */
  10. /* from the text file and set them as unique */
  11. /* WordRecs, along with setting and retrieving */
  12. /* the counts of each WordRec. This file also */
  13. /* allows the p2.cpp program to use overloaded */
  14. /* functions to compare and print WordRecs */
  15. /* directly, instead of having to call the */
  16. /* getWord() function every time. */
  17. /************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21. #include "WordRec.h"
  22.  
  23. using namespace std;
  24.  
  25. WordRec::WordRec(string w, int c){
  26. word = w;
  27. count = c;
  28. }
  29. /*************************************************************************/
  30. /* */
  31. /* Function name: setWord */
  32. /* Description: Sets the object's word to the specified string */
  33. /* Parameters: string - w */
  34. /* Return Value: none */
  35. /* */
  36. /*************************************************************************/
  37. void WordRec::setWord(string w){
  38. word = w;
  39. }
  40. /*************************************************************************/
  41. /* */
  42. /* Function name: setCount */
  43. /* Description: Sets the object's count to the specified integer */
  44. /* Parameters: int - c */
  45. /* Return Value: none */
  46. /* */
  47. /*************************************************************************/
  48. void WordRec::setCount(int c){
  49. count = c;
  50. }
  51. /*************************************************************************/
  52. /* */
  53. /* Function name: getWord */
  54. /* Description: Returns the word stored in the specified WordRec */
  55. /* Parameters: None */
  56. /* Return Value: string - word */
  57. /* */
  58. /*************************************************************************/
  59. string WordRec::getWord() const {
  60. return word;
  61. }
  62. /*************************************************************************/
  63. /* */
  64. /* Function name: getCount */
  65. /* Description: Returns the count stored in the specified WordRec */
  66. /* Parameters: None */
  67. /* Return Value: int - count */
  68. /* */
  69. /*************************************************************************/
  70. int WordRec::getCount() const {
  71. return count;
  72. }
  73. /*************************************************************************/
  74. /* */
  75. /* Function name: operator++ */
  76. /* Description: Returns the pre-incremented word count */
  77. /* Parameters: None */
  78. /* Return Value: WordRec - *this */
  79. /* */
  80. /*************************************************************************/
  81. WordRec &WordRec::operator++(){
  82. setCount(getCount() + 1);
  83. return *this;
  84. }
  85. /*************************************************************************/
  86. /* */
  87. /* Function name: operator++ */
  88. /* Description: Returns the post-incremented word count */
  89. /* Parameters: None */
  90. /* Return Value: WordRec - temp */
  91. /* */
  92. /*************************************************************************/
  93. WordRec WordRec::operator++(int){
  94. WordRec temp = *this;
  95. setCount(getCount() + 1);
  96. return temp;
  97. }
  98. /*************************************************************************/
  99. /* */
  100. /* Function name: operator< */
  101. /* Description: Checks if a WordRec's word is less than another WordRec's*/
  102. /* word */
  103. /* Parameters: const WordRec& - right */
  104. /* Return Value: bool - true (if less than) */
  105. /* false (if equal to or greater than) */
  106. /* */
  107. /*************************************************************************/
  108. bool WordRec::operator<(const WordRec& right) const {
  109. return(getWord() < right.getWord());
  110. }
  111. /*************************************************************************/
  112. /* */
  113. /* Function name: operator== */
  114. /* Description: Checks if a WordRec's word is equal to another WordRec's */
  115. /* word */
  116. /* Parameters: const WordRec& - right */
  117. /* Return Value: bool - true (if equal to) */
  118. /* false (if less than or greater than) */
  119. /* */
  120. /*************************************************************************/
  121. bool WordRec::operator==(const WordRec& right) const {
  122. return(getWord() == right.getWord());
  123.  
  124. }
  125. /*************************************************************************/
  126. /* */
  127. /* Function name: operator<< */
  128. /* Description: Prints out the word and it's count of each WordRec */
  129. /* Parameters: ostream& - out */
  130. /* const WordRec& - right */
  131. /* Return Value: ostream& - out */
  132. /* */
  133. /*************************************************************************/
  134. ostream &operator<<(ostream &out, const WordRec &right){
  135. while(right.getWord() != ""){
  136. out << setw(20) << left << right.getWord() << right.getCount();
  137. return out;
  138. }
  139. }
  140. /*************************************************************************/
  141. /* */
  142. /* Function name: operator>> */
  143. /* Description: Reads the file's input as a WordRec instead of a string */
  144. /* Parameters: ifstream& - inf */
  145. /* WordRec& - right */
  146. /* Return Value: ifstream& - inf */
  147. /* */
  148. /*************************************************************************/
  149. ifstream &operator>>(ifstream &inf, WordRec &right){
  150. string word;
  151. inf >> word;
  152. right.setWord(word);
  153. return inf;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement