`/** 选择题目一: 结合离散数学中集合的性质于运算,设计集合类(元素的类型自行定义),并实现集合的基本运算, 包括: …… **/
#ifndef UFSETS_H_ #define UFSETS_H_ #include #include #include #include #include
using namespace std;
/**
UFSets类的设计思路: 用STL的容器vector以及algorithm库的算法来进行设计
/ class UFSets { private: /
用vector的原因: 1.vector自带智能指针,不需要进行特殊的删除,所以可以用默认析构函数 2.vector本身自带了许多检测函数比较方便
**/ vector data;
public: UFSets(); //构造函数,用来在空集上确定具体的元素 ~UFSets(); //析构函数,这里用默认构造函数
bool isEmpty(); //确定是否为空集
bool isContain(int element) const; //确定是否包含某一个元素
bool isEquality( const UFSets& anotherSets); //判断两个集合是否相等
bool isSubSets(const UFSets& anotherSets); //判断是否为子集
void add(int element); //只是单纯的增加一个元素
void addElement(int element[], int length); //增加一组元素
void del(int element); //删除一个元素
UFSets unionSet(const UFSets& anotherSets); //并集操作
UFSets intersectSet(const UFSets& anotherSets); //交集操作
UFSets differenceSet(const UFSets& anotherSets); //差集操作
UFSets symDif(const UFSets& anotherSets); //全名:Symmetric difference,对称差运算
int getCount() const; //返回元素的个数
void Print() const; //打印当前的元素
};
#endif
//首先将vector容器设置为空 UFSets::UFSets() { data = { }; }
UFSets::~UFSets() { }
bool UFSets::isEmpty() { //bool std::vector::empty() const 如果为空返回true,不为空为false if (data.empty() == true) return true; else return false; }
bool UFSets::isContain(int element) const { //find函数用迭代器找元素,找的到就返回他在vector的位置,找不到就算球 if (find(data.begin(), data.end(), element) != data.end()) return true; else return false; }
//其实该函数可以用vector自带的重载运算符==,来进行判断,但是打完才想起来算了 bool UFSets::isEquality(const UFSets& anotherSets) { if (data.size() != anotherSets.data.size()) { //连长度都不相等,肯定不相等 return false; }
for (int i = 0; i < data.size(); i++) {
for(int j = 0;j < anotherSets.data.size();j++) //一个一个元素进行比较
if (data[i] != anotherSets.data[j])
return false;
}
return true;
}
//algorithm函数库中的includes函数来判断子集 bool UFSets::isSubSets(const UFSets& anotherSets) { return includes(data.begin(), data.end(), anotherSets.data.begin(), anotherSets.data.end()); }
//vector语法 void UFSets::add(int element) { data.push_back(element); //push_back函数为容器后面添加元素 }
//vector语法 void UFSets::addElement(int element[], int length) { for (int i = 0; i < length; i++) add(element[i]); //一个一个往里面添加
sort(data.begin(),data.end());
}
void UFSets::del(int element) { if (isContain(element) == false) { cout << "没有这个元素删除个蛇皮" << endl; //都不在删除个蛇蛇皮 return ; }
auto location = find(data.begin(), data.end(), element); //找到特定元素,然后直接删除
data.erase(location);
}
/** 交、差、并、对称差运算都是用到了algorithm函数库中的函数 这四个函数的语法都是通用的:
以set_union为例: set_union(data.begin(),data.end(),anotherSets.data.begin(),anotherSets.data.end(), inserter(result.data,result.data.begin()));
前面四个参数都是vector的迭代器,最后一个参数是输出,那么这里在输出那里用inserter函数,将输出的 结果直接搞到vector容器中 **/ UFSets UFSets::unionSet(const UFSets& anotherSets) { UFSets result; set_union(data.begin(),data.end(),anotherSets.data.begin(),anotherSets.data.end(), inserter(result.data,result.data.begin())); return result; }
UFSets UFSets::intersectSet(const UFSets& anotherSets) { UFSets result; set_intersection(data.begin(), data.end(), anotherSets.data.begin(), anotherSets.data.end(), inserter(result.data, result.data.begin())); return result; }
UFSets UFSets::differenceSet(const UFSets& anotherSets) { UFSets result; set_difference(data.begin(), data.end(), anotherSets.data.begin(), anotherSets.data.end(), inserter(result.data, result.data.begin())); return result; }
UFSets UFSets::symDif(const UFSets& anotherSets) { UFSets result; set_symmetric_difference(data.begin(), data.end(), anotherSets.data.begin(), anotherSets.data.end(), inserter(result.data, result.data.begin())); return result; }
//元素的个数直接用vector的函数方法 int UFSets::getCount() const { return data.size(); }
//打印元素 void UFSets::Print() const { cout << "当前集合的元素" << endl; for (int i = 0; i < data.size(); i++) { cout << data[i] << " "<<endl; } cout << "完成" << endl; }
/**
测试用例: set1 = {1 3 5 7 9} set2 = {2 4 6 7 9 10}
set2在删除元素2后的预测结果: set2 = {4 6 7 9 10}
用set1进行属性测试函数的验证:
预期结果:
是否为空集 不是
是否包含元素1 是
是否与set2相等 不是
set1是否为set2的子集 不是
用set1和删除元素2以后的set2进行并,交,差,对称差运算的结果预测: 并集 1 3 4 5 6 7 9 10 交集 7 9 差集 1 3 5 对称差运算 1 3 4 5 6 10
**/
int main() { int setOne[] = { 1,3,5,7,9 }; int setTwo[] = { 2,4,6,7,9,10 };
UFSets set1, set2;
set1.addElement(setOne, 5); //增加元素
set2.addElement(setTwo, 6); //增加元素
//打印这两个东西
cout<<"集合1的元素:"<<endl;
set1.Print();
cout<<endl;
cout<<"集合2的元素:"<<endl;
set2.Print();
cout<<endl;
//把2删除试一下
cout<<"集合2中删除2后:"<<endl;
set2.del(2);
set2.Print();
cout<<endl;
//并,交,差,对称差运算
cout << "并集:" << endl;
UFSets UnionSet = set1.unionSet(set2);
UnionSet.Print();
cout<<endl;
cout << "交集:" << endl;
UFSets IntersectSet = set1.intersectSet(set2);
IntersectSet.Print();
cout<<endl;
cout << "差集:" << endl;
UFSets DifferenceSet = set1.differenceSet(set2);
DifferenceSet.Print();
cout<<endl;
cout << "对称差运算:" << endl;
UFSets symdif = set1.symDif(set2);
symdif.Print();
cout<<endl;
//返回set1的元素个数
cout << "接下来都是set1的验证:" << endl;
cout << "集合1的个数:"<< set1.getCount() << endl;
cout << "是否为空集:" << " "<<set1.isEmpty() << endl;
cout << "是否包含元素:" <<" " <<set1.isContain(1) << endl;
cout << "是否相等:" <<" "<< set1.isEquality(set2) << endl;
cout << "set1是否为set2的子集:" <<" "<< set1.isSubSets(set2) << endl;
return 0;
} `
运行结果: