-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ordered set exercise with set class and operations like union, intersection, difference, contains, empty_group
- Loading branch information
Showing
3 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
#include "Header.h" | ||
#include <iostream> | ||
#define ZERO 0 | ||
#define ONE 1 | ||
void OrderedSet:: Sort_G (int * group, int size) | ||
{ | ||
while (Orderd_G(group, size) == false) | ||
{ | ||
|
||
|
||
for (int i = ONE; i < size; i++) { | ||
if (group[i] < group[i - ONE]) { | ||
int temp = group[i]; | ||
group[i] = group[i - ONE]; | ||
group[i - ONE] = temp; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
bool OrderedSet::Orderd_G(int *group, int size) | ||
{ | ||
for (int i = ONE; i < size; i++) { | ||
if (group[i] < group[i - ONE]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
|
||
} | ||
|
||
int OrderedSet::ReturningValue(int * group, int size) | ||
{ | ||
int ReturnV = ZERO; | ||
for (int i = ZERO; i < size; i++) { | ||
for (int j = i + ONE; j < size; j++) { | ||
if (group[i] == group[j]) { | ||
ReturnV ++; | ||
} | ||
} | ||
} | ||
return ReturnV; | ||
} | ||
|
||
bool OrderedSet::inArray(int *group, int size, int num ) | ||
{ | ||
for (int i = ZERO; i < size; i++) { | ||
if (group[i] == num) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int OrderedSet::SameValues(OrderedSet obj) | ||
{ | ||
int i ,*Tgroup = obj.Get_Group(),Tsize = obj.Get_Size(), count = ZERO; | ||
for (i = ZERO; i < Size; i++) { | ||
for (int j = ZERO; j < Tsize; j++) { | ||
if (Group[i] == (Tgroup)[j]) | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
OrderedSet::OrderedSet(int *group, int size) | ||
{ | ||
int count = ReturningValue(group, size); | ||
if(count == ZERO) { | ||
Sort_G(group , size); | ||
Group = group; | ||
Size = size; | ||
} | ||
else { | ||
Size = size - count; | ||
int j = ONE; | ||
Group = new int[Size]; | ||
Group[ZERO] = group[ZERO]; | ||
for (int i = ONE; i < size; i++) { | ||
if (inArray(Group, i , group[i]) == false) { | ||
Group[j++] = group[i]; | ||
} | ||
} | ||
Sort_G(Group, size - count); | ||
} | ||
} | ||
|
||
|
||
|
||
OrderedSet::OrderedSet(const OrderedSet & obj) | ||
{ | ||
Size = obj.Size; | ||
Group = new int[Size]; | ||
for (int i = ZERO; i < Size; i++) { | ||
Group[i] = (obj.Group)[i]; | ||
} | ||
} | ||
|
||
bool OrderedSet::Contain(int num) | ||
{ | ||
for (int i = ZERO; i < Size; i++) { | ||
if (Group[i] == num) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void OrderedSet::ChangeGroup(int * group, int size) | ||
{ | ||
if ((ReturningValue(group, size) == ZERO) && Orderd_G(group, size)) { | ||
delete[] Group; | ||
Group = group; | ||
Size = size; | ||
} | ||
else { | ||
cout << "the array isn't a group" << endl; | ||
} | ||
} | ||
|
||
OrderedSet OrderedSet::Def(OrderedSet obj) | ||
{ | ||
int j = ZERO; | ||
OrderedSet def; | ||
def.Size = Size - SameValues(obj); | ||
def.Group = new int[def.Size]; | ||
for (int i = ZERO; i < Size; i++) { | ||
if (obj.Contain((Group)[i]) == false) { | ||
(def.Group)[j++] = Group[i]; | ||
} | ||
} | ||
return def; | ||
} | ||
|
||
OrderedSet OrderedSet::intersect(OrderedSet obj) | ||
{ | ||
int c = ZERO; | ||
OrderedSet inter; | ||
if (obj.Size == ZERO) { | ||
inter.Group = NULL; | ||
inter.Size = ZERO; | ||
} | ||
else { | ||
inter.Size = SameValues(obj); | ||
inter.Group = new int[inter.Size]; | ||
for (int i = ZERO; i < Size; i++) { | ||
for (int j = ZERO; j < obj.Size; j++) { | ||
if (Group[i] == (obj.Group)[j]) { | ||
(inter.Group)[c++] = Group[i]; | ||
} | ||
|
||
} | ||
} | ||
} | ||
return inter; | ||
} | ||
|
||
OrderedSet OrderedSet::Union(OrderedSet obj) | ||
{ | ||
int c = ZERO,j = ZERO,i; | ||
OrderedSet Union; | ||
Union.Size = Size + obj.Size - SameValues(obj); | ||
Union.Group = new int[Union.Size]; | ||
for (i = ZERO; i < Size; i++) { | ||
(Union.Group)[i] = Group[j++]; | ||
} | ||
for (j = ZERO; j < obj.Size; j++) { | ||
if (Contain((obj.Group)[j]) == false) { | ||
(Union.Group)[i++] = (obj.Group)[j]; | ||
} | ||
} | ||
Sort_G(Union.Group, Union.Size); | ||
return Union; | ||
} | ||
|
||
bool OrderedSet::EqualGroups(OrderedSet obj) | ||
{ | ||
int i, j ,*Tgroup = obj.Get_Group(); | ||
for (i = ZERO; i < Size; i++) { | ||
j = i; | ||
if (Group[i] != Tgroup[j]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool OrderedSet::EmptyGroup() | ||
{ | ||
if (Size == ZERO) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool OrderedSet::Contain(OrderedSet obj) | ||
{ | ||
if (obj.Size == ZERO) { | ||
return true; | ||
} | ||
for (int i = ZERO; i < obj.Size ; i++) { | ||
if (Contain((obj.Group)[i]) == false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
void OrderedSet::PrintGroup() | ||
{ | ||
if (EmptyGroup() == true) { | ||
cout << '{' << '}' << endl; | ||
} | ||
else { | ||
cout << '{'; | ||
for (int i = ZERO; i < Size; i++) { | ||
if (i == Size - ONE) { | ||
cout << Group[i] << '}' << endl; | ||
} | ||
else { | ||
cout << Group[i] << ','; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <string> | ||
#define ZERO 0 | ||
#define ONE 1 | ||
using namespace std; | ||
class OrderedSet { //����� �� ����� | ||
private: // ����� ����� ����� �� ����� ������ ��������� ��� | ||
int * Group; | ||
int Size; | ||
void Sort_G(int *, int); //������� ����� ���� ������ | ||
bool Orderd_G(int *, int); //�������� ������ �� ���� ����� | ||
int ReturningValue(int * group, int size); //�������� ������ �� ���� ������ ������� ����� | ||
bool inArray(int*, int, int); //������� ������ �� ���� ��� ���� ����� | ||
int SameValues(OrderedSet obj); //������� ������ ���� ����� ������ ��� ��� ������ ����� | ||
|
||
public: | ||
OrderedSet() : Group(NULL), Size(ZERO) {} //���� �������� | ||
OrderedSet(int *group, int size); //���� �� ������ | ||
OrderedSet(const OrderedSet & obj); // ���� ����� | ||
~OrderedSet() { delete[] Group; } // ���� ������ �� ������� | ||
int *Get_Group() { return Group; } //�������� ����� ����� ����� | ||
int Get_Size() { return Size; } | ||
bool Contain(int); //������� ������ �� ���� ��� ����� ������ ������ | ||
void ChangeGroup(int * group, int size); // ����� ����� ����� ����� ������ ��� ��������(�� �� ����� ������ ����� ������ | ||
OrderedSet Def(OrderedSet obj); //�������� ������ ���� ��� ������ | ||
OrderedSet intersect(OrderedSet obj); // �������� ������ ����� ��� ������ | ||
OrderedSet Union(OrderedSet obj); // �������� ������ ����� �� ������ | ||
bool EqualGroups(OrderedSet obj); // �������� ������ �� ������ ���� | ||
bool EmptyGroup(); //������� ������ �� ����� ��� ������ ����� | ||
bool Contain(OrderedSet obj); //�������� ������ �� ����� ��� ����� �� ������ | ||
void PrintGroup(); //�������� ������� ����� | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "Header.h" | ||
#include <iostream> | ||
#define ZERO 0 | ||
#define ONE 1 | ||
int main() { //úåëðéú äîöéâä àúçåì ÷áåöåú îñôøéí ùìîéíú çéùåá çéúåëï ,äôøùï,àéçåãï | ||
cout << "creating an orderset orderset1 with the following parmeters:\narr1 = {2,3,4,8,9,10,1,5,7,6}\nsize1 = 10" << endl; | ||
int arr1[10] = { 2,3,4,8,9,10,1,5,7,6 }; | ||
int arr2[10] = { 2,3,4,-1 - 2,5,4,-3,1,-4,-5 }; | ||
OrderedSet OS1(arr1, 10); | ||
cout << "printing orderset1:"; | ||
OS1.PrintGroup(); | ||
cout << "Creating an OrderedSet orderedSet2 with the following parameters :\narr2 = { 2,3,4,-1,- 2,5,4,-3,1,-4,-5 }\nsize2 = 10" << endl; | ||
OrderedSet OS2(arr2, 10); | ||
cout << "printing orderset2:"; | ||
OS2.PrintGroup(); | ||
cout << "Creating empty orderedSet orderedSet3 without parameters" << endl; | ||
OrderedSet OS3; | ||
cout << "printing orderset3:"; | ||
OS3.PrintGroup(); | ||
cout << "calculating the intersection of orderedSet1 and orderedSet2:" << endl; | ||
OrderedSet Intersection(OS1.intersect(OS2)); | ||
Intersection.PrintGroup(); | ||
cout << "calculating the intersection of orderedSet1 and orderedSet3:" << endl; | ||
OrderedSet I2(OS1.intersect(OS3)); | ||
I2.PrintGroup(); | ||
cout << "calculating the union of orderedSet1 and orderedSet2" << endl; | ||
OrderedSet U1(OS1.Union(OS2)); | ||
U1.PrintGroup(); | ||
cout << "calculating the union of orderedSet2 and orderedSet3" << endl; | ||
OrderedSet U2(OS2.Union(OS3)); | ||
U2.PrintGroup(); | ||
if (OS1.Contain(OS3)) { | ||
cout << "orderedSet1 contains orderedSet3" << endl; | ||
} | ||
else | ||
{ | ||
cout << "orderedSet1 doesn't contains orderedSet3" << endl; | ||
} | ||
if (OS2.Contain(OS1) == false) { | ||
cout << "orderedSet2 doesn't contain orderedSet1" << endl; | ||
} | ||
else | ||
{ | ||
cout << "orderedSet2 contains orderedSet1" << endl; | ||
} | ||
cout << "calculating the def of orderedSet1 - orderedSet2:" << endl; | ||
OrderedSet def(OS1.Def(OS2)); | ||
def.PrintGroup(); | ||
|
||
return ZERO; | ||
} |