logo资料库

粗糙集属性约减c++源代码.doc

第1页 / 共52页
第2页 / 共52页
第3页 / 共52页
第4页 / 共52页
第5页 / 共52页
第6页 / 共52页
第7页 / 共52页
第8页 / 共52页
资料共52页,剩余部分请下载后查看
Set.h #include #include #include using namespace std; #ifndef Set_H #define Set_H //定义适合本文的集合(Set)抽象数据类型-------------------------------------------- class Set { public: Set():vec(0){}; Set(int A[], int n); //用数组初始化集合 Set(const Set &s); //用集合初始化集合 Set(vector v); const Set& operator=(const Set& s); //集合赋值运算符重载 bool IsEmpty(); //判断集合是否为空集 int Card(); //计算集合 A 的模 void Clear(); //集合清空 int Find(int a); //在集合中查找元素 a,返回位置,不存在返回-1 int KeyAt(int i); //返回集合中 i 位置的 key void SetKey(int index, int Val); //设置 index 位置的值为 Val void Sort(); //对集合中的元素按从小到大排序 void Remove(int a); //在集合中删除元素 a 得到新的集合 void Add(int a); //在集合中添加元素 a 得到新的集合 bool BelongTo(Set &s); //判断集合 A 是否属于集合 B
bool operator==(Set &s); //判断两个集合是否相等 Set Sub(Set &s)const; //集合 A 减 B 计算 Set Union(Set &s)const; //集合 A 并 B 计算 Set InterSect(Set &s)const; //集合 A 交 B 的计算 Set ValueOfAttribute(); //求属性 a 值集的值域 vector ToIntVector(); //把该集合转化成整型向量 void Print(); //在屏幕打印集合 A private: vector vec; }; #endif Set.cpp #include"Set.h" //集合类 Set 的实现----------------------------------------------- Set::Set(int A[], int n) { for(int i=0; i
Set::Set(vector v) { vec = v; } int Set::Card() { return (int) vec.size(); } const Set& Set::operator=(const Set& s) { if(this != &s) { vec = s.vec; } return *this; } bool Set::IsEmpty() { return vec.empty(); } void Set::Clear() { vec.clear(); } int Set::Find(int a) { for(int i=0; i<(*this).Card(); i++)
{ if(a == vec[i]) return i; } return -1; } int Set::KeyAt(int i) { return vec[i]; } void Set::SetKey(int index, int Val) { vec[index] = Val; } void Set::Sort() { sort(vec.begin(), vec.end()); } void Set::Remove(int a) { if(-1 != Find(a)) { vector v; for(vector::iterator it=vec.begin(); it
v.push_back(*it); } } vec = v; } } void Set::Add(int a) { vec.push_back(a); } bool Set::BelongTo(Set &s) { for(int i=0; i<(int) vec.size(); i++) { if(-1 == s.Find(vec[i])) return false; } return true; } bool Set::operator ==(Set &s) { if(this->BelongTo(s) && s.BelongTo(*this)) { return true; } else { return false;
} } Set Set::Sub(Set &s)const { Set temp(vec); for(int i=0; i
for(int i=0; i<(int) vec.size(); i++) { if(-1 == s.Find(vec[i])) { temp.Remove(vec[i]); } } Set VOfa = temp.ValueOfAttribute(); return VOfa; } Set Set::ValueOfAttribute() { Set temp; for(int i=0; i
continue; } } return temp; } vector Set::ToIntVector() { vector temp; for(int i = 0; i < this->Card(); i++) { temp.push_back( this->KeyAt(i)); } return temp; } void Set::Print() { vector::iterator it=vec.begin(); cout<<"{"; if(!vec.empty()) { cout<<*it++; } for( ; it
分享到:
收藏