Question: Set Class Use the files: ArrayBag.h and ArrayBag.cpp to implement the functionality of a set for integers as described in Chapter 1, Exercises 6 and

Set Class

Use the files: ArrayBag.hSet Class Use the files: ArrayBag.h and ArrayBag.cpp to implement the functionality and ArrayBag.cpp to implement the functionality of a set for integers as described in Chapter 1, Exercises 6 and 8 at page 28. When it comes to duplicates follow these instructions, not those in the book. ArrayBag, makes an array ADT with many of the functions of a STL vector. Please use this NOT a vector. To verify your set class, write a main function that takes two sets of integers from an input file setInput.txt, and store them into separate sets, and apply following two operations.

First, combine the two into one set by overloading the + (union) operator, and print out the union set. The + operator must remove all duplicates from the result and store one copy of any item. You also need to implement a subtraction operator (difference) where (A-B means removing all elements that are in B from A) an. If the item is in B but not A, it is not placed in the result) Then remove the duplicates from the result. So the main function takes two sets of integers and print the result of + and - operation.

You can either add the + and - operators to the ArrayBag (suggested for most students), or you can add a Set class as a friend to ArrayBag (friend class Set; Then define a class Set in a .h and associated .cpp)

A sample setInput.txtof a set for integers as described in Chapter 1, Exercises 6 may look like this. Note that we assume the input file includes only two sets of integer numbers, each number is separated by white spaces and sets are separated by a newline character. 3 4 5 7 5 16 7 12 11 12 3 9 9 8 1 12 15 4 3 6 1 12 3 12 7 8 19 9 11 12 8 5 -4 -100

After validating, instead of putting them into a vector, you will add them to a "Set" (ArrayBag) to meet this homework's requirement.

You should submit a single lastnameHW2.zip containing: ArrayBag.h, ArrayBag.cpp, main.cpp, SetFunctions.h, SetFunctions.cpp, makefile, and Readme.txt and setInput.txt.

Extra Credit (up to 4 pts, must note in your readme.txt): Validate that all inputs are integers in a function inside SetFunctions.h and SetFunctions.cpp. Anything else should be rejected with an error message. See validateInput.cpp for an idea of how to validate input. Include a setInputA.txt demonstrating invalid input.

ArrayBag.h

/** ADT bag: Array-based implementation.

@file ArrayBag.h */

#ifndef BAG_

#define BAG_

#include

typedef int ItemType;

class ArrayBag

{

private:

static const int DEFAULT_BAG_SIZE = 100;

ItemType items[DEFAULT_BAG_SIZE]; // array of bag items

int itemCount; // current count of bag items

int maxItems; // max capacity of the bag

// Returns either the index of the element in the array items that

// contains the given target or -1, if the array does not contain

// the target.

int getIndexOf(const ItemType& target) const;

public:

ArrayBag();

int getCurrentSize() const;

bool isEmpty() const;

bool add(const ItemType& newEntry);

bool remove(const ItemType& anEntry);

void clear();

bool contains(const ItemType& anEntry) const;

int getFrequencyOf(const ItemType& anEntry) const;

}; // end Bag

#endif

ArrayBag.cpp

/** ADT bag: Array-based implementation.

@file ArrayBag.h */

#ifndef BAG_

#define BAG_

#include

typedef int ItemType;

class ArrayBag

{

private:

static const int DEFAULT_BAG_SIZE = 100;

ItemType items[DEFAULT_BAG_SIZE]; // array of bag items

int itemCount; // current count of bag items

int maxItems; // max capacity of the bag

// Returns either the index of the element in the array items that

// contains the given target or -1, if the array does not contain

// the target.

int getIndexOf(const ItemType& target) const;

public:

ArrayBag();

int getCurrentSize() const;

bool isEmpty() const;

bool add(const ItemType& newEntry);

bool remove(const ItemType& anEntry);

void clear();

bool contains(const ItemType& anEntry) const;

int getFrequencyOf(const ItemType& anEntry) const;

}; // end Bag

#endif

validateInput.cpp

#include //cout, cin

#include //stringstream

#include

#include //ifstream

#include

using namespace std;

/*Pre: Input a file stream and a vector of integers that were read in from

the file

Post: True if all input in vector are ints

Desc: validates that all items in vector are ints

*/

bool getIntsFromFile(ifstream &infile, vector &vec )

//istream accepts from cin or file as a parent class

{

stringstream ss;

int tempInt;

string readString;

getline(infile, readString);

ss

while (!ss.eof()) //Until end of stream

{

ss >> tempInt; //Read in an int from ss into tempInt

if (ss.fail()) //If it fails to to read an int data type

{

cout

ss.clear(); //Clears state of string stream;

return false;

}

vec.push_back(tempInt); //Add to the vector

}

return true;

}

/*Desc: Prints out all values in a vector

Pre: Reads in a vector

Post: void

*/

void printValues(vector set)

{

for (int i = 0; i

{

cout

}

cout

}

int main()

{

ifstream infile("setInput.txt");

vector vec1, vec2;

if ( getIntsFromFile(infile, vec1) )

printValues(vec1);

cout

if ( getIntsFromFile(infile, vec2) )

printValues(vec2);

return 0;

}

Chapter 1, Exercise 6

The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method.

Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, c; and bag2 contains the strings b, b, d, and e. The expression bag1.union(bag2) returns a bag containing the strings a, b, b, b, c, d, and e. Note that union does not affect the contents of bag1 and bag2.

Chapter 1, Exercise 8

The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that occur in a second. Design and specify a method difference for the ADT bag that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method.

Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.difference(bag2) returns a bag containing only the strings a and c. Note that difference does not affect the contents of bag1 and bag2.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!