#include
#include
#include
using namespace std;
class Set
{
int maxsize;//集合的当前最大容量
int count;//集合的当前元素个数
int *elem; public: //Set(int initsize=10);//构造函数,创建一个空集,initsize: 集合的初始容量
Set(const Set &s);//拷贝构造函数
Set();//析构函数
int Add(int a[], int len);//增加一组新元素,返回值为新增加的元素个数
int Add(int e);//增加一个新元素,返回值为新增加的元素个数
bool Contains(int e) const;//检查当前集合中是否已包含元素 e
Set Intersect(const Set& s) const;//求与另一集合的交集
Set Union(const Set& s) const;//求与另一集合的并集
int GetCount() const;//获取当前集合中的元素个数
void Print() const;};//打印所有元素 };//注:const 表示该函数不会对当前对象的数据成员做出修改
int main()
{
int a[]={1,3,5,7,9}, b[]={2,4,6,8,10,3, 5};
Set s1, s2;
s1.Add(a, 5);
s2.Add(b, 7);
Set s3 = s1.Intersect(s2);
Set s4 = s1.Union(s2);
s3.Print();
s4.Print(); }
void Set::Print()const
{
for (int i = 0; i < count; i++)
cout << elem[i] << " ";
cout << endl; }
Set Set::Union(const Set& s) const
{ Set r(s); //用 s 来初始化新集合
r.Add(elem, count); //再添加入当前集合中包含的内容
return r; }
inline int Set::GetCount() const
{ return count; }
Set Set::Intersect(const Set& s) const
{
//int Set::count;
Set r(s.count + this->count);
for (int i = 0; i < s.count; i++)
{
if (Contains(s.elem[i])) r.Add(s.elem[i]); //两个集合中都有的元素,加入到交集中。
}
return r; }
Set::Set(const Set& s) { maxsize = s.maxsize;
count = s.count;
elem = new int[maxsize]; //为新元素分配空间
memcpy(elem, s.elem, sizeof(int)*count); //复制所有元素
}
Set::Set() { delete[] elem; //释放占用空间
}
Set::Set(int initsize) :maxsize(initsize), count(0)
{ elem = new int [maxsize];
if (!elem) throw "申请集合空间失败!"; }
int Set::Add(int a[], int len)
{ int c = 0; //用于记录新增加的元素个数
for (int i = 0; i < len; i++)
c+= Add(a[i]);
return c; }
int Set::Add(int e)
{ if (Contains(e)) return 0; //当前集合中已包含了指定元素,不用添加了。
if (count == maxsize) //空间已满,再增加空间
{ int *tmp = new int[maxsize+10];
if (!tmp)
return 0; //申请新空间失败,退出
memcpy(tmp, elem, count*sizeof(int)); //将原来的内容复制到新空间中
maxsize += 10; //最大容量增长10个数据单位
delete[] elem; //删除原有空间
elem = tmp;
} elem[count++] = e; //将新元素存放在数组的最后
return 1; }
bool Set::Contains(int e) const
{ for (int i = 0; i < count; i++)
if (elem[i] == e) return true;
return false;}
这段代码有两三个错误 代码是基础的c++ 运行环境为CB或者devc++麻烦帮我改一下~
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- shen_wei 2017-03-30 10:20关注
class Set { int maxsize;//集合的当前最大容量 int count;//集合的当前元素个数 int *elem; public: //Set(int initsize=10);//构造函数,创建一个空集,initsize: 集合的初始容量 Set(const Set &s);//拷贝构造函数 Set();//析构函数 Set(int initsize); int Add(int a[], int len);//增加一组新元素,返回值为新增加的元素个数 int Add(int e);//增加一个新元素,返回值为新增加的元素个数 bool Contains(int e) const;//检查当前集合中是否已包含元素 e Set Intersect(const Set& s) const;//求与另一集合的交集 Set Union(const Set& s) const;//求与另一集合的并集 int GetCount() const;//获取当前集合中的元素个数 void Print() const; };//打印所有元素 };//注:const 表示该函数不会对当前对象的数据成员做出修改 int main() { int a[]={1,3,5,7,9}, b[]={2,4,6,8,10,3, 5}; Set s1, s2; s1.Add(a, 5); s2.Add(b, 7); Set s3 = s1.Intersect(s2); Set s4 = s1.Union(s2); s3.Print(); s4.Print(); } void Set::Print()const { for (int i = 0; i < count; i++) cout << elem[i] << " "; cout << endl; } Set Set::Union(const Set& s) const { Set r(s); //用 s 来初始化新集合 r.Add(elem, count); //再添加入当前集合中包含的内容 return r; } inline int Set::GetCount() const { return count; } Set Set::Intersect(const Set& s) const { //int Set::count; Set r; r.count = s.count + this->count; for (int i = 0; i < s.count; i++) { if (Contains(s.elem[i])) r.Add(s.elem[i]); //两个集合中都有的元素,加入到交集中。 } return r; } Set::Set(const Set& s) { maxsize = s.maxsize; count = s.count; elem = new int[maxsize]; //为新元素分配空间 memcpy(elem, s.elem, sizeof(int)*count); //复制所有元素 } Set::Set() { delete[] elem; //释放占用空间 } Set::Set(int initsize) :maxsize(initsize), count(0) { elem = new int [maxsize]; if (!elem) throw "申请集合空间失败!"; } int Set::Add(int a[], int len) { int c = 0; //用于记录新增加的元素个数 for (int i = 0; i < len; i++) c+= Add(a[i]); return c; } int Set::Add(int e) { if (Contains(e)) return 0; //当前集合中已包含了指定元素,不用添加了。 if (count == maxsize) //空间已满,再增加空间 { int *tmp = new int[maxsize+10]; if (!tmp) return 0; //申请新空间失败,退出 memcpy(tmp, elem, count*sizeof(int)); //将原来的内容复制到新空间中 maxsize += 10; //最大容量增长10个数据单位 delete[] elem; //删除原有空间 elem = tmp; } elem[count++] = e; //将新元素存放在数组的最后 return 1; } bool Set::Contains(int e) const { for (int i = 0; i < count; i++) if (elem[i] == e) return true; return false; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
- ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
- ¥15 perl MISA分析p3_in脚本出错
- ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
- ¥15 ubuntu虚拟机打包apk错误
- ¥199 rust编程架构设计的方案 有偿
- ¥15 回答4f系统的像差计算