- #include<iostream.h>
- const int max=100;
- class set
- {
- int list[max];
- int pc;
- public:
- set()
- {
- pc=0;
- cout<<"Constructor called!"<<endl;
- }
- set(int a[],int size)
- {
- if(size>=100)
- pc=100;
- else
- pc=size;
- for(int i=0;i<pc;i++)
- list[i]=a[i];
- }
- set(set& s)
- {
- pc=s.pc;
- for(int i=0;i<pc;i++)
- list[i]=s.list[i];
- cout<<"Copy-initialization Constructor called!"<<endl;
- }
- void empty(){pc=0;}
- bool isempty()
- {
- if(pc==0)
- return true;
- else
- return false;
- }
- bool ismemberof(int n)
- {
- for(int i=0;i<pc;i++)
- if(list[i]==n)
- return true;
- return false;
- }
- void add(int n)
- {
- if(ismemberof(n))
- {
- cout<<"The set has included the number!";
- return ;
- }
- else if(pc>max)
- {
- cout<<"The set is full.DO NOT add a number!";
- return;
- }
- else
- list[pc++]=n;
- }
- set intersection(set& SET)
- {
- int a[100],size=0;
- for(int i=0;i<pc;i++)
- for(int j=0;j<SET.pc;j++)
- if(list[i]==SET.list[j])
- {
- a[size++]=list[i];
- break;
- }
- return set(a,size);
- }
- void print()
- {
- cout<<"{";
- for(int i=0;i<pc-1;i++)
- cout<<list[i]<<",";
- if(pc>0)
- cout<<list[pc-1];
- cout<<"}"<<endl;
- }
- };
- void main()
- {
- set A;
- if(A.isempty())
- cout<<"set 'A' is empty."<<endl;
- else
- cout<<"set 'A' is not empty."<<endl;
- cout<<"A:";A.print();
- for(int i=1;i<10;i++)
- A.add(i);
- cout<<"A:";A.print();
- set B;
- for(i=1;i<6;i++)
- B.add(i);
- cout<<"B:";B.print();
- B.empty();
- cout<<"B:";B.print();
- for(i=8;i<15;i++)
- B.add(i);
- cout<<"B:";B.print();
- set C(B);
- cout<<"C:";C.print();
- cout<<"A交C:";A.intersection(C).print();
- }