This program generates the intersection, union, relative composition, cross product, and power set, of given input (which generates the sets the operations are performed on.
/////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> createSet(void); // creates set from user input
std::vector<std::string> getIntersection(std::vector<std::string> set_A,
std::vector<std::string> set_B); // gets intersection of two sets
std::vector<std::string> getUnion(std::vector<std::string> set_A,
std::vector<std::string> set_B); // gets union of two sets
std::vector<std::string> getRelativeComp(std::vector<std::string> set_A,
std::vector<std::string> set_B); // generates relative comp of sets
std::vector<std::string> getCrossProduct(std::vector<std::string> set_A,
std::vector<std::string> set_B); // gets cross product of sets
void powerSet(std::vector<std::string> passedSet); // generates and prints
void printSet(std::vector<std::string> passedSet); // prints a set
////////////////////////////////////////////////////////////////////////////////
int main(void)
{
std::vector<std::string> set_A = createSet();
std::vector<std::string> set_B = createSet();
std::vector<std::string> setBuff;
std::vector<std::vector<std::string>> powerBuff;
setBuff = getIntersection(set_A, set_B);
std::cout << "\nPrinting intersection now: ";
printSet(setBuff);
setBuff = getUnion(set_A, set_B);
std::cout << "\nPrinting union now: ";
printSet(setBuff);
setBuff = getRelativeComp(set_A, set_B);
std::cout << "\nPrinting relative complement A and B: ";
printSet(setBuff);
setBuff = getRelativeComp(set_B, set_A);
std::cout << "\nPrinting relative complement of B and A: ";
printSet(setBuff);
setBuff = getCrossProduct(set_A, set_B);
std::cout << "\nPrinting cross product of A and B: ";
printSet(setBuff);
powerSet(setBuff);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> createSet(void)
{
std::string eleBuff;
std::cout << "\nPlease enter elements for the set, enter quit "
<< " when finished: ";
std::getline(std::cin, eleBuff);
// Create stringstream to split by whitespace
std::stringstream ss(eleBuff);
std::istream_iterator begin(ss);
std::istream_iterator end;
std::vector setBuff(begin, end);
std::copy(setBuff.begin(), setBuff.end(),
std::ostream_iterator(std::cout, "\n"));
std::sort(setBuff.begin(), setBuff.end());
// Erase duplicates
setBuff.erase(unique(setBuff.begin(), setBuff.end()), setBuff.end());
return setBuff;
}
////////////////////////////////////////////////////////////////////////////////
void printSet(std::vector<std::string> setBuff)
{
std::cout << "{ ";
for(auto it = setBuff.begin(); it != setBuff.end(); it++)
{
std::cout << (*(it));
if((it+1) != setBuff.end())
{
std::cout << ", "; // comma delimiting
}
}
std::cout << " }\nCardinality: " << setBuff.size(); // cardinality is size
return;
}
///////////////////////////////////////////////////////////////////
std::vector getIntersection(std::vector<std::string> set_A,
std::vector<std::string> set_B)
{
for(auto it = set_A.begin(); it != set_A.end();)
{
auto foundIt = find(set_B.begin(), set_B.end(), (*(it)));
// if the found iterator is at the end, we know it's not found
if(foundIt == set_B.end())
{
set_A.erase(it); // it advances by erasing
}
else // advance forward
{
it++;
}
}
return set_A;
}
///////////////////////////////////////////////////////////////////
std::vector getUnion(std::vector<std::string> set_A,
std::vector<std::string> set_B)
{
std::vector<std::string> setBuff = set_A; // set equal to one of the sets
for(auto it = set_B.begin(); it != set_B.end(); it++) // iterate through
{
// search other set for element
auto foundIt = find(set_A.begin(), set_A.end(), (*(it)));
if(foundIt == set_A.end()) // if it is not found
{
setBuff.push_back((*(it))); // push it back
}
}
std::sort(setBuff.begin(), setBuff.end()); // sort
return setBuff;
}
///////////////////////////////////////////////////////////////////
std::vector<std::string> getRelativeComp(std::vector<std::string> set_A,
std::vector<std::string> set_B)
{
for(auto it : set_B)
{
auto foundIt = find(set_A.begin(), set_A.end(), it);
if(foundIt != set_A.end()) // If the element is found
{
set_A.erase(foundIt); // erase to create complement
}
}
return set_A;
}
///////////////////////////////////////////////////////////////////
std::vector getCrossProduct(std::vector<std::string> set_A,
std::vector<std::string> set_B)
{
std::vector<std::string> setBuff;
std::string elementBuff;
for(auto aIt : set_A)
{
for(auto bIt : set_B)
{
setBuff.push_back("{ " + aIt + ", " + bIt + " }");
}
}
return setBuff;
}
///////////////////////////////////////////////////////////////////
void powerSet(std::vector<std::string> passedSet)
{
// Prints the roster of the powerset
std::vector<std::vector> powerSet;
std::vector<std::string> setBuff;
unsigned long long size = 0;
for(auto eleIt : passedSet)
{
if(powerSet.size() == 0) // push back the first element
{
setBuff.push_back(eleIt);
powerSet.push_back(setBuff);
}
else
{
size = powerSet.size();
for(unsigned long long i = 0; i < size; i++) // iterate through
setBuff = powerSet[i];
setBuff.push_back(eleIt); // addend
std::sort(setBuff.begin(), setBuff.end()); // sort by value
powerSet.push_back(setBuff);
}
setBuff.clear();
setBuff.push_back(eleIt); // push back the original element
powerSet.push_back(setBuff);
}
}
setBuff.clear();
setBuff.push_back("{ }"); // push back the null set
powerSet.push_back(setBuff);
std::cout << std::endl;
std::sort(powerSet.begin(), powerSet.end());
for(auto firstIt : powerSet) // This one is kind of long, so new line
{
for(auto secondIt : firstIt)
{
std::cout << secondIt << " ";
}
std::cout << "\n";
}
// cardinality is just the 2^size
std::cout << "\nPower set cardinality: " << pow(2, passedSet.size()) <<
"\n";
}
///////////////////////////////////////////////////////////////////