作业帮 > 综合 > 作业

编程如何编写下列两道题的程序(用C++或VC++6.0)?1,:求两个集合的交集.2:求两个集合的笛卡尔乘积.

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/01 03:57:18
编程
如何编写下列两道题的程序(用C++或VC++6.0)?
1,:求两个集合的交集.
2:求两个集合的笛卡尔乘积.
我这里有求集合的交集并集程序,对你的第一个问题,把int 改为char,对于笛卡尔积,自己实现吧.
#include
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(){next=NULL;}
};
class jihe
{
Node *head1,*head2,*head3,*head4;
public:
jihe(){head1=new Node();head2=new Node();head3=new Node();head4=new Node();}
void creat(); //创建2个集合链表,并输入数据
void display(Node *head); //显示链表
void bing(); //求并集
void jiao(); //求交集
bool is_ni(Node *head); //判断集合是否有逆序
void paixu(Node *head); //对集合排序,由小到大的顺序
void work(); //组织
bool is_chongfu(Node *head);//判断集合是否有重复数据
void chongfu(Node *head); //删除重复数据
};
void jihe::creat()
{
Node *p,*s;
int m;
coutm;
if(m==0) //输入0结束输入
break;
s=new Node(); //输入一个数据就创建一个链表
s->data=m;
p->next=s;
p=p->next;
}
coutm;
if(m==0)
break;
s=new Node();
s->data=m;
p->next=s;
p=p->next;
}
}
void jihe::display(Node *head)
{
Node *p;
p=head->next;
while(p)
{
coutnext;
s->data=q->data;
q=q->next;
}
break;
}
if(q==NULL)
{
while(p)
{
s->next=new Node();
s=s->next;
s->data=p->data;
p=p->next;
}
break;
}
s->next=new Node();
s=s->next;
if(p->data>q->data)
{
s->data=q->data;
q=q->next;
continue;
}
if(p->datadata)
{
s->data=p->data;
p=p->next;
continue;
}
if(p->data==q->data)
{
s->data=p->data;
q=q->next;
p=p->next;
}
}
display(head3);
}
void jihe::jiao()
{
Node *p,*q,*s;
p=head1->next;
q=head2->next;
s=head4;
while(1)
{
if(p==NULL || q==NULL)
break;
if(p->data < q->data)
{
p=p->next;
continue;
}
if(p->data > q->data)
{
q=q->next;
continue;
}
if(p->data==q->data)
{
s->next=new Node();
s=s->next;
s->data=p->data;
p=p->next;
q=q->next;
continue;
}
}
display(head4);
}
bool jihe::is_ni(Node *head)
{
Node *p,*q;
p=head->next;
while(p)
{
q=p->next;
while(q)
{
if(p->data > q->data)
return true;
q=q->next;
}
p=p->next;
}
return false;
}
void jihe::paixu(Node *head)
{
Node *p,*q,*s,*m,*q1,*m1;int t;
p=head;
s=p->next;
m=s;
q=m->next;
while(s->next)
{
t=s->data;
while(q)
{
if(q->data < t)
{
t=q->data;
q1=q;
m1=m;
}
m=m->next;
q=q->next;
}
if(s->data != t)//把找到的最小的提前
{
m1->next=q1->next;
p->next=q1;
q1->next=s;
}
p=p->next;
s=p->next;
m=s;
q=m->next;
}
}
bool jihe::is_chongfu(Node *head)
{
Node *p,*q;
p=head->next;
//异常情况:次集合没有一个元素
if(p == NULL)
return false;
//
q=p->next;
while(p->next)
{
q=p->next;
while(q)
{
if(p->data == q->data )
return true;
q=q->next;
}
p=p->next;
}
return false;
}
void jihe::chongfu(Node *head)
{
Node *p,*q,*m;
p=head->next;
m=p;
while(p->next )
{
q=p->next;
while(q)
{
if(p->data == q->data )
{
m->next=q->next;
delete q;
q=m->next;
continue;
}
q=q->next;
m=m->next;
}
p=p->next;
m=p;
if(p==NULL)
break;
}
}
void jihe::work()
{
creat();
if(is_chongfu(head1))//有重复
chongfu(head1);
if(is_chongfu(head2))//有重复
chongfu(head2);
if(is_ni(head1)) //有逆序
paixu(head1);
if(is_ni(head2))
paixu(head2);
cout