ArrayBag<ItemType> ArrayBag<ItemType>::intersection(const ArrayBag<ItemType> bag2) { ArrayBag<ItemType> interBag; for (int i=0; i<getCurrentSize(); i++) { if (bag2.contains(items[i])) { interBag.add(items[i]); } } return interBag; }
ArrayBag<ItemType> ArrayBag<ItemType>::Union(const ArrayBag<ItemType> secondBag) { ArrayBag<ItemType> unionBag; getCurrentSize(); int bigSize = itemCount + secondBag.getCurrentSize(); //Begin C++ Interlude 3 HW if (bigSize > unionBag.DEFAULT_CAPACITY) throw std::out_of_range ("Union of these Bags exceeds Default Capacity"); //End C++ Interlude 3 HW for (int i=0; i<getCurrentSize(); i++) unionBag.add(items[i]); for (int i=0; i<secondBag.getCurrentSize(); i++) unionBag.add(secondBag.items[i]); return unionBag; }
ArrayBag<ItemType> ArrayBag<ItemType>::difference(const ArrayBag<ItemType> bag2) { for (int i=0; i<getCurrentSize(); i++) { if (bag2.contains(items[i])) { remove(items[i]); } } }
ArrayBag<ItemType> client(ArrayBag<ItemType> a, ArrayBag<ItemType> b) { ArrayBag<ItemType> c; vector<ItemType> v1 = a.toVector(); vector<ItemType> v2 = b.toVector(); int i = 0; while (i < a.getCurrentSize()) c.add(v1[i]); i = 0; while (i < b.getCurrentSize()) c.add(v2[i]); return c; } //end client
void bagTester(ArrayBag<string>& bag) { cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl; cout << bag; ifstream myfile ("Words.txt"); int currSize = 10; int count = 0; if (myfile.is_open()) { string line; while ( getline (myfile,line) ) { if (count < currSize) { cout << line << endl; bag.add(line); count++; } //else if (count == currSize) { // ArrayBag<string> temp(bag); // count++; //temp = move(bag); //} } } string numbers[] = { "one", "two", "three", "four", "five", "one", "six", "seven", "eight" }; cout << "Add items to the bag container: " << endl; for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++) { bag.add( numbers[i] ); } // end for cout << bag; cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 0 (false)" << endl; cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 9" << endl; cout << "Try to add another entry: add(\"nine\") returns " << bag.add("nine") << endl; cout << "contains(\"three\"): returns " << boolalpha << bag.contains("three") << "; should be 1 (true)" << endl; cout << "contains(\"ten\"): returns " << boolalpha << bag.contains("ten") << "; should be 0 (false)" << endl; cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 2" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl; cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 1" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 0 (false)" << endl; cout << endl; cout << bag; cout << "After clearing the bag, "; bag.clear(); cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl; } // end bagTester