Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. // Ibrahim Ansari
  2.  
  3. #include <array>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <memory>
  8.  
  9. using namespace std;
  10.  
  11. enum { INVALID=-1,DEFAULT_CAPACITY = 10 }; // Small size to test for a full bag
  12.  
  13. template<typename T>
  14. class BagInterface
  15. {
  16. public:
  17. /** Gets the current number of entries in this bag.
  18. @return The integer number of entries currently in the bag. */
  19. virtual int getCurrentSize() const = 0;
  20.  
  21. /** Sees whether this bag is empty.
  22. @return True if the bag is empty, or false if not. */
  23. virtual bool isEmpty() const = 0;
  24.  
  25. /** Adds a new entry to this bag.
  26. @post If successful, newEntry is stored in the bag and
  27. the count of items in the bag has increased by 1.
  28. @param newEntry The object to be added as a new entry.
  29. @return True if addition was successful, or false if not. */
  30. virtual bool add(const T& newEntry) = 0;
  31.  
  32. /** Removes one occurrence of a given entry from this bag,
  33. if possible.
  34. @post If successful, anEntry has been removed from the bag
  35. and the count of items in the bag has decreased by 1.
  36. @param anEntry The entry to be removed.
  37. @return True if removal was successful, or false if not. */
  38. virtual bool remove(const T& anEntry) = 0;
  39.  
  40. /** Removes all entries from this bag.
  41. @post Bag contains no items, and the count of items is 0. */
  42. virtual void clear() = 0;
  43.  
  44. /** Counts the number of times a given entry appears in bag.
  45. @param anEntry The entry to be counted.
  46. @return The number of times anEntry appears in the bag. */
  47. virtual int getFrequencyOf(const T& anEntry) const = 0;
  48.  
  49. /** Tests whether this bag contains a given entry.
  50. @param anEntry The entry to locate.
  51. @return True if bag contains anEntry, or false otherwise. */
  52. virtual bool contains(const T& anEntry) const = 0;
  53.  
  54. }; // end BagInterface
  55.  
  56. template<typename T>
  57. class ArrayBag : public BagInterface<T>
  58. {
  59. private:
  60. T* items; // Array of bag items
  61. int itemCount; // Current count of bag items
  62. int maxItems; // Max capacity of the bag
  63. public:
  64. ArrayBag(int capacity = DEFAULT_CAPACITY) : itemCount(0), maxItems(capacity)
  65. {
  66. items = new T[capacity];
  67. } // end default constructor
  68. ~ArrayBag()
  69. {
  70. //if (items)
  71. // delete[] items;
  72. }
  73. int getCurrentSize() const
  74. {
  75. return itemCount;
  76. } // end getCurrentSize
  77. bool isEmpty() const
  78. {
  79. return itemCount == 0;
  80. } // end isEmpty
  81. T& get(int index)
  82. {
  83. return items[index];
  84. }
  85. bool add(const T& newEntry)
  86. {
  87. bool hasRoomToAdd = (itemCount < maxItems);
  88. if (hasRoomToAdd)
  89. {
  90. items[itemCount] = newEntry;
  91. itemCount++;
  92. } // end if
  93. return hasRoomToAdd;
  94. } // end add
  95. bool remove(const T& anEntry)
  96. {
  97. int locatedIndex = getIndexOf(anEntry, 0);
  98. bool canRemoveItem = !isEmpty() && (locatedIndex > INVALID);
  99. if (canRemoveItem)
  100. {
  101. itemCount--;
  102. items[locatedIndex] = items[itemCount];
  103. } // end if
  104.  
  105. return canRemoveItem;
  106. } // end remove
  107. void clear()
  108. {
  109. itemCount = 0;
  110. } // end clear
  111. bool contains(const T& anEntry) const
  112. {
  113. return getIndexOf(anEntry, 0) > INVALID;
  114. } // end contains
  115. int getFrequencyOf(const T& anEntry) const
  116. {
  117. return countFrequency(anEntry, 0);
  118. } // end getFrequencyOf
  119. int countFrequency(const T& anEntry, int searchIndex) const
  120. {
  121. int frequency = 0;
  122. if (searchIndex < itemCount)
  123. {
  124. if (items[searchIndex] == anEntry)
  125. {
  126. frequency = 1 + countFrequency(anEntry, searchIndex + 1);
  127. }
  128. else
  129. {
  130. frequency = countFrequency(anEntry, searchIndex + 1);
  131. } // end if
  132. } // end if
  133.  
  134. return frequency;
  135. } // end countFrequency
  136. int getIndexOf(const T& target, int searchIndex) const
  137. {
  138. int result = -1;
  139. if (searchIndex < itemCount)
  140. {
  141. if (items[searchIndex] == target)
  142. {
  143. result = searchIndex;
  144. }
  145. else
  146. {
  147. result = getIndexOf(target, searchIndex + 1);
  148. } // end if
  149. } // end if
  150.  
  151. return result;
  152. } // end getIndexOf
  153. T& getIndexValue(int index)
  154. {
  155. return items[index];
  156. }
  157. friend ostream& operator << (ostream& stream, ArrayBag<T>& bag)
  158. {
  159. stream << "The bag contains " << static_cast<int>(bag.getCurrentSize()) << " items:" << endl;
  160. for (int i = 0; i < bag.getCurrentSize(); i++)
  161. {
  162. stream << bag.getIndexValue(i) << " ";
  163. } // end for
  164. stream << endl << endl;
  165. return stream;
  166. } // end displayBag
  167. };
  168.  
  169. int ReadFile(string filename,string* slist) {
  170. ifstream file(filename);
  171. string buffer;
  172. int index = 0;
  173. if (file.is_open())
  174. {
  175. while (!file.eof())
  176. {
  177. getline(file, buffer);
  178. if (buffer.length() > 0)
  179. {
  180. slist[index] = buffer; // array notation
  181. buffer = "";
  182. index++;
  183. }
  184. }
  185. }
  186. else
  187. {
  188. cerr << "Unable to open file: " << filename << endl;
  189. return -1;
  190. }
  191. file.close();
  192. return index;
  193. }
  194.  
  195. void Output(string* slist,int number) // or slist[]
  196. {
  197. int index = 0;
  198. for (index = 0; index < number; index++)
  199. cout << *(slist + index) << endl; // pointer notation
  200. }
  201.  
  202. void bagTester(ArrayBag<string>& bag)
  203. {
  204. cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl;
  205. cout << bag;
  206.  
  207. string numbers[] =
  208. {
  209. "one",
  210. "two",
  211. "three",
  212. "four",
  213. "five",
  214. "one",
  215. "six",
  216. "seven",
  217. "eight"
  218. };
  219. cout << "Add items to the bag container: " << endl;
  220. for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++)
  221. {
  222. bag.add( numbers[i] );
  223. } // end for
  224.  
  225. cout << bag;
  226.  
  227. cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 0 (false)" << endl;
  228. cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 9" << endl;
  229. cout << "Try to add another entry: add(\"nine\") returns " << bag.add("nine") << endl;
  230. cout << "contains(\"three\"): returns " << boolalpha << bag.contains("three") << "; should be 1 (true)" << endl;
  231. cout << "contains(\"ten\"): returns " << boolalpha << bag.contains("ten") << "; should be 0 (false)" << endl;
  232. cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 2" << endl;
  233. cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl;
  234. cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 1" << endl;
  235. cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl;
  236. cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 0 (false)" << endl;
  237. cout << endl;
  238.  
  239. cout << bag;
  240.  
  241. cout << "After clearing the bag, ";
  242. bag.clear();
  243.  
  244. cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl;
  245. } // end bagTester
  246.  
  247. //void enableDebug(bool bvalue)
  248. //{
  249. // if (!bvalue) return;
  250. //
  251. // int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  252. //
  253. // // Turn on leak-checking bit.
  254. // tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
  255. //
  256. // // Turn off CRT block checking bit.
  257. // tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
  258. //
  259. // // Set flag to the new value.
  260. // _CrtSetDbgFlag(tmpFlag);
  261. //}
  262.  
  263. int main()
  264. {
  265. // enableDebug(true);
  266. ArrayBag<string> bag;
  267. cout << "Testing the Array-Based Bag:" << endl;
  268. cout << "The initial bag is empty." << endl;
  269. bagTester(bag);
  270. cout << "All done!" << endl;
  271. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement