1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <algorithm>
#include <iostream>
#include <string>
#include "VectorBag.h"
using namespace std;
template<class ItemType> void displayBag(VectorBag<ItemType>& bag);
template<class ItemType> void bagTester(VectorBag<ItemType>& bag);
// void newOpsTester();
/**
* The usual <code>main()</code> function.
*/
int main()
{
VectorBag<string> bag;
cout << "Testing the Vector-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
// newOpsTester();
cout << "All done!" << endl;
return 0;
}
/**
* Display the contents of a <code>VectorBag</code>, which can hold
* any kind of element. The elements are displayed in sorted order.
* (This means that the <code>ItemType</code> must be one for which
* the "<" operation is defined.)
* @param bag The bag to be displayed.
*/
template<class ItemType>
void displayBag(VectorBag<ItemType>& bag)
{
cout << "The bag contains " << bag.getCurrentSize()
<< " items:" << endl;
vector<ItemType> bagItems = bag.toVector();
int numberOfEntries = static_cast<int>(bagItems.size());
sort(bagItems.begin(), bagItems.end());
for (int i = 0; i < numberOfEntries; i++)
cout << bagItems[i] << " ";
cout << endl << endl;
} // end displayBag
/**
* This is an adaption of the <code>bagTester</code> function in
* Carrano-Henry <code>main.cpp</code>, which tests the
* <code>ArrayBag</class>. The changes are as follows:
* <ol>
* <li> Whereas the original version assumed that
* the <code>ArrayBag</code> contained <code>string</code>, our
* version allows the <code>VectorBag</code> to contain any kind of
* item.</li>
* <li> In addition to testing the original operations that are
* implemented from <code>BagInterface</code>, we test the operations
* of union, intersection, and difference.</li>
* </ol>
* @param bag The bag to be tested.
*/
template<class ItemType> void bagTester(VectorBag<ItemType>& bag)
{
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
int itemsSize = sizeof(items)/sizeof(char*);
cout << "Adding " << itemsSize << " items to the bag: " << endl;
for (unsigned int i = 0; i < itemsSize; i++)
bag.add(items[i]);
displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize()
<< "; should be " << itemsSize << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bag.add("extra") << endl;
cout << "contains(\"three\"): returns " << bag.contains("three")
<< "; should be 1 (true)" << endl;
cout << "contains(\"ten\"): returns " << bag.contains("ten")
<< "; should be 0 (false)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 2" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 1" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 0 (false)" << endl;
cout << endl;
displayBag(bag);
cout << "After clearing the bag, ";
bag.clear();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
} // end bagTester
// void newOpsTester()
// {
// cout << "\nLet's test the new bag operations.\n\n";
// int firstItems[] = {2, 4, 6, 6, 8, 10, 12, 15, 15};
// VectorBag<int> firstBag;
// int firstSize = sizeof(firstItems)/sizeof(int);
// cout << "Adding " << firstSize << " items to first bag: " << endl;
// for (int i = 0; i < firstSize; i++)
// firstBag.add(firstItems[i]);
// cout << "First bag: ";
// displayBag(firstBag);
// int secondItems[] = {3, 6, 9, 12, 12, 15, 15, 18};
// VectorBag<int> secondBag;
// int secondSize = sizeof(secondItems)/sizeof(int);
// cout << "Adding " << secondSize << " items to second bag: " << endl;
// for (int i = 0; i < secondSize; i++)
// secondBag.add(secondItems[i]);
// cout << "Second bag: ";
// displayBag(secondBag);
// VectorBag<int> unionBag = firstBag + secondBag;
// cout << "firstBag union secondBag: \n";
// displayBag(unionBag);
// VectorBag<int> intersectBag = firstBag * secondBag;
// cout << "firstBag intersect secondBag: \n";
// displayBag(intersectBag);
// VectorBag<int> diffBag = firstBag - secondBag;
// cout << "firstBag minus secondBag: \n";
// displayBag(diffBag);
// diffBag = secondBag - firstBag;
// cout << "secondBag minus firstBag: \n";
// displayBag(diffBag);
// }
|