# Operator overloaded set intersection and relative complement dislocation

## Question D: Set (Operator overload)

Time limit:   1 Sec    Memory limit:   128 MB
Submit:   187    Solve:   109
[Submit][state][Discussion Edition]

## Title Description

A collection is a whole made up of one or more deterministic elements. Set operations include union, intersection, relative complement, and so on.

Intersection of set A and B: A collection of the same elements belonging to A and B.

Union of set A and B: A collection of all elements belonging to set A or B.

Set B is the relative complement of set A, denoted as A-B: a set of elements belonging to A but not B.

Suppose set A={10, 20, 30} and set B={1, 10, 50, 8}. Then the union of A and B is {10, 20, 30, 1, 50, 8}, the intersection of A and B is {10}, and the relative complement of B with respect to A is {20, 30}.

Defines an integer collection class CSet with attributes such as the number of elements in the collection n, and integer pointer data stores the elements in the collection.

The methods are to overload the output and output the elements in the collection in a sample format.

Overload the + operator, find the union of set A and B, and return the result set.

Overload-operator, finds the relative complement of set B with respect to set A, and returns the result set.

Overload the * operator, intersect set A and B, and return the result set.

The main function inputs the data of sets A and B and calculates the union, intersection and relative complement of sets.

You can add the required member functions to the CSet class by title.

## input

Number of Tests

Each set of test data has two rows in the following format:

First line: Number and elements of set A

Line 2: Number and elements of set B

## output

Each set of test data is output as follows:

First line: Set A

Line 2: Set B

Line 3: Union of A and B

Line 4: Intersection of A and B

Line 5: The union of B's relative complement with A's relative complement with B, namely (A-B)+(B-A)

Each set of test data is separated by blank lines.

## sample input

2 3 10 20 30 4 10 1 2 3 5 100 2 3 4 -10 6 -34 12 2 4 90 100

## sample output

A:10 20 30 B:10 1 2 3 A+B:10 20 30 1 2 3 A*B:10 (A-B)+(B-A):20 30 1 2 3 A:100 2 3 4 -10 B:-34 12 2 4 90 100 A+B:100 2 3 4 -10 -34 12 90 A*B:100 2 4 (A-B)+(B-A):3 -10 -34 12 90

```#include <iostream>
using namespace std;
class CSet {
int n;
int* data;
public:
CSet(int n1 = 0, int* a = NULL) {
n = n1;
data = new int[n + 1];
for (int i = 0; i < n; i++) {
data[i] = a[i];
}
}
friend ostream& operator<<(ostream& os, CSet& a) {
for (int i = 0; i < a.n; i++) {
os << a.data[i];
if (i < a.n - 1)
os << ' ';
else if (i == a.n - 1)
os << '\n';
}
return os;
}
friend CSet operator+(CSet& a, CSet&b) {
int tn = b.n + a.n;
int* temp = new int[tn + 1];
int* temp2 = new int[tn + 1];
for (int i = 0; i < tn; i++) {
if (i < b.n)
temp[i] = b.data[i];
else if (i >=b.n)
temp[i] = a.data[i - b.n];
}
temp2 = temp;
int flag = 0;
int ntemp = 0;
for (int i = 0; i < tn; i++) {
for (int j = 0; j < i; j++) {
if (temp2[j] == temp[i]) {
flag = 1;
}
}
if (flag == 0) {
temp2[i - ntemp] = temp[i];
}
else if (flag == 1) {
ntemp++;
}
flag = 0;
}
CSet cs(tn - ntemp, temp2);
return cs;
}
friend CSet operator * (CSet& a, CSet& b)
{
CSet c;
int j, k, i = 0;
int* p = new int[a.n];
for (j = 0; j < a.n; j++)
{
for (k = 0; k < b.n; k++)
{
if (a.data[j] == b.data[k])
{
p[i] = a.data[j];
i++;
break;
}
}
}
c.data = new int[i];
c.n = i;
for (j = 0; j < i; j++)
{
c.data[j] = p[j];
}

delete[]p;
return c;
}
friend CSet operator - (CSet& a, CSet& b)
{
CSet c;
int i = 0, j, k, z;
int* p = new int[a.n + b.n];
for (j = 0; j < a.n; j++)
{
z = 0;
for (k = 0; k < b.n; k++)
{
if (a.data[j] == b.data[k])
{
z = 0;
break;
}
else
z = 1;
}
if (z == 1)
{
p[i] = a.data[j];
i++;
}
}
c.n = i;
c.data = new int[i];
for (j = 0; j < i; j++)
{
c.data[j] = p[j];
}
delete[]p;
return c;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n, n2;
cin >> n;
int* a = new int[n + 1];
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> n2;
int* a2 = new int[n2 + 1];
for (int i = 0; i < n2; i++)
cin >> a2[i];
CSet A(n, a);
CSet B(n2, a2);
CSet C = A + B;
CSet D = B - A;
CSet E;
cout << "A:" << A;
cout << "B:" << B;
cout << "A+B:" << C;
C = A * B;
cout << "A*B:" << C;
C = A - B;
E = C + D;
//E = (A - B) + (B - A);
cout << "(A-B)+(B-A):" << E<<endl;
delete[]a;
delete[]a2;
}
}```

The focus is on the dislocation of intersection and relative complement, and the handling methods are as follows:

```friend CSet operator * (CSet& a, CSet& b)
{
CSet c;
int j, k, i = 0;
int* p = new int[a.n];
for (j = 0; j < a.n; j++)
{
for (k = 0; k < b.n; k++)
{
if (a.data[j] == b.data[k])
{
p[i] = a.data[j];
i++;
break;
}
}
}
c.data = new int[i];
c.n = i;
for (j = 0; j < i; j++)
{
c.data[j] = p[j];
}

delete[]p;
return c;
}
friend CSet operator - (CSet& a, CSet& b)
{
CSet c;
int i = 0, j, k, z;
int* p = new int[a.n + b.n];
for (j = 0; j < a.n; j++)
{
z = 0;
for (k = 0; k < b.n; k++)
{
if (a.data[j] == b.data[k])
{
z = 0;
break;
}
else
z = 1;
}
if (z == 1)
{
p[i] = a.data[j];
i++;
}
}
c.n = i;
c.data = new int[i];
for (j = 0; j < i; j++)
{
c.data[j] = p[j];
}
delete[]p;
return c;
}
};```

The key is to find the complement and intersection, array subscript variables do not need to be the same as variables in the loop, you can easily misplace!!!

Tags: C++

Posted on Sun, 05 Dec 2021 14:18:46 -0500 by Spectre