|
| 1 | +/* |
| 2 | + This C++ program defines a 'Set' class that represents a set of integers and provides various set operations. |
| 3 | +
|
| 4 | + - The 'Set' class has private data members 'data', 'noOfElements', and 'capacity' to store the set elements, |
| 5 | + the number of elements in the set, and the capacity of the set, respectively. |
| 6 | +
|
| 7 | + - The class has constructors to create a set object and initialize its properties. |
| 8 | + The destructor is provided to free the dynamically allocated memory for the set. |
| 9 | +
|
| 10 | + - The 'insert' function inserts a new element into the set. |
| 11 | +
|
| 12 | + - The 'remove' function removes an element from the set. |
| 13 | +
|
| 14 | + - The 'getCardinality' function returns the number of elements in the set. |
| 15 | +
|
| 16 | + - The 'calcUnion', 'calcSymmetricDifference', and 'calcDifference' functions perform set operations |
| 17 | + Union, Symmetric Difference, and Difference, respectively, and return a new set object with the result. |
| 18 | +
|
| 19 | + - The 'isMember' function checks if an element is present in the set. |
| 20 | +
|
| 21 | + - The 'isSubSet' function checks if the current set is a proper or improper subset of another set. |
| 22 | +
|
| 23 | + - The 'reSize' function resizes the set to the specified new capacity. |
| 24 | +
|
| 25 | + - The 'update' function updates the value of an element in the set with a new value. |
| 26 | +
|
| 27 | + - The 'show' function displays the elements of the set. |
| 28 | +
|
| 29 | + Note: The program demonstrates the implementation of a set using dynamic memory allocation. |
| 30 | + It showcases various set operations and how elements can be inserted, removed, and updated in the set. |
| 31 | +*/ |
| 32 | +#include <iostream> |
| 33 | +using namespace std; |
| 34 | + |
| 35 | +class Set { |
| 36 | +private: |
| 37 | + int* data; |
| 38 | + int noOfElements; |
| 39 | + int capacity; |
| 40 | + |
| 41 | +public: |
| 42 | + Set(int cap = 0); |
| 43 | + Set(const Set& ref); |
| 44 | + ~Set(); |
| 45 | + void insert(int element); |
| 46 | + void remove(int element); |
| 47 | + int getCardinality() const; |
| 48 | + Set calcUnion(const Set& s2) const; |
| 49 | + Set calcSymmetricDifference(const Set& s2) const; |
| 50 | + Set calcDifference(const Set& s2) const; |
| 51 | + int isMember(int val) const; |
| 52 | + int isSubSet(const Set& s2) const; |
| 53 | + void reSize(int newcapacity); |
| 54 | + void update(int prVal, int curVal); |
| 55 | + void show() const; |
| 56 | +}; |
| 57 | + |
| 58 | +Set::Set(int cap) { |
| 59 | + // Ensure capacity is not negative |
| 60 | + if (cap < 0) { |
| 61 | + cap = 0; |
| 62 | + } |
| 63 | + |
| 64 | + this->data = new int[cap]; |
| 65 | + this->capacity = cap; |
| 66 | + this->noOfElements = 0; |
| 67 | +} |
| 68 | + |
| 69 | +Set::Set(const Set& ref) { |
| 70 | + noOfElements = ref.noOfElements; |
| 71 | + data = new int[ref.capacity]; |
| 72 | + for (int i = 0; i < noOfElements; i++) { |
| 73 | + data[i] = ref.data[i]; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +Set::~Set() { |
| 78 | + delete[] data; |
| 79 | +} |
| 80 | + |
| 81 | +void Set::insert(int element) { |
| 82 | + int* ptr = new int[this->noOfElements + 1]; |
| 83 | + for (int i = 0; i < noOfElements; i++) { |
| 84 | + ptr[i] = data[i]; |
| 85 | + } |
| 86 | + ptr[noOfElements] = element; |
| 87 | + delete[] data; |
| 88 | + data = ptr; |
| 89 | + noOfElements++; |
| 90 | +} |
| 91 | + |
| 92 | +void Set::remove(int element) { |
| 93 | + int* ptr = new int[this->noOfElements - 1]; |
| 94 | + int j = 0; |
| 95 | + for (int i = 0; i < noOfElements; i++) { |
| 96 | + if (data[i] != element) { |
| 97 | + ptr[j] = data[i]; |
| 98 | + j++; |
| 99 | + } |
| 100 | + } |
| 101 | + delete[] data; |
| 102 | + data = ptr; |
| 103 | + noOfElements--; |
| 104 | + |
| 105 | + // Check if the Set needs to be resized |
| 106 | + if (noOfElements <= capacity / 2) { |
| 107 | + reSize(capacity - capacity / 4); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +int Set::getCardinality() const { |
| 112 | + return noOfElements; |
| 113 | +} |
| 114 | + |
| 115 | +Set Set::calcUnion(const Set& s2) const { |
| 116 | + Set result(this->capacity + s2.capacity); |
| 117 | + for (int i = 0; i < this->noOfElements; i++) { |
| 118 | + result.insert(this->data[i]); |
| 119 | + } |
| 120 | + for (int i = 0; i < s2.noOfElements; i++) { |
| 121 | + if (!this->isMember(s2.data[i])) { |
| 122 | + result.insert(s2.data[i]); |
| 123 | + } |
| 124 | + } |
| 125 | + return result; |
| 126 | +} |
| 127 | + |
| 128 | +Set Set::calcSymmetricDifference(const Set& s2) const { |
| 129 | + Set result(this->capacity + s2.capacity); |
| 130 | + for (int i = 0; i < this->noOfElements; i++) { |
| 131 | + if (!s2.isMember(this->data[i])) { |
| 132 | + result.insert(this->data[i]); |
| 133 | + } |
| 134 | + } |
| 135 | + for (int i = 0; i < s2.noOfElements; i++) { |
| 136 | + if (!this->isMember(s2.data[i])) { |
| 137 | + result.insert(s2.data[i]); |
| 138 | + } |
| 139 | + } |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +Set Set::calcDifference(const Set& s2) const { |
| 144 | + Set result(this->capacity); |
| 145 | + for (int i = 0; i < this->noOfElements; i++) { |
| 146 | + if (s2.isMember(this->data[i])) { |
| 147 | + result.insert(this->data[i]); |
| 148 | + } |
| 149 | + } |
| 150 | + return result; |
| 151 | +} |
| 152 | + |
| 153 | +int Set::isMember(int val) const { |
| 154 | + for (int i = 0; i < this->noOfElements; i++) { |
| 155 | + if (this->data[i] == val) { |
| 156 | + return 1; |
| 157 | + } |
| 158 | + } |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +int Set::isSubSet(const Set& s2) const { |
| 163 | + int count = 0; |
| 164 | + for (int i = 0; i < s2.noOfElements; i++) { |
| 165 | + if (this->isMember(s2.data[i])) { |
| 166 | + count++; |
| 167 | + } |
| 168 | + } |
| 169 | + if (count == this->noOfElements && count < s2.noOfElements) { |
| 170 | + return 1; // Proper Subset |
| 171 | + } else if (count == this->noOfElements && count == s2.noOfElements) { |
| 172 | + return 2; // Improper Subset |
| 173 | + } else { |
| 174 | + return 0; // Not a Subset |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +void Set::reSize(int newcapacity) { |
| 179 | + if (newcapacity < 0) { |
| 180 | + newcapacity = 0; |
| 181 | + } |
| 182 | + |
| 183 | + if (newcapacity >= this->noOfElements) { |
| 184 | + int* newdata = new int[newcapacity]; |
| 185 | + for (int i = 0; i < this->noOfElements; i++) { |
| 186 | + newdata[i] = data[i]; |
| 187 | + } |
| 188 | + |
| 189 | + delete[] data; |
| 190 | + data = newdata; |
| 191 | + capacity = newcapacity; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +void Set::update(int prVal, int curVal) { |
| 196 | + if (!isMember(prVal)) { |
| 197 | + cout << "Target value not found." << endl; |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + if (isMember(curVal)) { |
| 202 | + cout << "Violation set property. Can't Modify the data." << endl; |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + for (int i = 0; i < noOfElements; i++) { |
| 207 | + if (data[i] == prVal) { |
| 208 | + data[i] = curVal; |
| 209 | + } |
| 210 | + } |
| 211 | + cout << "Record update successfully." << endl; |
| 212 | +} |
| 213 | + |
| 214 | +void Set::show() const { |
| 215 | + for (int i = 0; i < noOfElements; i++) { |
| 216 | + cout << data[i] << " "; |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +int main() { |
| 221 | + Set s(10); |
| 222 | + |
| 223 | + s.insert(5); |
| 224 | + s.insert(10); |
| 225 | + s.insert(15); |
| 226 | + |
| 227 | + Set s2 = s; |
| 228 | + s2.insert(20); |
| 229 | + |
| 230 | + Set s3 = s.calcUnion(s2); |
| 231 | + Set s4 = s.calcSymmetricDifference(s2); |
| 232 | + Set s5 = s.calcDifference(s2); |
| 233 | + |
| 234 | + s.show(); // Output: 5 10 15 |
| 235 | + cout << endl; |
| 236 | + |
| 237 | + s2.show(); // Output: 5 10 15 20 |
| 238 | + cout << endl; |
| 239 | + |
| 240 | + s3.show(); // Output: 5 10 15 20 |
| 241 | + cout << endl; |
| 242 | + |
| 243 | + s4.show(); // Output: 20 |
| 244 | + cout << endl; |
| 245 | + |
| 246 | + s5.show(); // Output: 5 10 15 |
| 247 | + cout << endl; |
| 248 | + |
| 249 | + s.update(5, 25); // Record update successfully. |
| 250 | + |
| 251 | + s.show(); // Output: 25 10 15 |
| 252 | + cout << endl; |
| 253 | + |
| 254 | + return 0; |
| 255 | +} |
0 commit comments