Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class SetArray
  5. {
  6. //1. encapsulate
  7. private:
  8. int *ARRAY;
  9. int count;
  10. int capacity;
  11.  
  12. //helper functions
  13.  
  14. bool isFull()
  15. {
  16. return (capacity == count);
  17. }
  18.  
  19. bool isEmpty()
  20. {
  21. return (count==0);
  22. }
  23.  
  24. void doubleTheSize()
  25. {
  26. int newcap = capacity * 2;
  27.  
  28. int *NEWARRAY = new int[newcap];
  29.  
  30. for (int i=0; i < count; i++)
  31. NEWARRAY[i] = ARRAY[i];
  32.  
  33. ARRAY = NEWARRAY;
  34. capacity = newcap;
  35.  
  36. NEWARRAY = NULL;
  37. }
  38.  
  39. public:
  40. int getCount() {
  41. return count;
  42. }
  43.  
  44. int getCapacity() {
  45. return capacity;
  46. }
  47.  
  48. int *getArray(){
  49. return ARRAY;
  50. }
  51.  
  52. int cardinality() {
  53. return getCount();
  54. }
  55. // constructor
  56. SetArray()
  57. {
  58. count = 0;
  59. capacity = 3;
  60.  
  61. ARRAY = new int[capacity];
  62. }
  63. bool exists(int item)
  64. {
  65. for (int i=i; i < count; i++)
  66. {
  67. if (ARRAY[i] == item)
  68. return true;
  69. }
  70. return false;
  71. }
  72. void add(int item)
  73. {
  74. if (!isFull())
  75. if (!exists(item))
  76. ARRAY[count++] = item;
  77. else
  78. cout << "Item " << item << " already exists...\n";
  79. else
  80. {
  81. doubleTheSize();
  82. add(item);
  83. }
  84. }
  85.  
  86. void display()
  87. {
  88. cout << "S[";
  89. for (int i = 0; i < count; i++)
  90. cout << ARRAY[i] << (i < count-1 ? ", " : "");
  91. cout << "]\n";
  92. }
  93.  
  94. friend ostream& operator<<(ostream& os, SetArray& sa)
  95. {
  96. os << "S[";
  97. for (int i = 0; i < sa.getCount(); i++)
  98. cout << sa.getArray()[i] << (i < sa.getCount()-1 ? ", " : "");
  99. cout << "]\n";
  100. }
  101.  
  102. //operations
  103.  
  104. SetArray unions(SetArray *as)
  105. {
  106. SetArray temp;
  107.  
  108. for (int i=0; i < getCount(); i++)
  109. temp.add(getArray()[i]);
  110.  
  111. for (int i=0; i < as->getCount(); i++)
  112. temp.add(as->getArray()[i]);
  113.  
  114. return temp;
  115. }
  116.  
  117. SetArray intersection(SetArray *as)
  118. {
  119. SetArray temp;
  120.  
  121. for (int i=0; i < getCount(); i++)
  122. if (as->exists(getArray()[i]))
  123. temp.add(getArray()[i]);
  124. return temp;
  125. }
  126.  
  127.  
  128. };
  129.  
  130. int main(int argc, char** argv) {
  131.  
  132. SetArray sa, sb, sc, sd;
  133.  
  134. sa.add(5);
  135. sa.add(75);
  136. sa.add(6);
  137. sa.add(100);
  138.  
  139. sb.add(8);
  140. sb.add(12);
  141. sb.add(5);
  142.  
  143. cout << sa;
  144. cout << sb;
  145.  
  146. cout << "Union = " ;
  147. sc = sa.unions(&sb);
  148.  
  149. cout << sc;
  150. cout << "Intersection = " ;
  151. sd = sa.intersection(&sb);
  152.  
  153. cout << sd;
  154.  
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement