Question: write: bag_with_receipts.cpp. your bag_with_receipts.cpp should work with: bag_with_receipts.h and hw1grade.cpp bag_with_receipts.h FILE: bag_with_receipts.h // adapted from https://www.cs.colorado.edu/~main/chapter3/bag1.h // // TYPEDEF and MEMBER CONSTANTS: // typedef
write: bag_with_receipts.cpp.
your bag_with_receipts.cpp should work with: bag_with_receipts.h and hw1grade.cpp
bag_with_receipts.h
FILE: bag_with_receipts.h
// adapted from https://www.cs.colorado.edu/~main/chapter3/bag1.h
//
// TYPEDEF and MEMBER CONSTANTS:
// typedef ____ value_type
// bag_with_receipts::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 default
// constructor, an assignment operator, and operators to
// test for equality (x == y) and non-equality (x != y).
//
// typedef ____ size_type
// bag_with_receipts::size_type is the data type of any variable that
keeps track of how many items
// are in a bag.
//
// static const size_type CAPACITY = _____
// bag_with_receipts::CAPACITY is the maximum number of items that a
bag can hold.
//
// CONSTRUCTOR for the bag class:
// bag_with_receipts( )
// Postcondition: The bag has been initialized as an empty bag.
//
// MODIFICATION MEMBER FUNCTIONS
//
// bool remove(int receipt, value_type& itemRemoved)
/* "when you want to remove an item,
you must provide a copy of the receipt as a parameter to the remove
function.
The remove function removes the item whose receipt has been
presented,
and also returns a copy of that item through a reference parameter."
*/
// Postcondition: If "the item whose receipt has been presented" was in
the bag,
// then the item has been removed and a copy of that item is returned
through
// the reference parameter itemRemoved; otherwise the bag is unchanged.
// A false return value indicates that nothing was removed; true
indicates otherwise.
//
// void insert(const value_type& item)
/* "Each time an item is added to a bag with receipts,
the insert function returns a unique integer called the receipt."
"When a new item is added, we will find the first spot that is
currently unused
and store the new item there. The receipt for the item is the index
of the location
where the new item is stored." */
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of item has been added to the bag.
//
// CONSTANT MEMBER FUNCTIONS
// size_type size( ) const
// Postcondition: The return value is the total number of items in the
bag.
//
// VALUE SEMANTICS for the bag class:
// Assignments and the copy constructor may be used with bag objects.
#ifndef MAIN_SAVITCH_BAGWR_H
#define MAIN_SAVITCH_BAGWR_H
#include
#include
namespace main_savitch_3
{
class bag_with_receipts
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef char value_type;
static const std::size_t CAPACITY = 100;
// CONSTRUCTOR
bag_with_receipts();
// MODIFICATION MEMBER FUNCTIONS
bool remove(int receipt, value_type& itemRemoved);
std::size_t insert(const value_type& item);
// CONSTANT MEMBER FUNCTIONS
std::size_t size( ) const { return used; }
// overloaded << operator
friend std::ostream& operator<<(std::ostream& os, const
bag_with_receipts& b);
private:
value_type data[CAPACITY];
bool in_use[CAPACITY];
std::size_t used; // How much of array is
used
};
}
#endif
hw1grade.cpp
// hw1grade.cpp
#include
#include
#include "bag_with_receipts.h" // With value_type defined as a char
using namespace std;
using namespace main_savitch_3;
int main()
{
float total = 0;
bag_with_receipts b1, b2, b3;
bool test0 = true;
size_t i = 0;
for (char c = ' '; c <= '>'; ++c)
{
if (i != b1.insert(c))
test0 = false;
++i;
}
if (test0)
total += 0.5;
b2 = b1;
for (char c = ' '; c <= '>'; ++c)
{
if (i != b2.insert(c))
test0 = false;
++i;
}
if (test0)
total += 0.25;
if ((2 * b1.size()) != b2.size())
test0 = false;
if (test0)
total += 0.25;
b3 = b2;
size_t b1size = b1.size();
size_t b2size = b2.size();
bool test1 = true;
char item = ' ';
i = 0;
while (test1 && (i < b1size))
{
char item1 = 'a';
char item2 = 'b';
b1.remove(i, item1);
b2.remove(i, item2);
if ((item1 != item) || (item2 != item))
test1 = false;
++i;
++item;
}
if (test1)
total += 0.5;
item = ' ';
while (test1 && (i < b2size))
{
char item1 = 'a';
char item2 = 'b';
b1.remove(i, item1);
b2.remove(i, item2);
if (item2 != item)
test1 = false;
++i;
++item;
}
if (test1)
total += 0.25;
bool test2 = true;
if (b3.insert('1') != b2size)
test2 = false;
if (test2)
total += 0.25;
char item3 = ' ';
b3.remove(1, item3);
if (item3 != '!')
test2 = false;
b3.remove(17, item3);
if (item3 != '1')
test2 = false;
if (test2)
total += 0.25;
if (b3.insert('1') != 1)
test2 = false;
if (test2)
total += 0.25;
cout << "HW1: your score is " << total << endl;
return EXIT_SUCCESS;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
