c学习指南吧 关注:731贴子:1,800
  • 0回复贴,共1

求两个集合的交集

取消只看楼主收藏回复

下面的程序中主要是inter函数出现问题
#include <iostream>
using namespace std;
const int max=1024;
class set
{
private:
int size;
int a[max];
static int count;
int n[100];
int num;
int find (int x);
public:
set()
{
num=-1;
size=0;
}
void inter (set &x,set &y);
};
int set::count=0;
void set::inter (set &x,set &y)
{
for (int i=0;i<x.size;i++)
{
int t=0;
for(int j=0;i<y.size;j++)
{
if (x.a[i]==y.a[j])
{
t=1
}
}
if (t==1)
{
num++;
n[num]=x.a[i];
}
}
for (i=0;i<num;i++)
cout<<n[i]<<" ";
cout<<endl;
}
int main ()
{
set t,p;
for (int i=0;i<11;i++)
{
t.insert (i+1);
}
cout<<"The array1:"<<endl;
t.display ();
for (int j=0;j<5;j++)
{
p.insert (j);
}
cout<<"The array2:"<<endl;
p.display ();
set ter;
cout<<"The intersection of set A and set B"<<endl;
ter.inter (t,p);
cout<<"The array after deleteing"<<endl;
t.display ();
return 0;
}


IP属地:北京1楼2015-04-23 22:05回复