Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. /////////////////
  2. // BagInterface.h
  3. /////////////////
  4. #ifndef _BAG_INTERFACE
  5. #define _BAG_INTERFACE
  6.  
  7. #include <vector>
  8. using namespace std;
  9.  
  10. template<class ItemType>
  11. class BagInterface
  12. {
  13. public:
  14. virtual int getCurrentSize() const = 0;
  15. virtual bool isEmpty() const = 0;
  16. virtual bool add(const ItemType& newEntry) = 0;
  17. virtual bool remove(const ItemType& anEntry) = 0;
  18. virtual void clear() = 0;
  19. virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
  20. virtual bool contains(const ItemType& anEntry) const = 0;
  21. virtual vector<ItemType> toVector() const = 0;
  22. };
  23.  
  24. #endif
  25.  
  26.  
  27. //////////////////
  28. //Bag.h
  29. //////////////////
  30. #ifndef _BAG
  31. #define _BAG
  32.  
  33. #include "BagInterface.h"
  34.  
  35. template<class T>
  36. class Bag : public BagInterface<T>
  37. {
  38. private:
  39. static const int DEFAULT_BAG_SIZE = 6;
  40. T items[DEFAULT_BAG_SIZE]; // array of bag items
  41. int itemCount; // current count of bag items
  42. int maxItems; // max capacity of the bag
  43. int getIndexOf(const T& target) const;
  44.  
  45. public:
  46. Bag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE) { }
  47. int getCurrentSize() const { return itemCount; }
  48. bool isEmpty() const { return itemCount == 0; }
  49. bool add(const T& newEntry);
  50. bool remove(const T& anEntry);
  51. void clear();
  52. bool contains(const T& anEntry) const;
  53. int getFrequencyOf(const T& anEntry) const;
  54. vector<T> toVector() const;
  55. }; // end Bag
  56.  
  57. template<class T>
  58. bool Bag<T>::add(const T& newEntry) {
  59. bool hasRoomToAdd = (itemCount < maxItems);
  60. if (hasRoomToAdd) {
  61. items[itemCount] = newEntry;
  62. itemCount++;
  63. }
  64. return hasRoomToAdd;
  65. }
  66.  
  67. template<class T>
  68. bool Bag<T>::remove(const T& anEntry) {
  69. int locatedIndex = getIndexOf(anEntry);
  70. bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
  71. if (canRemoveItem) {
  72. itemCount--;
  73. items[locatedIndex] = items[itemCount];
  74. }
  75.  
  76. return canRemoveItem;
  77. } // end remove
  78.  
  79. template<class T>
  80. void Bag<T>::clear() {
  81. itemCount = 0;
  82. }
  83.  
  84. template<class T>
  85. int Bag<T>::getFrequencyOf(const T& anEntry) const {
  86. int frequency = 0;
  87. int searchIndex = 0;
  88. while (searchIndex < itemCount) {
  89. if (items[searchIndex] == anEntry) {
  90. frequency++;
  91. }
  92. searchIndex++;
  93. }
  94. return frequency;
  95. }
  96.  
  97. template<class T>
  98. bool Bag<T>::contains(const T& anEntry) const {
  99. return getIndexOf(anEntry) > -1;
  100. }
  101.  
  102.  
  103. template<class ItemType>
  104. vector<ItemType> Bag<ItemType>::toVector() const
  105. {
  106. vector<ItemType> bagContents;
  107. for (int i = 0; i < itemCount; i++)
  108. bagContents.push_back(items[i]);
  109. return bagContents;
  110. } // end toVector
  111.  
  112. // private
  113. template<class ItemType>
  114. int Bag<ItemType>::getIndexOf(const ItemType& target) const
  115. {
  116. bool found = false;
  117. int result = -1;
  118. int searchIndex = 0;
  119. // if the bag is empty, itemCount is zero, so loop is skipped
  120. while (!found && (searchIndex < itemCount))
  121. {
  122. if (items[searchIndex] == target)
  123. {
  124. found = true;
  125. result = searchIndex;
  126. }
  127. else
  128. {
  129. searchIndex++;
  130. } // end if
  131. } // end while
  132.  
  133. return result;
  134. } // end getIndexOf
  135. #endif
  136.  
  137.  
  138. ////////////////////
  139. //usingBag.cpp
  140. ////////////////////
  141. #include <iostream> // For cout and cin
  142. #include <string> // For string objects
  143. #include "Bag.h" // For ADT bag
  144. using namespace std;
  145.  
  146. int main()
  147. {
  148. string clubs[] = { "Joker", "Ace", "Two", "Three",
  149. "Four", "Five", "Six", "Seven",
  150. "Eight", "Nine", "Ten", "Jack",
  151. "Queen", "King" };
  152. // Create our bag to hold cards.
  153. Bag<string> grabBag;
  154. // Place six cards in the bag.
  155. grabBag.add(clubs[1]);
  156. grabBag.add(clubs[2]);
  157. grabBag.add(clubs[4]);
  158. grabBag.add(clubs[8]);
  159. grabBag.add(clubs[10]);
  160. grabBag.add(clubs[12]);
  161. // Get friend’s guess and check it.
  162. int guess = 0;
  163. while (!grabBag.isEmpty())
  164. {
  165. cout << "What is your guess?"
  166. << "(1 for Ace to 13 for King):";
  167. cin >> guess;
  168. // Is card in the bag?
  169. if (grabBag.contains(clubs[guess]))
  170. {
  171. // Good guess – remove card from the bag.
  172. cout << "You get the card!\n";
  173. grabBag.remove(clubs[guess]);
  174. }
  175. else
  176. {
  177. cout << "Sorry, card was not in the bag.\n";
  178. } // end if
  179. } // end while
  180. cout << "No more cards in the bag. Game over!\n";
  181. return 0;
  182. }; // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement