高分求C语言数据结构c语言程序问题 请高手改8个代码,本人菜鸟,要求写出算法的思路、算法及程序

只需一步,快速开始
后使用快捷导航没有帐号?
查看: 1329|回复: 11
一个题目要求用C语言实现 求两个集合的交并补差集的运算 我的源代码有一点
签到天数: 90 天[LV.6]常住居民II我最爱的应用:
本帖最后由 权志龙_爱死你la 于
12:00 编辑
数据结构&&一个题目要求用C语言实现 求两个集合的交并补差集的运算 我的源代码有一点问题&&不能运行出来&&我感觉有问题的部分是void readdata_element(element *head)//键盘输入每个集合的元素&&这个函数&&求大家帮忙看看 谢谢
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
struct element *
}
element init_element(element *head)//初始化集合(集合是用单链表存储结构存储的)
{
head=(element*)malloc(sizeof(struct element));
head-&next=NULL;
return *
}
void readdata_element(element *head)//键盘输入每个集合的元素
{
printf(&请输入集合中的数据元素(以“F”为结束标志,结束输入):\n&);
scanf(&%c&,&c);
while(c!='F')
{
if((c&'a' && c&'z') ||( c&'0' && c&'9'))
{
element *p;
p=(element*)malloc(sizeof(struct element));
p-&data=c;
p-&next=head-&
head-&next=p;
scanf(&%c&,&c);
}
else
printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
}
}
void display_element(element * head)//输出集合中的所有元素
{
element *p;
p=head-&
while(p!=NULL)
{
printf(&%c&,p-&data);
p=p-&
}
printf(&\n&);
}
void bing_element(element *head1,element * head2,element * head3)//求两个集合的并集
{
element *p1,*p2,*p3;
p1=head1-&
while(p1!=NULL)
{
p3=(element*)malloc(sizeof(struct element));
p3-&data=p1-&
p3-&next=head3-&
head3-&next=p3;
p1=p1-&
}
p2=head2-&
while(p2!=NULL)
{
p1=head1-&
while((p1!=NULL) && (p1-&data!=p2-&data))
p1=p1-&
if(p1==NULL)
{
p3=(element*)malloc(sizeof(struct element));
p3-&data=p2-&
p3-&next=head3-&
head3-&next=p3;
}
p2=p2-&
}
}
void jiao_element(element *head1,element *head2,element *head3)//求两个集合的交集
{
element *p1,*p2,*p3;
p1=head1-&
while(p1!=NULL)
{
p2=head2-&
while((p2!=NULL) && (p2-&data!=p1-&data))
p2=p2-&
if(p2-&data==p1-&data)
{
p3=(element*)malloc(sizeof(struct element));
p3-&data=p1-&
p3-&next=head3-&
head3-&next=p3;
}
p1=p1-&
}
}
void cha_element(element *head1,element *head2,element *head3)//求两个集合的差集
{
element *p1,*p2,*p3;
p1=head1-&
while(p1!=NULL)
{
p2=head2-&
while((p2!=NULL) && (p2-&data!=p1-&data))
p2=p2-&
if(p2==NULL)
{
p3=(element*)malloc(sizeof(struct element));
p3-&data=p1-&
p3-&next=head3-&
head3-&next=p3;
}
p1=p1-&
}
}
void bu_element(element *head1,element *head2,element *head3)//求集合的补集
{
element *p1,*p2,*p3;
p1=head1-&
p2=head2-&
while(p1!=NULL)
{
while((p2!=NULL) && (p1-&data!=p2-&data))
{
p2=p2-&
}
if(p2==NULL)
{
p3-&data=p1-&
p3-&next=head3-&
head3-&next=p3;
p3=head3-&
}
p1=p1-&
}
}
void main()
{
element *head1,*head2,*head3,*head4;
init_element(head1);
init_element(head2);
init_element(head3);
init_element(head4);
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&);
scanf(&%c&,&c1);
if(c1=='A')
{
printf(&集合1与集合2的并集为:\n&);
bing_element(head1,head2,head4);
display_element(head4);
head4-&next=NULL;
}
if(c1=='B')
{
printf(&集合1与集合2的交集为:\n&);
bing_element(head1,head2,head4);
display_element(head4);
head4-&next=NULL;
}
if(c1=='C')
{
printf(&集合1与集合2的差集为:\n&);
bing_element(head1,head2,head4);
display_element(head4);
head4-&next=NULL;
}
if(c1=='D')
{
printf(&集合1的补集为:\n&);
bing_element(head3,head1,head4);
display_element(head4);
head4-&next=NULL;
}
if(c1=='E')
{
printf(&集合2的补集为:\n&);
bing_element(head3,head2,head4);
display_element(head4);
head4-&next=NULL;
}
}复制代码
下面是把交集和补集改过之后的,顺便改成了c风格的 没有用到c++的引用,而是用的二级指针
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 78 天[LV.6]常住居民II
本帖最后由 熊文杰 于
00:57 编辑
下面是把交集和补集改过之后的,顺便改成了c风格的 没有用到c++的引用,而是用的二级指针 #include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& && &&&
& && &&&struct element *
}
element* init_element(element *head)//初始化集合(集合是用单链表存储结构存储的)
{
& && &&&head=(element*)malloc(sizeof(struct element));
& && &&&head-&next=NULL;
& && &&&
}
void readdata_element(element **_head)//键盘输入每个集合的元素
{
& && &&&static char szBuf[256] = {0};
& && &&&element *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-&
& && && && && && && && &head-&next=p;
& && && && && & }
& && && && && & else
& && && && && & {
& && && && && && && && &printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& && && && && && && && &
& && && && && & }
& && && && && & i++;& && &&&
& && &&&}
& && &&&memset(szBuf,0,256);
}
void display_element(element *head)//输出集合中的所有元素
{
& && &&&element *p;
& && &&&p=head-&
& && &&&while(p!=NULL)
& && &&&{
& && && && && & printf(&%c&,p-&data);
& && && && && & p=p-&
& && &&&}
& && &&&printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p3=(element*)malloc(sizeof(struct element));
& && && && && & p3-&data=p1-&
& && && && && & p3-&next=head3-&
& && && && && & head3-&next=p3;
& && && && && & p1=p1-&
& && &&&}
& && &&&p2=head2-&
& && &&&while(p2!=NULL)
& && &&&{
& && && && && & p1=head1-&
& && && && && & while((p1!=NULL) && (p1-&data!=p2-&data))
& && && && && && && && &p1=p1-&
& && && && && & if(p1==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p2-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p2=p2-&
& && &&&}
}
void jiao_element(element *head1,element *head2,element **head3)//求两个集合的交集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && & {
& && && && && && && && &p2=p2-&
& && && && && & }
& && && && && && && && &
& && && && && & if(p2 != NULL&&&& p2-&data==p1-&data)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=(*head3)-&
& && && && && && && && &(*head3)-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void cha_element(element *head1,element *head2,element **head3)//求两个集合的差集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=(*head3)-&
& && && && && && && && &(*head3)-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
//第一个参数是全集
void bu_element(element *head1,element *head2,element **head3)//求集合的补集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p1-&data!=p2-&data))
& && && && && & {
& && && && && && && && &p2=p2-&
& && && && && & }
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=(*head3)-&
& && && && && && && && &(*head3)-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
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;
}复制代码
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 433 天[LV.9]以坛为家II
学习学习!!!!!!!!!!!
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 78 天[LV.6]常住居民II
只帮你改得可以运行,还有一些逻辑上的问题,楼主自己处理一下把#include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& & & &
& & & & struct element *
}
element init_element(element *&head)//初始化集合(集合是用单链表存储结构存储的)
{
& & & & head=(element*)malloc(sizeof(struct element));
& & & & head-&next=NULL;
& & & & return *
}
void readdata_element(element *&head)//键盘输入每个集合的元素
{
& & & & static char szBuf[256] = {0};
& & & &
& & & & 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-&
& & & & & & & & & & & & head-&next=p;
& & & & & & & & }
& & & & & & & & else
& & & & & & & & {
& & & & & & & & & & & & printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& & & & & & & & & & & &
& & & & & & & & }
& & & & & & & & i++;& & & &
& & & & }
& & & & memset(szBuf,0,256);
}
void display_element(element *head)//输出集合中的所有元素
{
& & & & element *p;
& & & & p=head-&
& & & & while(p!=NULL)
& & & & {
& & & & & & & & printf(&%c&,p-&data);
& & & & & & & & p=p-&
& & & & }
& & & & printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& & & & element *p1,*p2,*p3;
& & & & p1=head1-&
& & & & while(p1!=NULL)
& & & & {
& & & & & & & & p3=(element*)malloc(sizeof(struct element));
& & & & & & & & p3-&data=p1-&
& & & & & & & & p3-&next=head3-&
& & & & & & & & head3-&next=p3;
& & & & & & & & p1=p1-&
& & & & }
& & & & p2=head2-&
& & & & while(p2!=NULL)
& & & & {
& & & & & & & & p1=head1-&
& & & & & & & & while((p1!=NULL) && (p1-&data!=p2-&data))
& & & & & & & & & & & & p1=p1-&
& & & & & & & & if(p1==NULL)
& & & & & & & & {
& & & & & & & & & & & & p3=(element*)malloc(sizeof(struct element));
& & & & & & & & & & & & p3-&data=p2-&
& & & & & & & & & & & & p3-&next=head3-&
& & & & & & & & & & & & head3-&next=p3;
& & & & & & & & }
& & & & & & & & p2=p2-&
& & & & }
}
void jiao_element(element *head1,element *head2,element *&head3)//求两个集合的交集
{
& & & & element *p1,*p2,*p3;
& & & & p1=head1-&
& & & & while(p1!=NULL)
& & & & {
& & & & & & & & p2=head2-&
& & & & & & & & while((p2!=NULL) && (p2-&data!=p1-&data))
& & & & & & & & & & & & p2=p2-&
& & & & & & & & if(p2-&data==p1-&data)
& & & & & & & & {
& & & & & & & & & & & & p3=(element*)malloc(sizeof(struct element));
& & & & & & & & & & & & p3-&data=p1-&
& & & & & & & & & & & & p3-&next=head3-&
& & & & & & & & & & & & head3-&next=p3;
& & & & & & & & }
& & & & & & & & p1=p1-&
& & & & }
}
void cha_element(element *head1,element *head2,element *&head3)//求两个集合的差集
{
& & & & element *p1,*p2,*p3;
& & & & p1=head1-&
& & & & while(p1!=NULL)
& & & & {
& & & & & & & & p2=head2-&
& & & & & & & & while((p2!=NULL) && (p2-&data!=p1-&data))
& & & & & & & & & & & & p2=p2-&
& & & & & & & & if(p2==NULL)
& & & & & & & & {
& & & & & & & & & & & & p3=(element*)malloc(sizeof(struct element));
& & & & & & & & & & & & p3-&data=p1-&
& & & & & & & & & & & & p3-&next=head3-&
& & & & & & & & & & & & head3-&next=p3;
& & & & & & & & }
& & & & & & & & p1=p1-&
& & & & }
}
void bu_element(element *head1,element *head2,element *&head3)//求集合的补集
{
& & & & element *p1,*p2,*p3;
& & & & p1=head1-&
& & & & p2=head2-&
& & & & while(p1!=NULL)
& & & & {
& & & & & & & & while((p2!=NULL) && (p1-&data!=p2-&data))
& & & & & & & & {
& & & & & & & & & & & & p2=p2-&
& & & & & & & & }
& & & & & & & & if(p2==NULL)
& & & & & & & & {
& & & & & & & & & & & & p3-&data=p1-&
& & & & & & & & & & & & p3-&next=head3-&
& & & & & & & & & & & & head3-&next=p3;
& & & & & & & & & & & & p3=head3-&
& & & & & & & & }
& & & & & & & & p1=p1-&
& & & & }
}
int main()
{
& & & & element *head1,*head2,*head3,*head4;
& & & & init_element(head1);
& & & & init_element(head2);
& & & & init_element(head3);
& & & & init_element(head4);
& & & & 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&);
& & & & & & & & bing_element(head1,head2,head4);
& & & & & & & & display_element(head4);
& & & & & & & & head4-&next=NULL;
& & & & }
& & & & else if('C' == c1)
& & & & {
& & & & & & & & printf(&集合1与集合2的差集为:\n&);
& & & & & & & & bing_element(head1,head2,head4);
& & & & & & & & display_element(head4);
& & & & & & & & head4-&next=NULL;
& & & & }
& & & & else if('D' == c1)
& & & & {
& & & & & & & & printf(&集合1的补集为:\n&);
& & & & & & & & bing_element(head3,head1,head4);
& & & & & & & & display_element(head4);
& & & & & & & & head4-&next=NULL;
& & & & }
& & & & else if('E' == c1)
& & & & {
& & & & & & & & printf(&集合2的补集为:\n&);
& & & & & & & & bing_element(head3,head2,head4);
& & & & & & & & display_element(head4);
& & & & & & & & head4-&next=NULL;
& & & & }
& & & & system(&pause&);
& & & & return 0;
}复制代码
热爱鱼C^_^给力
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 32 天[LV.5]常住居民I
哇,只知道并查集是怎么搞的,但是不知道交集和补集怎么求
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 56 天[LV.5]常住居民I
三楼用了一些c++的语法,正解的,如果c的话把传参的引用& 不要就可以
判断那里应该要加=号吧。
我也看了看~~学习~~这是代码:
void readdata_element(element *head)//键盘输入每个集合的元素
& & & & static char szBuf[256] = {0};
& & & & 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-&
& & & & & & & & & & & & head-&next =
& & & & & & & & }
& & & & & & & & else
& & & & & & & & {
& & & & & & & & & & & & //printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& & & & & & & & & & & &
& & & & & & & & }
& & & & & & & & i++;& && &&&
& & & & memset( szBuf, 0, 256 );
一起学习,希望对大家有帮助。{:7_155:}支持支持
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 11 天[LV.3]偶尔看看II
牛逼{:7_155:}
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 90 天[LV.6]常住居民II
熊文杰 发表于
只帮你改得可以运行,还有一些逻辑上的问题,楼主自己处理一下把
根据你的建议 我将代码改成下面这样 但是不能正确计算补集和交集啊&&两个函数看了很多遍 还是不知道是错在函数里 还是主函数里 麻烦你再帮我看一下,谢谢#include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& && &&&
& && &&&struct element *
}
element* init_element()//初始化集合(集合是用单链表存储结构存储的)
{
& && &&&element *
& && &&&head=(element*)malloc(sizeof(struct element));
& && &&&head-&next=NULL;
& && &&&
}
void readdata_element(element *head)//键盘输入每个集合的元素
{
& && &&&static char szBuf[256] = {0};
& && &&&printf( &请输入集合中的数据元素:\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-&
& && && && && && && && &head-&next =
& && && && && & }
& && && && && & else
& && && && && & {
& && && && && && && && &//printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& && && && && && && && &
& && && && && & }
& && && && && & i++;& && &&&
& && &&&}
& && &&&memset( szBuf, 0, 256 );
}
void display_element(element *head)//输出集合中的所有元素
{
& && &&&element *p;
& && &&&p=head-&
& && &&&while(p!=NULL)
& && &&&{
& && && && && & printf(&%c&,p-&data);
& && && && && & p=p-&
& && &&&}
& && &&&printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p3=(element*)malloc(sizeof(struct element));
& && && && && & p3-&data=p1-&
& && && && && & p3-&next=head3-&
& && && && && & head3-&next=p3;
& && && && && & p1=p1-&
& && &&&}
& && &&&p2=head2-&
& && &&&while(p2!=NULL)
& && &&&{
& && && && && & p1=head1-&
& && && && && & while((p1!=NULL) && (p1-&data!=p2-&data))
& && && && && && && && &p1=p1-&
& && && && && & if(p1==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p2-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p2=p2-&
& && &&&}
}
void jiao_element(element *head1,element *head2,element *&head3)//求两个集合的交集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2-&data==p1-&data)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void cha_element(element *head1,element *head2,element *&head3)//求两个集合的差集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void bu_element(element *head1,element *head2,element *&head3)//求集合的补集(head1是全集)
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&
& && &&&while(p1!=NULL)
& && &&&{
& & & & & & & && && &&&p2=head2-&
& && && && && &
& & & & & & & & & & & && & while((p2!=NULL) && (p1-&data!=p2-&data))
& && && && && & {
& && && && && && && && &p2=p2-&
& && && && && & }
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && && && && &p3=head3-&
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
int main()
{
& && &&&element *head1,*head2,*head3,*head4;
& && &&&head1=init_element();
& && &&&head2=init_element();
& && &&&head3=init_element();
& && &&&head4=init_element();
& && &&&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;
}复制代码
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 90 天[LV.6]常住居民II
fanki 发表于
三楼用了一些c++的语法,正解的,如果c的话把传参的引用& 不要就可以
判断那里应该要加=号吧。
我也看了看 ...
根据你们的建议 我将代码改成下面这个样子 但是不能正确计算出交集和补集&&我看了很多遍 不知道是函数的问题还是在主函数里出了问题 麻烦 你再帮我看一下好吗,谢谢根据你的建议 我将代码改成下面这样 但是不能正确计算补集和交集啊&&两个函数看了很多遍 还是不知道是错在函数里 还是主函数里 麻烦你再帮我看一下,谢谢
#include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& && &&&
& && &&&struct element *
}
element* init_element()//初始化集合(集合是用单链表存储结构存储的)
{
& && &&&element *
& && &&&head=(element*)malloc(sizeof(struct element));
& && &&&head-&next=NULL;
& && &&&
}
void readdata_element(element *head)//键盘输入每个集合的元素
{
& && &&&static char szBuf[256] = {0};
& && &&&printf( &请输入集合中的数据元素:\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-&
& && && && && && && && &head-&next =
& && && && && & }
& && && && && & else
& && && && && & {
& && && && && && && && &//printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& && && && && && && && &
& && && && && & }
& && && && && & i++;& && &&&
& && &&&}
& && &&&memset( szBuf, 0, 256 );
}
void display_element(element *head)//输出集合中的所有元素
{
& && &&&element *p;
& && &&&p=head-&
& && &&&while(p!=NULL)
& && &&&{
& && && && && & printf(&%c&,p-&data);
& && && && && & p=p-&
& && &&&}
& && &&&printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p3=(element*)malloc(sizeof(struct element));
& && && && && & p3-&data=p1-&
& && && && && & p3-&next=head3-&
& && && && && & head3-&next=p3;
& && && && && & p1=p1-&
& && &&&}
& && &&&p2=head2-&
& && &&&while(p2!=NULL)
& && &&&{
& && && && && & p1=head1-&
& && && && && & while((p1!=NULL) && (p1-&data!=p2-&data))
& && && && && && && && &p1=p1-&
& && && && && & if(p1==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p2-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p2=p2-&
& && &&&}
}
void jiao_element(element *head1,element *head2,element *&head3)//求两个集合的交集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2-&data==p1-&data)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void cha_element(element *head1,element *head2,element *&head3)//求两个集合的差集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void bu_element(element *head1,element *head2,element *&head3)//求集合的补集(head1是全集)
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && && && &&&p2=head2-&
& && && && && &
& && && && && && && && && &while((p2!=NULL) && (p1-&data!=p2-&data))
& && && && && & {
& && && && && && && && &p2=p2-&
& && && && && & }
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && && && && &p3=head3-&
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
int main()
{
& && &&&element *head1,*head2,*head3,*head4;
& && &&&head1=init_element();
& && &&&head2=init_element();
& && &&&head3=init_element();
& && &&&head4=init_element();
& && &&&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;
}复制代码
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 90 天[LV.6]常住居民II
fanki 发表于
三楼用了一些c++的语法,正解的,如果c的话把传参的引用& 不要就可以
判断那里应该要加=号吧。
我也看了看 ...
晚上又琢磨了几个小时&&改了一下 现在就剩一个问题了 求交集的时候 集合1的元素个数要是大于集合2的元素个数&&就不能执行&&只有集合1的元素个数小于集合2的元素个数才能执行&&求交集的函数 我隐藏每一行都检查了 就是if这个循环语句的问题 手动也搞了半天 就是不知道这是个啥问题 求帮助(下面是修改后的代码)#include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& && &&&
& && &&&struct element *
}
element* init_element()//初始化集合(集合是用单链表存储结构存储的)
{
& && &&&element *
& && &&&head=(element*)malloc(sizeof(struct element));
& && &&&head-&next=NULL;
& && &&&
}
void readdata_element(element *head)//键盘输入每个集合的元素
{
& && &&&static char szBuf[256] = {0};
& && &&&printf( &请输入集合中的数据元素:\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-&
& && && && && && && && &head-&next =
& && && && && & }
& && && && && & else
& && && && && & {
& && && && && && && && &//printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& && && && && && && && &
& && && && && & }
& && && && && & i++;& && &&&
& && &&&}
& && &&&memset( szBuf, 0, 256 );
}
void display_element(element *head)//输出集合中的所有元素
{
& && &&&element *p;
& && &&&p=head-&
& && &&&while(p!=NULL)
& && &&&{
& && && && && & printf(&%c&,p-&data);
& && && && && & p=p-&
& && &&&}
& && &&&printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p3=(element*)malloc(sizeof(struct element));
& && && && && & p3-&data=p1-&
& && && && && & p3-&next=head3-&
& && && && && & head3-&next=p3;
& && && && && & p1=p1-&
& && &&&}
& && &&&p2=head2-&
& && &&&while(p2!=NULL)
& && &&&{
& && && && && & p1=head1-&
& && && && && & while((p1!=NULL) && (p1-&data!=p2-&data))
& && && && && && && && &p1=p1-&
& && && && && & if(p1==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p2-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p2=p2-&
& && &&&}
}
void jiao_element(element *head1,element *head2,element *head3)//求两个集合的交集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1;
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2;
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if((p2-&data)==(p1-&data))
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& & & & & & & & & & & & display_element(head3);
& & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & &
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
& & & & & & & &
}
void cha_element(element *head1,element *head2,element *&head3)//求两个集合的差集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void bu_element(element *head1,element *head2,element *head3)//求集合的补集(head1是全集)
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& & & & & & & &
& && &&&
& && &&&while(p1!=NULL)
& && &&&{
& & & & & & & && && &&&p2=head2-&
& && && && && &
& & & & & & & & & & & && & while((p2!=NULL) && (p1-&data!=p2-&data))
& && && && && &{
& && && && && && && &&&p2=p2-&
& && && && && & }
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && &&&p3=(element*)malloc(sizeof(struct element));& &
& & & & & & & & & & & & & & & & & & & & p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && && && && &p3=head3-&
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
int main()
{
& && &&&element *head1,*head2,*head3,*head4;
& && &&&head1=init_element();
& && &&&head2=init_element();
& && &&&head3=init_element();
& && &&&head4=init_element();
& && &&&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;
}复制代码
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 90 天[LV.6]常住居民II
熊文杰 发表于
只帮你改得可以运行,还有一些逻辑上的问题,楼主自己处理一下把
晚上又琢磨了几个小时&&改了一下 现在就剩一个问题了 求交集的时候 集合1的元素个数要是大于集合2的元素个数&&就不能执行&&只有集合1的元素个数小于集合2的元素个数才能执行&&求交集的函数 我隐藏每一行都检查了 就是if这个循环语句的问题 手动也搞了半天 就是不知道这是个啥问题 求帮助(下面是修改后的代码)#include &string.h&
#include&stdio.h&
#include&stdlib.h&
#define NULL 0
typedef struct element//集合元素的结构体
{
& && &&&
& && &&&struct element *
}
element* init_element()//初始化集合(集合是用单链表存储结构存储的)
{
& && &&&element *
& && &&&head=(element*)malloc(sizeof(struct element));
& && &&&head-&next=NULL;
& && &&&
}
void readdata_element(element *head)//键盘输入每个集合的元素
{
& && &&&static char szBuf[256] = {0};
& && &&&printf( &请输入集合中的数据元素:\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-&
& && && && && && && && &head-&next =
& && && && && & }
& && && && && & else
& && && && && & {
& && && && && && && && &//printf(&输入错误!必须是小写字母或者0~9的数字!请重新输入:\n&);
& && && && && && && && &
& && && && && & }
& && && && && & i++;& && &&&
& && &&&}
& && &&&memset( szBuf, 0, 256 );
}
void display_element(element *head)//输出集合中的所有元素
{
& && &&&element *p;
& && &&&p=head-&
& && &&&while(p!=NULL)
& && &&&{
& && && && && & printf(&%c&,p-&data);
& && && && && & p=p-&
& && &&&}
& && &&&printf(&\n&);
}
void bing_element(element *head1,element * head2,element * &head3)//求两个集合的并集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p3=(element*)malloc(sizeof(struct element));
& && && && && & p3-&data=p1-&
& && && && && & p3-&next=head3-&
& && && && && & head3-&next=p3;
& && && && && & p1=p1-&
& && &&&}
& && &&&p2=head2-&
& && &&&while(p2!=NULL)
& && &&&{
& && && && && & p1=head1-&
& && && && && & while((p1!=NULL) && (p1-&data!=p2-&data))
& && && && && && && && &p1=p1-&
& && && && && & if(p1==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p2-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p2=p2-&
& && &&&}
}
void jiao_element(element *head1,element *head2,element *head3)//求两个集合的交集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1;
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2;
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if((p2-&data)==(p1-&data))
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& & & & & & & & & & & & display_element(head3);
& & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & &
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
& & & & & & & &
}
void cha_element(element *head1,element *head2,element *&head3)//求两个集合的差集
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& && &&&while(p1!=NULL)
& && &&&{
& && && && && & p2=head2-&
& && && && && & while((p2!=NULL) && (p2-&data!=p1-&data))
& && && && && && && && &p2=p2-&
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && && && &p3=(element*)malloc(sizeof(struct element));
& && && && && && && && &p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
void bu_element(element *head1,element *head2,element *head3)//求集合的补集(head1是全集)
{
& && &&&element *p1,*p2,*p3;
& && &&&p1=head1-&
& & & & & & & &
& && &&&
& && &&&while(p1!=NULL)
& && &&&{
& & & & & & & && && &&&p2=head2-&
& && && && && &
& & & & & & & & & & & && & while((p2!=NULL) && (p1-&data!=p2-&data))
& && && && && &{
& && && && && && && &&&p2=p2-&
& && && && && & }
& && && && && & if(p2==NULL)
& && && && && & {
& && && && && && &&&p3=(element*)malloc(sizeof(struct element));& &
& & & & & & & & & & & & & & & & & & & & p3-&data=p1-&
& && && && && && && && &p3-&next=head3-&
& && && && && && && && &head3-&next=p3;
& && && && && && && && &p3=head3-&
& && && && && & }
& && && && && & p1=p1-&
& && &&&}
}
int main()
{
& && &&&element *head1,*head2,*head3,*head4;
& && &&&head1=init_element();
& && &&&head2=init_element();
& && &&&head3=init_element();
& && &&&head4=init_element();
& && &&&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;
}复制代码
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
签到天数: 56 天[LV.5]常住居民I
权志龙_爱死你la 发表于
晚上又琢磨了几个小时&&改了一下 现在就剩一个问题了 求交集的时候 集合1的元素个数要是大于集合2的元素个 ...
昨晚没有上线~~不好意思吖。
如果逻辑没有问题的话,你试试这样。把长的永远放在第一个集合里面放进去做吧。
我改了一下。
加了一个函数:
对比集合一集合二,如果集合一大于集合二不交换,如果不大于的话交换
void exchange( element* head1, element* head2 )
& & & & element *p1 = head1;
& & & & element *p2 = head2;
& & & & int x1 = 0, x2 = 0;
& & & & while( p1 == NULL )
& & & & & & & & x1++;
& & & & & & & & p1 = p1-&
& & & & while( p2 == NULL )
& & & & & & & & x2++;
& & & & & & & & p2 = p2-&
& & & & if ( x1 & x2 )
& & & & else
& & & & & & & & p1 = head1;
& & & & & & & & head1 = head2;
& & & & & & & & head2 = p1;
原来判断的代码为:
& & & & else if('B' == c1)
& & & & & & & & printf(&集合1与集合2的交集为:\n&);
& & & & & & & & exchange( head1, head2 );
& & & & & & & & jiao_element(head1,head2,head4);
& & & & & & & & display_element(head4);
& & & & & & & & head4-&next=NULL;
加一个exchange的判断。
这样做就可以了,这样改比较简单,如果要详细改的话,你可以在jiao_element里边改
原因是因为你只按照集合一大于集合二的方式做,没有考虑集合二大于集合一的考虑。
希望对你有帮助{:7_155:}一起学习学习,加油哦。
如果您的【问题求助】得到满意的解答,请自行将分类修改为【已经解决】;如果想鼓励一下楼主或帮助到您的朋友,可以给他们【评分】鼓励;善用【论坛搜索】功能,那里可能有您想要的答案!
•••(
Powered by}

我要回帖

更多关于 数据结构c语言程序 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信