#include<stdio.h>
#include<stdlib.h>
typedef struct LNode//定义结构体类型指针
{
char data;
struct LNode*next;
}*pointer;
void readdata(pointer head)//定义输入集合函数
{
pointer p;
char tmp;
scanf("%c",&tmp);
while(tmp!='\n')
{
p=(pointer)malloc(sizeof(struct LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
void pop(pointer head)//定义输出集合函数
{
pointer p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void and(pointer head1,pointer head2,pointer head3)//定义集合的并集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next;
}
p2=head2->next;
while(p2!=NULL)
{
p1=head1->next;
while((p1!=NULL)&&(p1->data!=p2->data))
p1=p1->next;
if (p1==NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3;
}
p2=p2->next;
}
}
void or(pointer head1,pointer head2,pointer head3)//定义集合的交集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if((p2!=NULL)&&(p2->data==p1->data))
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void differ(pointer head1,pointer head2,pointer head3)//定义集合的差集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if(p2==NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据)\n");
pointer head1,head2,head3;
head1=(pointer)malloc(sizeof(struct LNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(struct LNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(struct LNode));
head3->next=NULL;
printf("请输入集合1:\n");
readdata(head1);//调用输入集合函数
printf("请输入集合2:\n");
readdata(head2);//调用输入集合函数
A:printf("1.并集 2.交集 3.差集 4.结束 x.重新运算\n");
do{
printf("请选择序号\n");
scanf("%d",&x);
system("cls");
switch(x)
{
case 1:
printf("两集合的并是\n");
and(head1,head2,head3);//调用并集函数
pop(head3);
head3->next=NULL;
break;
case 2:
printf("两集合的交是\n");
or(head1,head2,head3);//调用交集函数
pop(head3);
head3->next=NULL;
break;
case 3:
printf("两集合的差是\n");
differ(head1,head2,head3);//调用差集函数
pop(head3);
head3->next=NULL;
break;
case 4:break;
default:goto A;
}
}while(x!=4);
}
#include<stdlib.h>
typedef struct LNode//定义结构体类型指针
{
char data;
struct LNode*next;
}*pointer;
void readdata(pointer head)//定义输入集合函数
{
pointer p;
char tmp;
scanf("%c",&tmp);
while(tmp!='\n')
{
p=(pointer)malloc(sizeof(struct LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
void pop(pointer head)//定义输出集合函数
{
pointer p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void and(pointer head1,pointer head2,pointer head3)//定义集合的并集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next;
}
p2=head2->next;
while(p2!=NULL)
{
p1=head1->next;
while((p1!=NULL)&&(p1->data!=p2->data))
p1=p1->next;
if (p1==NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3;
}
p2=p2->next;
}
}
void or(pointer head1,pointer head2,pointer head3)//定义集合的交集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if((p2!=NULL)&&(p2->data==p1->data))
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void differ(pointer head1,pointer head2,pointer head3)//定义集合的差集函数
{
pointer p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if(p2==NULL)
{
p3=(pointer)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据)\n");
pointer head1,head2,head3;
head1=(pointer)malloc(sizeof(struct LNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(struct LNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(struct LNode));
head3->next=NULL;
printf("请输入集合1:\n");
readdata(head1);//调用输入集合函数
printf("请输入集合2:\n");
readdata(head2);//调用输入集合函数
A:printf("1.并集 2.交集 3.差集 4.结束 x.重新运算\n");
do{
printf("请选择序号\n");
scanf("%d",&x);
system("cls");
switch(x)
{
case 1:
printf("两集合的并是\n");
and(head1,head2,head3);//调用并集函数
pop(head3);
head3->next=NULL;
break;
case 2:
printf("两集合的交是\n");
or(head1,head2,head3);//调用交集函数
pop(head3);
head3->next=NULL;
break;
case 3:
printf("两集合的差是\n");
differ(head1,head2,head3);//调用差集函数
pop(head3);
head3->next=NULL;
break;
case 4:break;
default:goto A;
}
}while(x!=4);
}