#include<iostream.h> #define size 40 class collect{ int length; int array[size]; public: collect(); collect(int n); void display(); void insert(int n); void deleted(int n); friend collect operator+( collect &d,collect &c); friend collect operator*(collect &d,collect &c); friend collect operator-(collect &d,collect &c); collect &operator=(collect &c);
}; collect::collect(){ length=0; array[size]; } collect::collect(int n){ int i; length=n; for(i=0;i<length;i++) {cout<<"enter the element:"; cin>>array[i]; } } collect operator+( collect &d,collect &c) { collect a; for(int i=0;i<d.length;i++) a.array[a.length++]=d.array[i]; for(int j=0;j<c.length;j++) { for(int k=0;k<d.length; k++) if(c.array[j]==d.array[k]) break; if(k==d.length) a.array[a.length++]=c.array[j]; } return a; } collect operator*( collect &d,collect &c) { collect a; int i,j; a.length=0; for(i=0;i<c.length;i++) for(j=0;j<d.length;j++) if(c.array[i]==d.array[j]) {a.array[a.length++]=d.array[j]; break; } return a; } collect operator-(collect &d, collect &c) { collect a,b; b=d; int i,j,k; for(j=0;j<c.length;j++) for(k=0;k<b.length;k++) if(c.array[j]==b.array[k]) { for(i=k;i<b.length;i++) b.array[i]=b.array[i+1]; b.array[i]=0; b.length--; break;} a=b; return a; } collect &collect::operator=( collect &c) { int i; length=c.length; for(i=0;i<c.length;i++) array[i]=c.array[i] ; return *this; } void collect::deleted(int n) { int j, i; for(i=0;i<length;i++) if(array[i]==n) { for(j=i;j<length;j++) array[j]=array[j+1]; array[j]=0; length--; break;} if(i==length) cout<<"no"<<endl; length; } void collect::insert(int n) { int i; for(i=0;i<length;i++) if(array[i]==n) break; if(i<length) cout<<"yes"<<endl; else array[length++]=n; } void collect::display() {cout<<"there have"<<" "<<length<<" "<<"elements" <<endl; int i; for(i=0;i<length;i++) { if(i%5==0) cout<<endl<<array[i]<<" "; } cout<<endl; } void main() { int a,b,c,d; cout<<"enter the length of collect1:"; cin>>a; while(a>size) {cout<<"enter the length of collect1:"; cin>>a;} cout<<endl; collect collect1(a) ; collect1.display(); cout<<"enter the length of collect2:"; cin>>b; while(b>size) {cout<<"enter the length of collect2:"; cin>>b;} cout<<endl; collect collect2(b); collect2.display(); collect collect5,collect3,collect4; cout<<"collect3=collect1-collect2"; cout<<endl; collect3=collect1-collect2; collect3.display(); cout<<"collect4=collect1+collect2"; cout<<endl; collect4=collect1+collect2; collect4.display(); cout<<"collect5=collect1*collect2"; cout<<endl; collect5=collect1*collect2; collect5.display(); cout<<"enter the insert element:"; cin>>c; collect1.insert(c); collect1.display(); cout<<"enter the delete element:"; cin>>d; collect1.deleted(d); collect1.display(); } |