Write a C++ code to implement the Bag class to store strings. The implementation
ID: 3883730 • Letter: W
Question
Write a C++ code to implement the Bag class to store strings. The implementation should include: a header file, an implementation file, an application file. A Bag class will include the following operations create an empty bag, insert a new string into a bag, remove one matching element and return true if successful, clear all matching elements and return the count of item removed, return the total number of elements in the bag, returns the number of copies of the given item present in the bag. Each line should include an explaination. In the application file will have the menu list and ask users to choose which operations that they want, also
Explanation / Answer
HEADER FILE
#ifndef _BAG_
#define _BAG_
class Bag {
private:
int count; // number of members in bag
string data[20]; // data store up to 20 string members
public:
Bag(); // empty Bag Constructor
bool insert(string); // Put a member in the bag
bool remove(string); // remove one matching member from the bag
int size(); // number of total members in bag
int clear(string); // remove all matching members from the bag
int sameinbag(string); // how many matching member in bag.
};
#endif
The Bag implementation Code
#include<iostream>
#include<string>
using namespace std;
Bag::Bag() // Constructor
{
// set the initial state of the bag
count=0;
}
int Bag::clear(string value) // clear/remove all matching members from the bag
{
int number_of_members_removed = 0;
// no matching member in the bag
if(sameinbag(value) == 0) return 0;
int index =0;
for(;index<count;index++){
if(data[index].compare(value)==0){
remove(value);
number_of_members_removed +=1;
}
}
return number_of_members_removed;
}
bool Bag::insert(string value) // Insert/Place a value in Bag
{
bool reply;
if(count <20 ) {
data[count]=value;
reply=true;
count++;
} else {
reply=false;
}
return reply;
}
int Bag::sameinbag(string value) // how many matching member in bag
{
int matching_members = 0;
int index;
for(index=0;index<count;index++)
if(data[index].compare(value)==0){
matching_members += 1;
}
return matching_members;
}
bool Bag::remove(string value) // remove one matching member from the bag
{
bool reply=false;
int index;
// no matching member in the bag
if(sameinbag(value) == 0) return reply;
reply=true;
index=0;
while(data[index].compare(value)!=0) index++;
for(;index<count;index++)
data[index]=data[index+1];
count--;
}
int Bag::size() // How many elements in bag?
{
return count;
}
Driver to test the Bag
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
// create an object of bag class
Bag b;
string value;
cout << "Enter your choice or press -1 to exit" << endl;
cout << "1. Total number of elements in the bag" <<endl;
cout << "2. Put a element in the bag" <<endl;
cout << "3. Remove one matching element from the bag" <<endl;
cout << "4. Remove all matching elements from the bag" <<endl;
cout << "5. How many matching elements in the bag" <<endl;
int option;
cin >> option;
while(option!= -1){
switch(option){
case 1 :
{
cout << b.size() << " elements in the bag." << endl;
break;
}
case 2 :
{
cout << "Enter string: " << endl;
string str;
cin >> str;
b.insert(str);
break;
}
case 3 :
{
cout << "Enter string to delete: " <<endl;
string str1;
cin >> str1;
cout << "Removed: " << b.remove(str1);
break;
}
case 4 :
{
cout << "Enter string to delete: " <<endl;
string str2;
cin >> str2;
cout <<"Number of elements removed: " << b.clear(str2);
break;
}
case 5 :
{
cout << "Enter string to count: " <<endl;
string str3;
cin >> str3;
cout <<"Number of matching elements : " << b.sameinbag(str3);
break;
}
}
int temp_option;
cin >> temp_option;
option = temp_option;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.