C++ code Set Class First, combine the two into one set by using + (union) operat
ID: 3785411 • Letter: C
Question
C++ code
Set Class
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 < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
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 < itemCount)
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
searchIndex++;
} // end while
return frequency;
} // end getFrequencyOf
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 < itemCount; i++)
ArrayBagContents.push_back(items[i]);
return ArrayBagContents;
} // end toVector
// 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 < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
// 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 method’s 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 method’s 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.
// 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
/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 << readString; //Write readString into sswhile (!ss.eof()) //Until end of stream{
ss >> tempInt; //Read in an int from ss into tempIntif (ss.fail()) //If it fails to to read an int data type{cout << "Input contains non-integer data ";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 < set.size(); i++)
{
cout << set[i] << " ";
} cout << endl;}
int main(){
ifstream infile("setInput.txt");vector vec1, vec2;
if ( getIntsFromFile(infile, vec1) )
printValues(vec1);
cout << endl;
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
Explanation / Answer
#include<iostream.h>
#include<conio.h>
class Set
{
int a[15],n;
public:
Set(){}
Set(int n)
{
this->n=n;
}
void get();
void sort();
void show();
void operator +(Set);
void operator -(Set);
void operator <(Set); //superset
void operator >(Set); //subset
};
void Set :: operator <(Set s2)
{
Set s3;
}
void Set::get()
{
cout<<" Enter the Set Values : ";
for(int i=0;i<n;i++)
cin>>a[i];
sort();
}
void Set ::sort()
{
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
void Set::show()
{
for(int i=0;i<n;i++,cout<<" ")
cout<<a[i];
}
void Set::operator +(Set s2)
{
// int unions[30];
Set s3;
int i=0,j=0,k;
for(k=0;i<n && j<s2.n;k++)
{
if(a[i]==s2.a[j])
{
s3.a[k]=a[i++];
j++;
}
else if(a[i]<s2.a[j])
s3.a[k]=a[i++];
else
s3.a[k]=a[j++];
}
while(i<n)
s3.a[k++]=a[i++];
while(j<s2.n)
s3.a[k++]=s2.a[j++];
cout<<" A U B ";
s3.n=k;
s3.show();
}
void Set :: operator -(Set s2)
{
Set s3;
int i=0,j=0,k;
for(k=0;i<n && j<s2.n;)
{
if(a[i]==s2.a[j])
{
s3.a[k++]=a[i];
j++;
i++;
}
if(a[i]<s2.a[j])
j++;
else
i++;
}
cout<<" A INTERSECTION B ";
s3.n=k;
s3.show();
}
void main()
{
clrscr();
int n,n1;
cout<<" SET OPERATIONS ";
cout<<" Enter No.of elements in Set A & B : ";
cin>>n>>n1;
Set s1(n),s2(n1),s3;
s1.get();
s2.get();
clrscr();
cout<<" Set A ";
s1.show();
cout<<" Set B ";
s2.show();
s1+s2; //union
s1-s2;//intersection
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.