In: Computer Science
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5].
The following operations should be supported:
- Check if the value x belongs to the given interval.
- Check if the value x belongs to the intersection of two intervals.
- Check if the value x belongs to the union of two intervals.
Test this class in the main program.
Hand in a zip file containing:
- Header and implementation files for the setOper class
- The main program source code
#include<iostream>
using namespace std;
class setOper
{
public:
bool belong(double*a,double n) //check if n belongs in the interval
{
if(a[0]<=n && a[1]>=n)
return true;
return false;
}
bool unify(double*a,double*b,double n) //check if n belongs in union
{
double ret[2] = {0,0};
if(a[0]<b[0])
ret[0] = a[0];
else
ret[0] = b[0];
if(a[1]>b[1])
ret[1] = a[1];
else
ret[1] = b[1];
return belong(ret,n);
}
bool inter(double*a,double*b,double n) //check if n belongs in intersection
{
double ret[2] = {0,0};
if(a[0]>b[0])
ret[0] = a[0];
else
ret[0] = b[0];
if(a[1]<b[1])
ret[1] = a[1];
else
ret[1] = b[1];
return belong(ret,n);
}
};
int main()
{
setOper so;
double a[] = {-2,1};
double b[] = {-1,2};
double n = 1.5;
cout << so.belong(a,n);
cout << so.belong(b,n);
cout << so.unify(a,b,n);
cout << so.inter(a,b,n);
}