#include
using namespace std;
class DATA
{
public:
DATA(int t[ ], int n1) ; // 构造函数,用n1初始化n,并根据n动态生成数组a,用t数组对a数组初始化。
int belong(int a[ ], int n, int x) ; //判断x的值是否在数组a中,如果在返回1,否则返回0。
void fun( ) ; //判断求余运算%对本对象是否封闭,如果封闭,输出“封闭”。如果不封闭,则输出“不封闭”,同时输出第一个不满足条件的a[i]和a[j]。
void print( ) ; //输出成员数据的值。
~DATA() ; //析构函数,完成必要的功能。
private:
int *a ; //整数指针,指向动态分配的数组空间
int n ; //数组中元素个数
};
DATA::DATA(int t[],int n1)
{
n=n1;
a=new int [n1];
if(a)
{
memcpy(a,t,n1*sizeof(int));
}
}
int DATA::belong(int a[],int n,int x)
{
for(int i=0;i
return 0;
}
void DATA::fun()
{
if(a)
{
int i,j,b1,b2,c,r;
for(i=0;i
for(j=i+1;j
b1=b2=-1;
c=0; r=0;
if(a[j]!=0)
{
b1=a[i]%a[j];
c++;
}
if(a[i]!=0)
{
b2=a[j]%a[i];
c++;
}
if(c==2)
{
if(!(belong(a,n,b1)&&belong(a,n,b2)))
{
cout<<"不封闭"<
}
}
if(c==1)
{
if(b1==-1)
{
r=belong(a,n,b2);
}
else
r=belong(a,n,b1);
if(!r)
{
cout<<"不封闭"<
}
}
}
}
}
cout<<"封闭"<
void DATA::print()
{
for(int i=0;i
if(i==0)
cout< else
cout<<'\t'< }
cout<
DATA::~DATA()
{
if(a)
{
n=0;
delete []a;
a=NULL;
}
}
int main()
{
int d1[9]={1,3, 22, 4, 15, 2, 7, 5, 0};
int d2[8]={1, 3, 8, 4, 6, 7, 5, 0};
DATA test1(d1,9);
DATA test2(d2,8);
test1.print();
test1.fun();
test2.print();
test2.fun();
return 0;
}