要求如下
//(1)定义Set类,编写Set类构造函数和析构函数,使它们完成打开文件读入元素到数组中和将数组中元素写入指定的磁盘文件中;
//(2)编写集合增加一个元素的成员函数insert(int x);(注:如果集合中已有相同的元素,则不再加入)
//(3)编写删除一个元素的成员函数erase(int x);
//(4)编写判别元素是否属于集合的成员函数isInSet(int x);
//(5)编写求两个集合交的函数成员intersection(Set &s);
//(6)编写求两个集合差的成员函数intersetion(Set &s);
//(7)重载+运算符,用于求两个集合的并;
//(8)编写集合排序的成员函数sort();(从大到小排序)
//(9)重载<<运算符,用于在屏幕上输出集合元素。
#include<bits/stdc++.h>
using namespace std;
class AbsCollection{
protected:
int *iPtr; //指向数组的指针
int last; //数组最后一个元素的位置
int maxSize; //数组总体长度
public:
AbsCollection(int size = 100){
maxSize = size;
iPtr = new int[maxSize];
last = -1;
};
virtual ~AbsCollection();
void OutputSet(); //输出集合中元素
};
void AbsCollection::OutputSet()
{
cout<<"The number of elements:"<<last+1<<endl;
for(int i=0;i<=last;i++)
{
cout<<iPtr[i]<<" ";
}
cout<<endl;
}
class Set:public AbsCollection
{
private:
char *fileName; //存放数据的文件
public:
Set(int size = 100, const char *szFileName="");//从文件读取数据存入数组(要判断是否有重复元素)
virtual ~Set(); //析构函数,释放内存并将数据写入文件
void insert(int x);
void erase(int x);
int isInSet(int x); //判断x是否已经存在于集合中
friend ostream& operator<<(ostream& os, Set& s);
void intersetion(Set &s);//求集合的交(*this与s的交,结果放在*this中)
void operator+(Set &s); //求集合的并(*this与s的并,结果放在*this中)
void diff(Set &s); //求集合的差(*this与s的差,结果放在*this中)
set& operator=(Set &s); //赋值拷贝函数
void sort(); //将集合中元素从大到小排序
};
int Set::isInSet(int x)
{
int flag = 0;
for(int i=0; i<last+1; i++)
{
if(x==iPtr[i])
flag = 0;
else
flag = 1;
}
return flag;
}
void Set::insert(int x)
{
if(!isInSet(x)){
if(last+1<maxSize){
iPtr[++last] = x;
}
else{
Set tempSet(maxSize+1);
for(int i = 0;i<= last;i++){
tempSet.iPtr[i] = iPtr[i];
tempSet.last++;
}
delete []iPtr;
last = -1;
iPtr = new int[++maxSize];
for(int i = 0;i<= tempSet.last;i++){
iPtr[i] = tempSet.iPtr[i];
last++;
}
last++;
iPtr[last] = x;
}
}
Set::Set(int size, const char *szFileName):AbsCollection(size)
{
if(szFileName =="")
{
fileName=Null;
return;
}
fileName = new char[strlen(szFileName)];
fstream fs;
fs.open(szFileName,ios::in);
if(!fs.isopen())
{
cout<<"open error!"<<endl;
abort();
}
int x;
while(fs>>x)
{
if(!isInSet(x))
{
insert(x);
}
}
fs.close();
}
Set::~Set()
{
if(fileName){
fstream fs(fileName,ios::out|ios::trunc)
for(int *ip=iPtr;ip<=iPtr+last;)
{
fs<<*ip++<<" ";
}
fs.close();
delete fileName;
}
if(iPtr)
{
delete []iPtr;
}
}
void Set::erase(int x)
{
for(int i = 0;i<=last;i++){
if(iPtr[i]==x){
break;
}
if(i<=last){
while(i!=last){
iPtr[i]=iPtr[i++];
}
}
}
last--;
}
ostream&operator<<(ostream& os, Set& s)
{
for(int i = 0;i<=last;i++)
{
os<<iPtr[i]<<" ";
}
return os;
}
void Set::intersetion(Set &s)
{
Set tempSet(maxSize);
for(int i = 0;i<=last;i++)
{
if(s.isInSet(iPtr[i]))
{
tempSet.insert(iPtr[i]);
}
}
*this = tempSet;
return *this;
}
void Set::operator+(Set &s){
for(int i=0;i<=s.last;i++){
if(!isInSet(s.iPtr[i])){
insert(s.iPtr[i]);
}
}
}
void Set::diff(Set &s)
{
Set tempSet(maxSize);
tempSet = *this;
for(int i=0;i<=s.last;i++)
{
if(isInSet(s.iPtr[i]))
{
tempSet.erase(s.iPtr[i]);
}
}
*this = tempSet;
}
Set& Set::operator=(Set &s)
{
if(iPtr) delete []iPtr;
maxSize = s.maxSize;
iPtr = new int[maxSize];
last = s.last;
for(int i=0;i<=last;i++)
{
iPtr[i]=s.iPtr[i];
}
return *this;
}
void Set::sort()
{
std::sort(iPtr,iPtr+last+1,great<int>());
}
int main()
{
Set set1(0,"f1.txt");
set1.insert(30);
set1.OutputSet();
}
谢谢大家!