-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerSet.cpp
89 lines (73 loc) · 2.25 KB
/
IntegerSet.cpp
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
//
// Keita Nonaka
//
// This is Rational class, which calculate rational numbers.
// This class is for implementing methods.
//
#include "IntegerSet.h"
#include <iostream>
using namespace std;
IntegerSet::IntegerSet() { // constructor
initializeVT();
}
IntegerSet::~IntegerSet() { // destructor
// nothing
}
void IntegerSet::initializeVT() { // integer set, initialize 101 sets as false
for (int i = 0; i < 101; i++)
integerSet.push_back(false);
}
IntegerSet IntegerSet::intersect(IntegerSet intset) { // intersect
IntegerSet setC;
for (int i = 0; i < 101; i++) {
setC.integerSet[i] = this->integerSet[i] && intset.integerSet[i];
}
return setC;
}
IntegerSet IntegerSet::unionize(IntegerSet intset) { // union
IntegerSet setC;
for (int i = 0; i < 101; i++) {
setC.integerSet[i] = this->integerSet[i] || intset.integerSet[i];
}
return setC;
}
IntegerSet IntegerSet::complement() { // complement
for (int i = 0; i < 101; i++)
integerSet[i] = !integerSet[i];
return *this;
}
IntegerSet IntegerSet::difference(IntegerSet intset) { // difference
IntegerSet setC = this->intersect(intset);
IntegerSet setD;
setC = setD.unionize(setC).complement();
setC = this->intersect(setC);
return setC;
}
bool IntegerSet::subset(IntegerSet intset) { // subset
IntegerSet setC = this->intersect(intset);
return this->isEqualTo(setC);
}
bool IntegerSet::isEmpty() { // empty or not
vector<bool>::iterator itr = integerSet.begin(); // iterator
itr = find(itr, integerSet.end(), true);
return itr == integerSet.end();
}
bool IntegerSet::isEqualTo(IntegerSet intset) { // is equal to
return this->integerSet == intset.integerSet;
}
void IntegerSet::insertElement(int num) { // insert element to vector
integerSet[num] = true;
}
void IntegerSet::deleteElement(int num) { // delete element of set
integerSet[num] = false;
}
void IntegerSet::printSet() { // print element of set
vector<bool>::iterator itr = integerSet.begin(); // iterator
itr = find(itr, integerSet.end(), true);
while(itr != integerSet.end()) {
cout << itr - integerSet.begin() << " ";
++itr;
itr = find(itr, integerSet.end(), true);
}
cout << "}" << endl;
}