C++集合类 数组实现
//但是也蛮有趣味的,只是在下才疏学浅,运行时有时会出现错误
//注意:是有时会出错,程序不稳定
//望大虾们给予赐教,小弟不甚感激!
//原创:安徽大学 07计算机学院
#include<iostream.h>
#define Max 10
int i=0,j=0;
class Set
{
private:
int elems[Max];
int pc;
public:
Set(){pc=0;elems[0]='\0';}
Set(Set &s); // 对象引用作为参数
void set();
int size(Set S);
void isfull(Set S);
void isempty(Set S);
int ismemberof(int n);
int add(int n);
int delet(int n);
Set bingji(Set A,Set B);
Set jiaoji(Set A,Set B);
void display();
};
void Set::set()//初始化
{
int n,m,flag=0;
cout<<"数组元素的个数(N<=10):";
cin>>n;
cout<<"输入数组元素:";
for(i=0;i<n;i++)
{cin>>m;
for(j=0;j<pc;j++)
if(m==elems[j]) {flag=1;break;}
if(!flag)
{
elems[i]=m;
pc++;}
}
}
int Set::size(Set S)
{
return S.pc;}
void Set::isempty(Set S)//判断空
{if(!size(S))
cout<<"Empty!";
else cout<<"Not Empty!";}
void Set::isfull(Set S)//判断满
{if(size(S)==Max)
cout<<"Full!";
else cout<<"Not Full!";}
int Set::ismemberof(int n) //判断是否为其元素
{
for(int i=0;i<pc;i++)
if(elems[i]==n)
return 1;
return 0;
}
int Set::add(int n) //增加元素
{
if(ismemberof(n))
return 1;
else if(pc>Max)
return 0;
else
{
elems[pc++]=n;
return 1;
}
}
int Set::delet(int n)//删除元素
{
if(ismemberof(n))
{for(i=0;i<pc;i++)
if(elems[i]==n)
{pc--;
for(j=i;j<pc;i++)
elems[j]=elems[j+1];
}
return 1;
}
else return 0;
}
Set::Set(Set &p) //复制构造函数
{
pc=p.pc;
for(int i=0;i<pc;i++)
elems[i]=p.elems[i];
}
Set Set::bingji(Set A,Set B)
{
Set C(A);
for(i=0;i<B.pc;i++)
for(j=0;j<C.pc;j++)
if(B.elems[i]!=C.elems[j]) C.add(B.elems[i]);
return C;
}
Set Set::jiaoji(Set A,Set B)
{
Set C;
for(i=0;i<B.pc;i++)
for(j=0;j<A.pc;j++)
if(B.elems[i]==A.elems[j]) C.add(B.elems[i]);
return C;
}
void Set::display ()//
{
if(pc<1) cout<<"ф"<<endl;
else
{
cout<<"{";
for(i=0;i<pc-1;i++)
cout<<elems[i]<<",";
cout<<elems[i]<<"}";
cout<<endl;
}
}
int main()
{
int n;
Set A,B,C;
cout<<"A";
A.set();
cout<<"B";
B.set();
cout<<"A=";
A.display();
cout<<"B=";
B.display();
cout<<"A∩B=";
C=A.jiaoji(A,B);
C.display();
cout<<"A∪B=";
C=A.bingji(A,B);
C.display();
cout<<"从A中选择要删除的元素:";
cin>>n;
A.delet(n);
cout<<"A=";
A.display();
cout<<"从B中选择要删除的元素:";
cin>>n;
B.delet(n);
cout<<"B=";
B.display();
cout<<"输入要增加到集合A中的元素:";
cin>>n;
if(!A.add(n)) cout<<"增加失败,集合A已满!";
else
{cout<<"A=";
A.display();
}
cout<<"输入要增加到集合B中的元素:";
cin>>n;
if(!B.add(n)) cout<<"增加失败,集合B已满!";
else
{cout<<"B=";
B.display();
}
return 0;
} 展开
//看看符合你要求不?
#include <iostream>
using namespace std;
const int Max=100;
//------------------------------------------------------
class Set
{
unsigned int len;
int Elem[Max];
public:
//Constructor
Set();
Set(const Set & SetObj);
inline bool isFull() const;
inline bool isEmpty() const;
inline unsigned int size() const;
inline int isMemberOf(int nElem) const;//返回位置
bool addElem(int nElem);
bool delElem(int nElem);
Set Union(const Set & SetB);
Set InSection(const Set & SetB);
void display() const;
void Input();
};
//------------------------------------------------------
Set::Set():len(0){}
//------------------------------------------------------
Set::Set(const Set & SetObj)
{
for (len = 0;len < SetObj.len;len++)
{
Elem[len] = SetObj.Elem[len];
}
}
//------------------------------------------------------
unsigned int Set::size() const
{
return len;
}
//------------------------------------------------------
bool Set::isEmpty() const
{
return len == 0;
}
//------------------------------------------------------
bool Set::isFull() const
{
return len == Max;
}
//------------------------------------------------------
int Set::isMemberOf(int nElem) const
{
for (int index = 0; index < len; index++)
{
if (Elem[index] == nElem) return index + 1;
}
return -1;
}
//-------------------------------------------------------
void Set::display() const
{
for (int index = 0; index < len;index++)
cout<<Elem[index]<<" ";
cout<<endl;
}
//-------------------------------------------------------
//添加和删除元素操作
//-------------------------------------------------------
bool Set::addElem(int nElem)
{
if (isFull()) return false;
else if (isMemberOf(nElem)!= -1){}//Do nothing
else
{
Elem[len++] = nElem;
}
return true;
}
//--------------------------------------------------------
bool Set::delElem(int nElem)
{
int loc = isMemberOf(nElem);
if (isEmpty()||(loc == -1)) return false;
for (int index = loc; index < len; index++)
{
Elem[index - 1] = Elem[index];
}
len--;
return true;
}
//--------------------------------------------------------
//求交集(InSection)与并集(Union)
//--------------------------------------------------------
Set Set::Union(const Set & SetB)
{
//注意:没有考虑溢出的情况
Set SetC(SetB);
for (int index = 0;index < len;index++)
SetC.addElem(Elem[index]);
return SetC;
}
//------------------------------------------------------------
Set Set::InSection(const Set & SetB)
{
Set SetC;
for (int index = 0;index < len;index++)
{
for (int indexB = 0;indexB < SetB.len;indexB++)
{
if (Elem[index] == SetB.Elem[indexB])
SetC.addElem(Elem[index]);
}
}
return SetC;
}
//--------------------------------------------------------------
void Set::Input()
{
int nSize;
cout<<"请输入集合的大小:\n";
cin>>nSize;
if (nSize > Max) return;
cout<<"请输入集合元素!\n";
for (int i = 0,elem; i < nSize ;i++)
{
cin>>elem;
addElem(elem);
}
}
//---------------------------------------------------------------
int main()
{
int n;
Set A,B,C;
cout<<"A";
A.Input();
cout<<"B";
B.Input();
cout<<"A=";
A.display();
cout<<"B=";
B.display();
cout<<"A∩B=";
C=A.InSection(B);
C.display();
cout<<"A∪B=";
C=A.Union(B);
C.display();
cout<<"从A中选择要删除的元素:";
cin>>n;
A.delElem(n);
cout<<"A=";
A.display();
cout<<"从B中选择要删除的元素:";
cin>>n;
B.delElem(n);
cout<<"B=";
B.display();
cout<<"输入要增加到集合A中的元素:";
cin>>n;
if (!A.addElem(n)) cout<<"增加失败,集合A已满!";
else
{
cout<<"A=";
A.display();
}
cout<<"输入要增加到集合B中的元素:";
cin>>n;
if (!B.addElem(n)) cout<<"增加失败,集合B已满!";
else
{
cout<<"B=";
B.display();
}
return 0;
}
//---------------------------------------------------------------