|
发表于 2013-6-21 11:58:23
|
显示全部楼层
本帖最后由 熊文杰 于 2013-6-23 00:57 编辑
下面是把交集和补集改过之后的,顺便改成了c风格的 没有用到c++的引用,而是用的二级指针- #include <string.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define NULL 0
- typedef struct element//集合元素的结构体
- {
- char data;
- struct element *next;
- }element;
- element* init_element(element *head)//初始化集合(集合是用单链表存储结构存储的)
- {
- head=(element*)malloc(sizeof(struct element));
- head->next=NULL;
- return head;
- }
- void readdata_element(element **_head)//键盘输入每个集合的元素
- {
- static char szBuf[256] = {0};
- element *head = *_head;
-
- printf("请输入集合中的数据元素(以“F”为结束标志,结束输入):\n");
- scanf("%s",szBuf);
- int i = 0;
- while(szBuf[i] != 'F')
- {
-
- if((szBuf[i]>='a' && szBuf[i]<='z') ||( szBuf[i]>='0' && szBuf[i]<='9'))
- {
- element *p;
- p=(element*)malloc(sizeof(struct element));
- p->data=szBuf[i];
- p->next=head->next;
- head->next=p;
- }
- else
- {
- printf("输入错误!必须是小写字母或者0~9的数字!请重新输入:\n");
- break;
- }
- i++;
- }
- memset(szBuf,0,256);
- }
- void display_element(element *head)//输出集合中的所有元素
- {
- element *p;
- p=head->next;
- while(p!=NULL)
- {
- printf("%c",p->data);
- p=p->next;
- }
- printf("\n");
- }
- void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
- {
- element *p1,*p2,*p3;
- p1=head1->next;
- while(p1!=NULL)
- {
- p3=(element*)malloc(sizeof(struct element));
- 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=(element*)malloc(sizeof(struct element));
- p3->data=p2->data;
- p3->next=head3->next;
- head3->next=p3;
- }
- p2=p2->next;
- }
- }
- void jiao_element(element *head1,element *head2,element **head3)//求两个集合的交集
- {
- element *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=(element*)malloc(sizeof(struct element));
- p3->data=p1->data;
- p3->next=(*head3)->next;
- (*head3)->next=p3;
- }
- p1=p1->next;
- }
- }
- void cha_element(element *head1,element *head2,element **head3)//求两个集合的差集
- {
- element *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=(element*)malloc(sizeof(struct element));
- p3->data=p1->data;
- p3->next=(*head3)->next;
- (*head3)->next=p3;
- }
- p1=p1->next;
- }
- }
- //第一个参数是全集
- void bu_element(element *head1,element *head2,element **head3)//求集合的补集
- {
- element *p1,*p2,*p3;
- p1=head1->next;
-
- while(p1!=NULL)
- {
- p2=head2->next;
- while((p2!=NULL) && (p1->data!=p2->data))
- {
- p2=p2->next;
- }
- if(p2==NULL)
- {
- p3=(element*)malloc(sizeof(struct element));
- p3->data=p1->data;
- p3->next=(*head3)->next;
- (*head3)->next=p3;
- }
- p1=p1->next;
- }
- }
- int main()
- {
- element *head1,*head2,*head3,*head4;
- element *p = NULL;
- head1=init_element(p);
- head2=init_element(p);
- head3=init_element(p);
- head4=init_element(p);
- printf("请输入集合1:\n");
- readdata_element(&head1);
- printf("请输入集合2:\n");
- readdata_element(&head2);
- printf("请输入集合3(全集):\n");
- readdata_element(&head3);
- printf("集合1为:\n");
- display_element(head1);
- printf("集合2为:\n");
- display_element(head2);
- printf("集合3(全集)为:\n");
- display_element(head3);
- char c1 ;
- printf("运算目录\nA、集合1与集合2的并集\nB、集合1与集合2的交集\nC、集合1与集合2的差集\nD、集合1的补集\nE、集合2的补集\n");
- fflush(stdin);
- scanf("%c",&c1);
- if('A' == c1)
- {
- printf("集合1与集合2的并集为:\n");
- bing_element(head1,head2,head4);
- display_element(head4);
- head4->next=NULL;
- }
- else if('B' == c1)
- {
- printf("集合1与集合2的交集为:\n");
- jiao_element(head1,head2,&head4);
- display_element(head4);
- head4->next=NULL;
- }
- else if('C' == c1)
- {
- printf("集合1与集合2的差集为:\n");
- cha_element(head1,head2,&head4);
- display_element(head4);
- head4->next=NULL;
- }
- else if('D' == c1)
- {
- printf("集合1的补集为:\n");
- bu_element(head3,head1,&head4);
- display_element(head4);
- head4->next=NULL;
- }
- else if('E' == c1)
- {
- printf("集合2的补集为:\n");
- bu_element(head3,head2,&head4);
- display_element(head4);
- head4->next=NULL;
- }
- system("pause");
- return 0;
- }
复制代码 |
|