A bag can contain more than one copy of an item. For example, the chapter descri
ID: 3859466 • Letter: A
Question
A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two cop- ies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You'l1 need to change the interface a bit. For example, in- stead of the bag's count function, you'll want a con- stant member function such as this bool set::contains (const value_type& target) const; // Postcondition: The return value is true if // target is in the set; otherwise the return // value is false Make an explicit statement of the invariant of the set class. Do a time analysis for each operation. At thisExplanation / Answer
//bag.h
#ifndef _BAG_
#define _BAG_
typedef int bag_type;
class Bag {
private:
int cnt;
bag_type data1[20]; // 20 memebers in bag type
typedef int Item;
public:
Bag(); // Constructor method
bool add(bag_type); // Put a member in the bag
bool remove(bag_type); // delete the bag
int size(); // size of bag
void clear(); // clear bag
bool bagprocess(bag_type); //member process
int many(bag_type); // how many member in bag.
bool contains(const Item & target)const;
};
#endif
//bag.cpp
#include "bag.h"
#include <iostream>
Bag::Bag() // Constructor
{
// set bag process
cnt=0;
}
void Bag::clear() // Clear
{
cnt=0;
}
bool Bag::add(bag_type val1) //add bag
{
bool re;
if(cnt <20 ) {
data1[cnt]=val1;
re=true;
cnt++;
} else {
re=false;
}
return re;
}
bool Bag::bagprocess(bag_type val1)
{
bool re=false;
int index1;
for(index1=0;index1<cnt&& !re;index1++)
if(data1[index1] == val1) re=true;
return re;
}
int Bag::many(bag_type val1)
{
int many1=0;
int in1;
for(in1=0;in1<cnt;in1++)
if(data1[in1]==val1) many1++;
return many1;
}
bool Bag::remove(bag_type val1)
{
bool re=false;
int index1;
if(many(val1) == 0) return re;
re=true;
index1=0;
while(data1[index1] != val1) index1++;
for(;index1<cnt;index1++)
data1[index1]=data1[index1+1];
cnt--;
}
bool Bag::contains(const Item & target1)const
{
for(int k = 0;k < 1 ;k++ )
{
if(data1[k]==target1)
return true;
}
return false;
}
int Bag::size()
{
return cnt;
}
//main.cpp
#include <iostream>
#include <cstdlib>
#include "bag.h"
using namespace std;
int main(int argc, char *argv[])
{
Bag k;
bag_type value;
cout << "Bag" << endl;
k.add(4);
do {
value=rand()%6 +1;
} while(k.add(value));
cout << k.size()<< " elements in the bag" << endl;
cout << k.many(4) << " fours " << endl;
k.remove(4);
cout << k.size()<< " elements in the bag" << endl;
cout << k.many(4) << " fours " << endl;
cout << k.many(5) << " fives " << endl;
while(k.bagprocess(5)) k.remove(5);
cout << k.many(5) << " fives " << endl;
// system("Pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.