Question: C++ code Set Class Use the files: ArrayBag.h and ArrayBag.cpp to implement the Set class for integers as described in Chapter 1, Exercises 6 and
C++ code
Set Class
Use the files: ArrayBag.h
and ArrayBag.cpp
to implement the "Set" class for integers as described in Chapter 1, Exercises 6 and 8 at page 28. 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 using + (union) operator, and print out the union set. The + operator must remove all duplicates and store one copy of any item. You also need to implement a subtraction operator (difference) where (A-B means removing all elements of B from A) and print out the result. So the main function takes two sets of integers and print the result of + and - operation.
/** @file ArrayBag.cpp */
#include "ArrayBag.h"
ArrayBag::ArrayBag() : itemCount(0), maxItems(DEFAULT_ArrayBag_SIZE) { } // end default constructor
int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
bool ArrayBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount
bool ArrayBag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove
void ArrayBag::clear() { itemCount = 0; } // end clear
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int searchIndex = 0; while (searchIndex
bool ArrayBag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains
std::vector ArrayBag::toVector() const { std::vector ArrayBagContents; for (int i = 0; i
// private
int ArrayBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // if the ArrayBag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex
// 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; std::vector toVector() const; }; // end Bag
#endif
//Excercises 6 and 8 ////
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, and 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.First, combine the two into one set by using + (union) operator, and print out the union set. The + operator must remove all duplicates and store one copy of any item. You also need to implement a subtraction operator (difference) where (A-B means removing all elements of B from A) and print out the result. So the main function takes two sets of integers and print the result of + and - operation.
8. The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that also occur in the 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.
A sample setInput.txt
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.
// setinput.txt // 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
Validate that all inputs are integers. Anything else should be rejected with an error message. SeevalidateInput.cpp
for an idea of how to validate input. After validating, instead of putting them into a vector, you might add them to a "Set" to meet this homework's requirement.
/validateinput.cpp//
This program demonstrates how to validate if input is all integer values*/ #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 thefile 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
ss >> tempInt; //Read in an int from ss into tempIntif (ss.fail()) //If it fails to to read an int data type{cout
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
ifstream infile("setInput.txt");vector vec1, vec2; if ( getIntsFromFile(infile, vec1) )
printValues(vec1);
cout
if ( getIntsFromFile(infile, vec2) )
printValues(vec2);
return 0;}
Turn into a .zip containing: ArrayBag.h, ArrayBag.cpp, main.cpp, SetFunctions.h, SetFunctions.cpp, makefile, and Readme.txt.
Loose algorithm provided
ArrayBag bag1, bag2, resultbag;
getline(file, string1);
getline(file, string2;
stream string 1 into SS (of type StringStream)
Stream individuals out of SS
If not a number, exit the entire program.
If numberStreamed is an int (not fail)
bag1.add(numberStreamed)
Then the same thing for String2.
now just
resultbag = bag1 + bag2
Loop through each and print out resultbag
resultbag = bag1 -bag2
Loop though each and print out resultbag
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
