请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的IntSet是一个用于表示正整数集合的类。IntSet的成员函数Merge的功能是求当前集合与另一个集合的并集,在Merge中可以使用成员函数IsMemberOf判断_个正

admin2019-07-10  22

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的IntSet是一个用于表示正整数集合的类。IntSet的成员函数Merge的功能是求当前集合与另一个集合的并集,在Merge中可以使用成员函数IsMemberOf判断_个正整数是否在集合中。请完成成员函数Mellge。在main函数中给出了一组测试数据,此时程序的输出应该是:
求并集前:
1235810
28911305667
求并集后:
1235810
28911305667
1235810911305667
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include
usingnamespacestd;
constintMax=100;
classIntSet
{
public:
IntSet()//构造一个空集合
{
end=-1;
}
IntSet(inta[],intsize)
//构造一个包含数组a中size个元素的集合
{
if(Size>=Max)
end=Max-;
else
end=size-1;
for(inti=0;i<=end;i++)
element=a
}
boolIsMemberOf(inta)
//判断a是否为集合中的元素
{
for(inti=0;i<=end;i++)
if(element:=a)
returntrue;
returnfalse;
}
intGetEnd(){returnend;)
//返回最后一个元素的下标
intGetElement(inti){returnelement;}
//返回下标i处的元素
IntSetMerge(IntSet&set);
//求当前集合与集合set的并集
voidPrint()
//输出集合中的所有元素
{
for(inti=0;i<=end;i++)
if((i+1)%20=0)
cout<<else
cout<<<’,’;
cout<}
private:
intelement[Max];
intend;
};
voidwriteToFile(constchar*);
//main.cpp
#include"IntSet.h"
IntSetIntSet::Merge(IntSet&set)
{
inta[Max],size=0;
//********333********
//********666********
returnIntSet(a,size);
}
intmain()
{
inta[]={1,2,3,5,8,i0};
intb[]={2,8,9,ii,30,56,67};
IntSetsetl(a,6),set2(b,7),set3;
cout<<"求并集前:"<set1.Print();
set2.Print();
set3.Print();
set3=set1.Merge(set2);
cout<set1.Print();
set2.Print();
set3.Print();
writeToFile("");
return0;
}

选项

答案for (int i = 0; i <= end; i++) { a[i] = element[i]; size++; } for (int k = 0; k <= set.GetEnd(); k++) if (!IsMemberOf(set.GetElement(k))) a[size++] = set.GetElement(k);

解析 主要考查考生对数组的掌握情况,题目要求计算集合的并集,定义一个新集合a,先复制一个数组的元素,再判断另一个数组中的元素,只要元素不重复就添加到集合a中。
转载请注明原文地址:https://kaotiyun.com/show/bP8p777K
0

最新回复(0)