You are on page 1of 5

//Set.cpp //ComS 228 Spring 2005 //Homework 2 //Name: John Chargo //Section: B1 //Username: jchargo #include <iostream> #include"Set.

h" using namespace std;

Set::Set() // the default constructor which initializes an empty set. { theSet=NULL; size=0; return; } Set::Set(int n, int a[]) // accepts an integer array of size n. { theSet=new int[n]; size=n; for(int i=0; i<n; i++) { theSet[i]=a[i]; } return; } Set::Set(int n) // accepts n elements from the standard input by the command cin. { if(n==0){theSet=NULL; return;} theSet=new int[n]; size=n; int input; cout<< "Please input " << n << "elements now \n"; for(int i=0; i<n; i++) { cin >> input; theSet[i]=input; } return; } Set::Set(const Set& other) // the copy constructor. { size=other.size; theSet=new int[size]; for(int i=0; i< size; i++) theSet[i]=other.theSet[i]; return;

} Set::~Set() //Destructor { delete [] theSet; }

/*

void Set::reduce() //makes sure there are no duplicate items in array. { if(theSet==NULL) return; int temp; int newsize; for( int i=0; i<size; i++) { temp=theSet[i]; for(int j=i+1; i<size-1; j++) { } } } */

bool Set::inSet(int e) const // test whether an integer is in the set. { if(theSet==NULL) return false; for(int i=0; i< size; i++) { if(e==theSet[i]) return true; } return false; } void Set::setAdd(int e) // add an integer to the set. { int* temp; temp=new int[size]; for(int i=0; i<size; i++) temp[i]=theSet[i]; delete [] theSet; theSet=new int[size+1]; for(int j=0; j<size; j++) theSet[j]=temp[j]; theSet[size]=e; size++; delete [] temp; return; }

void Set::setDelete(int e) { int j=0; if(inSet(e)) {

// delete an integer from the set.

int* temp; temp=new int[size]; for(int i=0; i<size; i++) temp[i]=theSet[i]; delete [] theSet; theSet=new int[size-1]; for(int i=0; i<size; i++) { if(temp[i]!=e){theSet[j]=temp[i]; j++;} } } return; } int Set::getCardinality() const // query the set's cardinality. { return size; } Set Set::operator*(const Set& other) const //performs intersection of 2 sets; { Set temp; temp.size=other.size+size; for(int i=0; i<size; i++) { for(int j=0; j<other.size; j++) { if(theSet[i]==other.theSet[j]) temp.setAdd(theSet[i]); } } return temp; }

Set Set::operator+(const Set& other) const //performs union of 2 sets { Set temp; for(int i=0; i<size; i++) temp.setAdd(theSet[i]); for(int j=0; j<other.size; j++) temp.setAdd(other.theSet[j]); return temp; }

Set Set::operator-(const Set& other) const //performs differnce of 2 sets { Set temp; for(int i=0; i<size; i++) { if((other.inSet(theSet[i]))==false)

temp.setAdd(theSet[i]); } return temp; }

bool Set::operator<(const Set& other) const { bool subset=true;

//subset testing

for(int i=0; i<size; i++) { if((other.inSet(theSet[i]))==false) subset=false; } return subset; }

bool Set::operator==(const Set& other) const {

//equality testing

if(size!=other.size) return false; if((theSet==NULL)&&(other.theSet==NULL)) return true; for(int i=0; i<size; i++) { if((other.inSet(theSet[i]))==false) return false; }

return true; }

void Set::operator=(const Set& other) {

//assignment operator

delete [] theSet; theSet=new int[other.size]; size=other.size; for(int j=0; j<size; j++) theSet[j]=other.theSet[j]; return; }

ostream& operator << (ostream& outs, Set& other) //output operater { if(other.theSet==NULL){ outs << "(empty Set)"; return outs;} other.sort(); outs << "{ "; for(int i=0; i<other.size; i++)

{ outs << other.theSet[i]<<" "; } outs << "}"; return outs; }

void Set::sort() //sorts arrays from least to max { int min=10000; int counter=1; for(int i=0; i<size; i++) { if(theSet[i]<min) min=theSet[i]; } int* temp; temp=new int[size]; for(int i=0; i<size; i++) temp[i]=theSet[i]; delete [] theSet; theSet=new int[size]; theSet[0]=min; min++; while(counter<size) { for(int j=0; j<size; j++) { if(temp[j]==min){theSet[counter]=min; counter++;} } min++; } delete [] temp; return; }