/FILE: bag3.h (part of the namespace main savitch 5) // CLASS PROVIDED: bag (a c
ID: 3859462 • Letter: #
Question
/FILE: bag3.h (part of the namespace main savitch 5) // CLASS PROVIDED: bag (a collection of items, where each item may appear multiple times) // TYPEDEFS for the bag class // typede // bag::value type is the data type of the items in the bag. It may be any of the C++ //built-in types (int, char, etc.), or a class with a defauit constructor, a copy constructor //an assignment operator, and a test for equality (xy) f value-type // typedefs // bag: size type is the data type of any variable that keeps track of how many items are //in a bag s12e-type _ // CONSTRUCTOR for the bag class // bag) //Postcondition: The bag is empty // MODIFICATION MEMBER FUNCTIONS for the bag class // size type erase(const value type⌖) //Postcondition: All copies of target have been removed from the bag. The return value //is the number of copies removed (which could be zero). // bool erase_one (const value type& target) //Postcondition: If target was in the bag, then one copy of target has been removed from //the bag, otherwise the bag is unchanged. A true return value indicates that one copy //was removed; false indicates that nothing was removed // void insert (const value type& entry) //Postcondition: A new copy of entry has been inserted into the bag. // void operator +=(const bag& addend) //Postcondition: Each item in addend has been added to the bag // CONSTANT MEMBER FUNCTIONS for the bag class: // size type size) const / Postcondition: The return value is the total number of items in the bag. // size type count(const value type& target) const //Postcondition: The return value is the number of times target is in the bag. // value type grab() const Precondition: size)0 //Postcondition: The return value is a randomly selected item from the bag. (continued)Explanation / Answer
------------------------------------------------------------------------------------------
// FILE: bag1.cpp
//
// CLASS IMPLEMENTED: bag (see bag1.h for documentation)
// INVARIANT for the bag class:
// 1. The number of items in the bag is in the member variable used;
// 2. For an empty bag, we do not care what is stored in any of data; for a
// non-empty bag the items in the bag are stored in data[0] through
// data[used-1], and we don't care what's in the rest of data.
#include // Provides copy function
#include // Provides assert function
#include "bag1.h"
using namespace std;
namespace main_savitch_3
{
// (Omitted for VC++ 6.0) const bag::size_type bag::CAPACITY;
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
--used;
data[index] = data[used];
++many_removed;
}
else
++index;
}
return many_removed;
}
bool bag::erase_one(const value_type& target)
{
size_type index; // The location of target in the data array
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as used-1. If target is not
// in the array, then index will be set equal to used.
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used)
return false; // target isnt in the bag, so no work to do.
// When execution reaches here, target is in the bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
--used;
data[index] = data[used];
return true;
}
void bag::insert(const value_type& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
void bag::operator +=(const bag& addend)
// Library facilities used: algorithm, cassert
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
bag::size_type bag::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
bag operator +(const bag& b1, const bag& b2)
// Library facilities used: cassert
{
bag answer;
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.