1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
//driver program for testing the IntegerSet class.
//Instantiates several IntegerSet objects and executes
//all member functions of the IntegerSet class.
//
#include <iostream>
using namespace std;
#include "Second_Try_Lab05.h" //include IntegerSet class definition
int main()
{
// IntegerSet holds integers in the range 0..100
const int maxInteger = 100;
// Test default constructor --
// Create an empty set s
IntegerSet s;
cout << "Initial empty set: " << endl;
s.printSet();
cout << endl;
// Test insertElement function --
// Insert powers of 5 into Integer set s
for (int i = 0; i <= maxInteger; i += 5)
{
s.insertElement(i);
}
cout << "Inserted powers of 5: " << endl;
s.printSet();
cout << endl;
// Test deleteElement Function --
// Delete powers of 10 from IntegerSet s
for (int i = 0; i <= maxInteger; i+= 10)
{
s.deleteElement(i);
}
cout << "Deleted powers of 10: " << endl;
s.printSet();
cout << endl;
// Create a set of even integers
IntegerSet evenSet;
for (int i = 0; i <= maxInteger; i+=2)
{
evenSet.insertElement(i);
}
cout << "Even Integers: " << endl;
evenSet.printSet();
cout << endl;
// Create a set of odd integers
IntegerSet oddSet;
for (int i = 1; i <= maxInteger; i+=2)
{
oddSet.insertElement(i);
}
cout << "Odd Integers: " << endl;
oddSet.printSet();
cout << endl;
// Test unionOfSets Function --
// Print union of even and odd integers which
// results in a set of all integers between 0..100
s = evenSet.unionOfSets(oddSet);
cout << "Union of Even and Odd Integers: " << endl;
s.printSet();
cout << endl;
// Test intersectionOfSets Function --
// Print intersection of even and odd integers which
// results in an empty set
s = evenSet.intersectionOfSets(oddSet);
cout << "Intersection of Even and Odd Integers: " << endl;
s.printSet();
cout << endl;
// Test isEqualTo Function --
// Even integers should not be equal to odd integers.
// if (evenSet.isEqualTo(oddSet)) //HERE IS ONE OF TWO FUNCTION CALLS FOR THE ISeQUALtO MEMBER FUNCTION
{
cout << "Even Integer Set is equal "
<< "to Odd Integer Set." << endl;
}
else
{
cout << "Even Integer Set is NOT equal "
<< "to Odd Integer Set." << endl;
}
// Test isEqualTo Function --
// Set s, the intersection of even and odd integers,
// should equal the empty set.
IntegerSet emptySet;
// if (s.isEqualTo(emptySet)) //HERE IS THE SECOND OF TWO FUNCTION CALLS FOR THE ISeQUALtO MEMBER FUNCTION
{
cout << "Intersection of even and odd integers "
<< "is equal to the Empty Set." << endl;
}
else
{
cout << "Intersection of even and odd integers "
<< "is NOT equal to the Empty Set." << endl;
}
cout << endl;
// Test constructor from array of integers --
// Create a set of prime numbers between 0 to 100
const int numPrimes = 25;
int primes[numPrimes] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97};
IntegerSet primeSet(primes, numPrimes);
cout << "Prime Integers: " << endl;
primeSet.printSet();
cout << endl;
return 0;
}
//IntegerSet.h Class declaration
//
//IntegerSet class declaration
#include <iostream>
#include <vector>
using namespace std;
//prevents multiple inclusion of header
#ifndef INTEGERSET_H
#define INTEGERSET_H
const int maxInteger =100;
class IntegerSet
{
public:
IntegerSet();
IntegerSet(int [], int);
void printSet();
void insertElement(int);
void deleteElement(int);
bool getElement(int);
IntegerSet unionOfSets(IntegerSet);
IntegerSet intersectionOfSets(IntegerSet);
// bool isEqualTo(IntegerSet);
private:
int numberSet [101];
}; //end class IntegerSet
#endif
//IntegerSet.cpp
//IntegerSet class member function definitions
#include <iostream>
#include "Second_Try_Lab05.h"
using namespace std;
//constructor
IntegerSet::IntegerSet()
{
for ( int a = 0; a <= maxInteger; a++)
{
numberSet[a] = false;
}
}
IntegerSet::IntegerSet( int b[], int SIZE)
{
}
void IntegerSet::printSet()
{
int count = 1;
for (int j = 0; j <=maxInteger; j++)
{
if (count%20 == 0)
{
if (numberSet[j])
{
cout << j << " " << endl;
count += 1;
}
}
else
{
if (numberSet[j])
{
cout << j << " ";
count += 1;
}
}
}
if (count == 1)
{
cout << "---" << endl;
}
cout << endl << endl;
}
bool IntegerSet::getElement(int a)
{
return numberSet[a];
}
void IntegerSet::insertElement(int a)
{
numberSet[a] = true;
return;
}
void IntegerSet::deleteElement(int a)
{
numberSet[a] = false;
return;
}
//bool IntegerSet::isEqualTo(IntegerSet)
//{
// what goes here???
//}
IntegerSet IntegerSet::unionOfSets(IntegerSet setA)
{
IntegerSet s;
for (int k = 0; k <= maxInteger; k++)
{
if (setA.getElement(k) == true || numberSet[k] == true)
{
s.insertElement(k);
}
}
return s;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet setA)
{
IntegerSet s;
for (int k = 0; k <= maxInteger; k++)
{
if (setA.getElement(k) == true && numberSet[k] == true)
{
s.insertElement(k);
}
}
return s;
}
|