// File Name: IntegerSet.h
// Assignment: IntegerSet // Description: head of IntegerSet Class #ifndef INTEGERSET_H #define INTEGERSET_H class IntegerSet{ public: IntegerSet(); //default constructor; IntegerSet(int* set,int size); //constructor from an int array; IntegerSet(const IntegerSet& ia); //copy constructor; //three ways to initialize the instance;
friend std::istream& operator>>(std::istream & cin,IntegerSet& ia); //input overload friend std::ostream& operator<<(std::ostream & cout,IntegerSet& ia); //output overload void printSet(std::ostream &ostr=std::cout ); //output the instance;
IntegerSet operator+(IntegerSet &addedSet); //overload union IntegerSet operator-(IntegerSet &subbedSet); //overload intersection void unionOf(const IntegerSet &set1, const IntegerSet &set2 ); void intersectionOf( const IntegerSet &set1, const IntegerSet &set2 ); bool isEqualTo( const IntegerSet &set );
void insertElement( int num ); void deleteElement( int num );
bool isInSet( int num ); private: bool integer[101]; }; /*std::ostream& operator<<(std::ostream& cout,IntegerSet& ia){ cout<<"{"; for(int index=0;index<=100;index++){ if(ia.integer[index]==true) cout<<ia.integer[index]<<","; } cout<<"}"; return cout;
}*/
#endif
//IntegerSet.cpp // Assignment: IntegerSet // Description: detail of IntegerSet Class #include<iostream> #include "IntegerSet.h"
IntegerSet::IntegerSet(){ //initialize the set having no integer
for(int index=0;index<=100;index++) integer[index]=false; }
IntegerSet::IntegerSet(const IntegerSet & ia){ //initialize the set using copy constructor for(int index=0;index<=100;index++) integer[index]=false; for(index=0;index<=100;index++) if(ia.integer[index]) this->integer[index]=true; }
IntegerSet::IntegerSet(int* set,int size){ //initialize the set from a integer array
for(int index=0;index<=100;index++) integer[index]=false; for( index=0;index<size;index++) integer[set[index]]=true; }
void IntegerSet::unionOf(const IntegerSet &set1, const IntegerSet &set2 ){ for(int index=0;index<=100;index++){ if(set1.integer[index]==true||set2.integer[index]==true) this->integer[index]=true; else this->integer[index]=false; } }
void IntegerSet::intersectionOf( const IntegerSet &set1, const IntegerSet &set2 ){ for(int index=0;index<=100;index++){ if(set1.integer[index]==true&&set2.integer[index]==true) this->integer[index]=true; else this->integer[index]=false; } }
bool IntegerSet::isEqualTo( const IntegerSet &set ){ int index; for(index=0;index<=100;index++) { if(integer[index]!=set.integer[index]) return false; } return true; } void IntegerSet::printSet( std::ostream &ostr ) {
ostr<<"{"; for(int index=0;index<=100;index++){ if(this->isInSet(index)) ostr<<index<<","; } ostr<<'\b'; ostr<<"}"<<std::endl; } void IntegerSet::insertElement( int num ){ if(num<=100&&num>=0) this->integer[num]=true; }
void IntegerSet::deleteElement( int num ){ if(num<=100&&num>=0) this->integer[num]=false; }
bool IntegerSet::isInSet( int num ){ return this->integer[num]; }
IntegerSet IntegerSet::operator+(IntegerSet &addedSet){ IntegerSet temp; for(int index=0;index<=100;index++){ if(this->integer[index]==true||addedSet.integer[index]==true) temp.integer[index]=true; else temp.integer[index]=false; } return temp; } IntegerSet IntegerSet::operator-(IntegerSet &subbedSet){ IntegerSet temp; for(int index=0;index<=100;index++){ if(this->integer[index]==true&&subbedSet.integer[index]==true) temp.integer[index]=true; else temp.integer[index]=false; } return temp; }
// File Name: test.cpp
// Assignment: IntegerSet // Description: test of IntegerSet Class
#include <iostream> #include "IntegerSet.h"
using namespace std; istream& operator >> (istream & cin, IntegerSet& ia); ostream& operator << (ostream & cout, IntegerSet& ia); //Integer Class's friend to cin and cout IntegerSet //cin and cout overload
int main(){
IntegerSet intset1,intset2,clear; //three ways to initialize in fact //use default constructor
char flag='y'; //flag to decide whether want quit
do{ intset1.intersectionOf(intset1,clear); intset2.intersectionOf(intset2,clear); cout << "Enter set1:"; cin >> intset1;
cout << "Enter set2"; cin >> intset2;
cout << "set1 is:" << intset1; cout << "set2 is:"; intset2.printSet(); //two ways to output;
cout << "set1==set2:" << intset1.isEqualTo(intset2) << endl;
IntegerSet temp; //temp instance to hold the output of intersection and union //temp.intersectionOf(intset1,intset2); cout << "Intersection of both sets is" << intset1-intset2; //temp.unionOf(intset1,intset2); cout << "Union of both sets is " << intset1+intset2; int index; char ch; //temp variable for input cout << "Enter numbers to remove from Set 1:"; do{ cin >> index; ch=cin.get(); if( index<=100&&index>=0 ) intset1.deleteElement(index); }while( ch!='\n' ); //use int and ' ' format to input //take enter as finish sign
cout << "Enter numbers to insert into Set 2:"; do{ cin >> index; ch=cin.get(); if( index<=100&&index>=0 ) intset2.insertElement(index); }while( ch!='\n' ); //use int and ' ' format to input //take enter as finish sign cout << "set1 is:"<<intset1; cout << "set2 is:" << intset2;
cout << "set1==set2:" << intset1.isEqualTo(intset2) << endl;
temp.intersectionOf(intset1,intset2); cout << "Intersection of both sets is" << temp; temp.unionOf(intset1,intset2); cout << "Union of both sets is " << temp;
cout << "another try?y/n:"; flag=cin.get(); }while(flag!='n'); return 0; } ostream& operator << ( ostream & cout, IntegerSet& ia ){ cout << "{"; for( int index=0;index<=100;index++ ){ if( ia.integer[index]==true ) cout << index<< ','; } cout << '\b'; cout << "}" << endl; return cout; } istream& operator >> ( istream & cin, IntegerSet& ia ){ int index; char ch; cout << "input:"; do{ cin >> index; ch=cin.get(); if( index<=100&&index>=0 ) ia.insertElement(index); }while( ch!='\n' ); return cin; } 
|