Set Class First, combine the two into one set by overloading the + (union) opera
ID: 3744613 • Letter: S
Question
Set Class
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)
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.
Arraybag.h
ArrayBag.cpp
The assginment description is at the top. I need help with writing the setFunctions.h and .cpp and main.cpp. can you do this without using vectors?
thank you.
Explanation / Answer
The setFunctions.h and .cpp and main.cpp of the above is
x Question
main.cpp
#include <iostream>
#include "ArrayBag.h"
#include "SetFunctions.h"
int main() {
Set sett ,
set2;
std::ifstream
fin("setInput.txt");
//Bring the first two lines of the filE the sets
set1.fileln(fin);
set2.fileln(fin);
fin.close();
//print it all out
std::cout « "Set 1:" « std::endl;
std::cout « setl « std::endl «
std::endl;
std::cout « "Set 2:" «
std::endl;
std::cout « set2 « std::endl «
std::endl;
std::cout « "Set 1 - Set 2:" «
std::endl;
std::cout « set1 - set2 « std::end
std::endl;
std::cout « "Set 2 - Set 1:"
« std::endl;
std::cout « set2 - set1 « std::endl
std::endl;
std::cout « "Set 1 + Set 2:"
« std::endl;
std::cout « set1 + set2 « std::endl
« std::endl;
}
ArrayBag.cpp
/** @file ArrayBag.cpp */
#include "ArrayBag.h"
ArrayBag::ArrayBag() : itennCount(0),
maxltems(DEFAULT_BAG_SIZE) {
} // end default constructor
int ArrayBag::getCurrentSize() cons
{
return itemCount;
} // end getCurrentSize
int ArrayBag::getMaxSize() const
{
return maxitems;
}
bool ArrayBag::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
bool ArrayBag::add(const ItemType newEntry)
{
bool hasRoomToAdd = (itemCoui maxitems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndex0f(anEntry);
bool canRemoveltem = !isEmpty() && (locatedIndex > -1);
if (canRemoveltem)
{
itemCount--;
items[Iocatedlndex] = items[itemCount];
} // end if
return canRemoveltem;
} // end remove
const int ArrayBag::operatorp(cons. index) const
{
return items[index];
}
int ArrayBag::operatorfl(const int index)
{
return items[index];
}
void ArrayBag::clear()
{
itemCount = 0;
} // end clear
int ArrayBag::getFrequency0f(cons. ItemType& anEntry) const
{
int frequency = 0;
int searchlndex = 0;
while (searchlndex < itemCount) {
if (items[searchlndex] == anEntr {
frequency++;
} // end if
searchlndex++;
} // end while
return frequency;
} // end getFrequency0f
bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndex0f(anEntry) > -1;
} // end contains
std::vector<ltemType> ArrayBag::toVector() const
{
std::vector<ltemType> ArrayBagContents;
for (int i = 0; i < itemCount; i++)
ArrayBagContents.push_back(items[i]);
return ArrayBagContents;
} // end toVector
// private
int ArrayBag::getlndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchlndex = 0;
// if the ArrayBag is empty, itemCc is zero, so loop is skipped
while (!found && (searchlndex < itemCount))
{
if (items[searchIndex] == target) {
found = true;
result = searchlndex;
}
else {
searchlndex++;
} // end if
} // end while
return result;
} // end getlndexOf
ArrayBag.h
/**ADT bag: Array-based implementation. @file ArrayBag.h */
#ifndef BAG_
#define BAG_
#include <vector>
typedef int ItemType; class ArrayBag
{
private:
static const int DEFAULT_BAG_SI = 100;
ItemType items[DEFAULT_BAG_SIZE]; // array bag
items int itemCount; // currer count of bag items
int maxltems; // max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, i the array does not contain
// the target.
int getlndexOf(const ItemType& target) const;
public:
ArrayBag();
int getMaxSize() const;
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
const int operator[](const int inde) const;
int operator[](const int index);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequency0f(const ItemTy anEntry) const;
std::vector<ltemType> toVector() const;
}; // end Bag
#endif
SetFunctions.cpp
#include <fstream> //file input
#include <sstream> //stringstream
#include <string> //read from file tc string
#include "ArrayBag.h"
#include "SetFunctions.h"
Set::Set()
{
_set = ArrayBag();
}
boot Set::add(int newValue)
{
if(! set.contains(newValue)) return set.add(newValue);
//this is to keep consistent with th return value of the ArrayBag class
if( _set.getCurrentSize() < _set.getMaxSize() )
return true;
else
return false;
}
bool Set::remove(int rmValue)
{
return _set.remove(rmValue);
}
bool Set::fileln(std::ifstream& fin) {
std::stringstream ss;
int buffer;
std::string reader;
getline(fin, reader);
ss « reader;
while(!ss.eof())
{
ss » buffer;
//if we run into an error that's not end of the line, quit
if(!ss.eof() && ss.fail())
{
std::cout « "Input contains non. integer data!" « std::endl;
ss.clear();
return false;
} //otherwise, add the newly extracted
int to the Set
add(buffer);
}
return true;
Set operator+(const Set lhs, const rhs)
{
Set sum;
//add all elements from both sets. "add" for sets assures that
//only one copy of any given int wi be added
for(int i = 0; i < Ihs._set.getCurrentSize(); i++) sum.add(Ihs._set[i]);
for(int i = 0; i < rhs._set.getCurrentSize(); i++) sum. add(rhs._set[i]);
return sum;
}
Set operator-(const Set lhs, const Set
rhs)
{
Set diff;
//add all the elements of the first
for(int i = 0; i < Ihs._set.getCurrentSize(); i++) diff.add(Ihs._set[i]);
//then remove all of the elements from the second
for(int i = 0; i < rhs._set.getCurrentSize(); i++) diffsemove(rhs._set[i]); return cliff;
}
std::ostream& operator«(std::ostrE &os, const Set output)
{
for(int i = 0; i < output_set.getCurrentSize(); i++)
os « output._set[i] « ' ';
return os;
}
SetFunctions.h
#include<iostream>
#include<fstream>
#include"ArrayBag.h"
class Set
{
private:
ArrayBag _set;
public:
Set();
//add new member to the set
bool add(const int newValue);
//remove member from the set
bool remove(const int rmValue);
//read sets in from a file bool fileln(std::ifstream& fin);
//Union operator for sets friend Set operator+(const Set lhs, const Set rhs);
Default Term
sh-4.25 g++ -std=c++11 -0 main*.cpp
sh-4.25 g++ -std=c++11 -0 main*.cpp
sh-4.25 main
set 1: 3 4 5 7 16 12 11 9 6 1
Set 2:15 4 3 6 1 12 7 8 19 9
1 5 -4 -100
set1 - set2:
16
set2 - set1:
15 -4 -100 6 19
set1 + set2:
3 4 5 7 16 12 11 9 8 1
5 6 19 -4 -100
//Asymmetrical difference of sets friend Set operator-(const Set lhs, const Set rhs);
//Display the set on the console friend std::ostream& operator«(std::ostream &os, const Set output); };
Set operator+(const Set Ihs, const rhs); Set operator-(const Set lhs, const S rhs);
std::ostream& operator«(std::ostr€ &os, const Set output);
setlnput.txt
3 4 5 7 5 16 7 12 11 12 3 9 9 8 12 15 4 3 6 1 12 3 127 8 19 9 11 1:
5 -4 -100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.