C++ PROGRAM I HAVE INCLUDED ALL OF THE FILES BELOW. PLEASE MAKE SURE THAT PROGRA
ID: 3667046 • Letter: C
Question
C++ PROGRAM I HAVE INCLUDED ALL OF THE FILES BELOW. PLEASE MAKE SURE THAT PROGRAM RUNS CORRECTLY.
bag.h: http://pastebin.com/MCnFmZzF
bag.cpp: http://pastebin.com/r7skEXvn
bag_diff3.cpp: http://pastebin.com/MAJ5AZjK
1. A Different dynamic bag (50 points) For dynamic bag, implement the operators -- and - for the class These operators compute the difference between two bags. In summary bl-b2 results in a bag that has the elements of bl with the elements of b2 removed. For example, if bl has eight 4's in it and b2 has five 4's in it then bl-b2 has three 4's in it. o Ifb2 has more of an element than bl, it is not an error. The difference simply ends up with none of that element. · o For example, if bl has five 4's in it and b2 has eight 4's in it then bl-b2 has no 4's in it. Implement-= as a member function and-as a non-member function (similar to += and +). Here are the two headers from bag.h: o void operator -(const bag &subtrahend;); o bag operator- (const bag& bl, const bag & b2); o The program bag diff3.cpp provide a basic check of the two new operators.Explanation / Answer
Bag.cpp
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <utility>
#include "Bag.h"
using namespace std;
namespace xxx
{
Bag::size_type Bag::count(const value_type& target)
{
size_type counted = 0; // keep track of count
//
for(int i = 0; i < used; i++) // iterate through items in bag
{ //
if(data[i] == target) // if the current item matches target add 1
counted += 1; // to counted
} //
//
return counted; // return the counted variable
}
void Bag::setUsed(size_type used)
{
this->used = used;
}
void Bag::insert(const value_type& entry)
{
if(used != CAPACITY) // if bag is not full
{ //
data[used] = entry; // add an item at data[used] equal to entry
used++; // and add 1 to used
}
}
bool Bag::erase_one(const value_type target)
{
if(used != 0) // if bag is not empty
{ //
bool isRemoved = false; // set var isRemoved to false
int i = 0; // keep track of current location in array
while(!isRemoved && i < used) // as long as we are inside of array and
{ // is nothing is been removed
if(data[i] == target) // if data[i] equals target
{ //
data[i] = data[used-1]; // set data[i] to the last item in array
used--; // reduce used by one
isRemoved = true; // isRemoved is set to true
} //
i += 1; // add one to i
}
return isRemoved; // return isRemoved if bag not empy
} //
else //
return false; // if empty return false
}
void Bag::erase()
{ // to erase the items in the bag it is not
used = 0; // necessary to do more than just set used to zero
} // this way we just ignore the old contents
Bag::size_type Bag::erase_copies(const value_type& target)
{
size_type erased;
for(int i=0; i < used; i++)
{
if(data[i] == target)
{
data[i] = data[used - 1];
used--;
i--;
erased++;
}
}
return erased;
}
void Bag::operator += (const Bag& addend)
{
assert(this->used + addend.used < CAPACITY);
copy(addend.data, addend.data + addend.size(), data + used);
used += addend.size();
}
void Bag::operator -= (const Bag& subt)
{
bool gotIt;
for(int i=0; i < subt.size(); i++)
{
gotIt = erase_one(subt.data[i]);
}
}
Bag operator + (const Bag& b1, const Bag& b2)
{
if(b1.size() + b2.size() < Bag::CAPACITY)
{
Bag temp;
temp.setUsed(b1.size() + b2.size());
temp += b1;
temp += b2;
}
}
Bag& Bag::operator = (const Bag& equ)
{
Bag temp;
copy(equ.data, equ.data + equ.size(), temp.data);
temp.used = equ.used;
return temp;
}
Bag operator - (const Bag& b1, const Bag& b2)
{
Bag temp = b1;
if(b1.size() == 0 or b2.size() == 0)
return temp;
else
temp -= b2;
return temp;
}
ostream& operator << (ostream& out, const Bag& b)
{
for(int i = 0; i < b.size(); i++)
{
out << b.data[i] << " ";
}
out << endl;
return out;
}
}
Bag.h
#ifndef BAG_H
#define BAG_H
#include <fstream>
#include <cstdlib>
#include <utility>
namespace xxx
{
class Bag
{
public:
typedef int value_type; // data stored
typedef std::size_t size_type; // current number of items
static const size_type CAPACITY = 40; // capacity -> array
Bag(){used = 0;} // constructor
size_type count(const value_type& target);
void insert(const value_type& entry); // insert item x into data
bool erase_one(const value_type target);// remove target from data
void erase(); // erase all items in data
size_type erase_copies(const value_type& target); // erase target doubles
void setUsed(size_type used);
void operator += (const Bag& addend);
void operator -= (const Bag& subt);
Bag& operator = (const Bag& equ);
friend std::ostream& operator << (std::ostream& out,const Bag& b);
size_type size() const {return used;}
private:
value_type data[CAPACITY];
size_type used;
};
Bag operator + (const Bag& b1, const Bag& b2);
Bag operator - (const Bag& b1, const Bag& b2);
}
#endif // end definition
main.cpp
#include <iostream>
#include <cstdlib>
#include "Bag.h"
using namespace std;
void menuMessage();
void bagMenu();
int main()
{
xxx::Bag b1, b2, b3;
b2.insert(1);
b2.insert(2);
b2.insert(3);
b2.insert(4);
int res;
cout << "********** Bag Menu -Test File- ********** ";
char ans = 'a';
while(ans != 'q')
{
menuMessage();
cin >> ans;
switch (ans)
{
case '1':
// select bag to change and value to add
cout << "Enter value to add: ";
cin >> res;
b1.insert(res);
cout << endl;
break;
case '2':
// Erase one value from a bag
cout << "Enter value to remove: ";
cin >> res;
b1.erase_one(res);
cout << endl;
break;
case '3':
// Erase all copies
int result;
cout << "Enter value to recusively remove: ";
cin >> res;
result = b1.erase_copies(res);
cout << "Amount erased: " << result << endl;
break;
case '4':
// erase all
b1.erase();
cout << "The contents have been erased" << endl;
break;
case '5':
// Add two +
cout << "Adding bag1 and bag2... ";
b3 = b1 + b2;
cout << endl;
break;
case '6':
// add two with +=
cout << "Adding bag2 to bag1 ";
b1 += b2;
cout << endl;
break;
case '7':
// display a bags contents
cout << b1 << endl;
break;
case '8':
// subtract items
//b1 = b1 - b2;
cout << "b2 subtracted b2 from b1... " << endl;
break;
case '9':
// subract again
b1-= b2;
cout << "b2 subtracted from b1... " << endl;
break;
default:
cout << "Please choose one of the following: ";
}
}
return EXIT_SUCCESS;
}
void menuMessage()
{
cout << " >>> Options: ";
cout << "1. To add a new item to a bag "
<< "2. Erase one value "
<< "3. Erase all copies of one value "
<< "4. Erase all "
<< "5. Add two bags using + "
<< "6. Add two bags using += "
<< "7. Display a bag's contents "
<< "8. Subtract b2 from b1 using - "
<< "9. Subtract b2 from b1 using -= "
<< "q Quit test program "
<< ">>> ";
}
Sample Output
********** Bag Menu -Test File- **********
>>> Options:
1. To add a new item to a bag
2. Erase one value
3. Erase all copies of one value
4. Erase all
5. Add two bags using +
6. Add two bags using +=
7. Display a bag's contents
8. Subtract b2 from b1 using -
9. Subtract b2 from b1 using -=
q Quit test program
>>> 1
Enter value to add: 2 3 4
>>> Options:
1. To add a new item to a bag
2. Erase one value
3. Erase all copies of one value
4. Erase all
5. Add two bags using +
6. Add two bags using +=
7. Display a bag's contents
8. Subtract b2 from b1 using -
9. Subtract b2 from b1 using -=
q Quit test program
>>> Enter value to recusively remove: Amount erased: 0
>>> Options:
1. To add a new item to a bag
2. Erase one value
3. Erase all copies of one value
4. Erase all
5. Add two bags using +
6. Add two bags using +=
7. Display a bag's contents
8. Subtract b2 from b1 using -
9. Subtract b2 from b1 using -=
q Quit test program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.