新人求助...集合的交并运算....
集合的交并运算,求代码...
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define INITSIZE 20
#define ADDSIZE 10
typedef struct{
int *addr;
int length;
int size;
}Set;
void Initset(Set *l)
{l->addr=(int *)malloc(INITSIZE*sizeof(int));
if (!l->addr) exit(0);
l->length=0,l->size=20;}
void Addsetsize(Set *l)
{if (l==NULL) exit(0);
l->addr=(int *)realloc(l->addr,(l->size+ADDSIZE)*sizeof(int));
if (!l->addr) exit(0);
l->size+=ADDSIZE;}
void Assignmentset(Set *l)
{printf ("输入集合元素个数\n");
int flag,num;
scanf ("%d",&num);
l->length=num;
while (l->length>l->size) Addsetsize(l);
for (flag=0;flag<num;flag++)
{printf ("第%d个:",flag+1);
scanf ("%d",l->addr+flag);}
}
void Orderset(Set *l)
{if (l==NULL) exit(0);
int i,j,temp;
for (i=0;i<l->length-1;i++)
for (j=0;j<l->length-1-i;j++)
if (*(l->addr+j)>*(l->addr+j+1))
temp=*(l->addr+j),*(l->addr+j)=*(l->addr+j+1),*(l->addr+j+1)=temp;
}
void Intersection(Set l1,Set l2,Set *l3)
{l3->length=(l1.length>l2.length)?l1.length:l2.length;
while (l3->length>l3->size) Addsetsize(l3);
int *p1,*p2,*p3;
p1=l1.addr;
p2=l2.addr;
p3=l3->addr;
l3->length=0;
while (p1<l1.addr+l1.length&&p2<l2.addr+l2.length)
{if (*p1==*p2)
{*p3++=*p1;
p1++,p2++;
l3->length++;}
else
{if (*p1<*p2) p1++;
else p2++;}}
}
void Union(Set l1,Set l2,Set *l3)
{l3->length=l1.length+l2.length;
while (l3->length>l3->size) Addsetsize(l3);
int *p1,*p2,*p3;
p1=l1.addr;
p2=l2.addr;
p3=l3->addr;
l3->length=0;
while (p1<l1.addr+l1.length&&p2<l2.addr+l2.length)
if (*p1<*p2) {*p3++=*p1++;l3->length++;}
else if (*p2<*p1) {*p3++=*p2++;l3->length++;}
else {*p3++=*p1++;p2++;l3->length++;}
while (p1<l1.addr+l1.length) {*p3++=*p1++;l3->length++;}
while (p2<l2.addr+l2.length) {*p3++=*p2++;l3->length++;}
}
void Printset(Set l)
{int flag;
for (flag=0;flag<l.length;flag++)
printf ("%4d",*(l.addr+flag));
printf ("\n共%d个元素,存储空间为%d\n",l.length,l.size);
}
int main(void)
{Set l[4];
Initset(l);
printf ("集合A\n");
Assignmentset(l);
Initset(l+1);
printf ("集合B\n");
Assignmentset(l+1);
Orderset(l);
Orderset(l+1);
Initset(l+2);
Initset(l+3);
Union(l[0],l[1],l+2);
printf ("并集:\n");
Printset(l[2]);
Intersection(l[0],l[1],l+3);
printf ("交集:\n");
Printset(l[3]);
return 0;
}
#include <stdlib.h>
#include <memory.h>
#define INITSIZE 20
#define ADDSIZE 10
typedef struct{
int *addr;
int length;
int size;
}Set;
void Initset(Set *l)
{l->addr=(int *)malloc(INITSIZE*sizeof(int));
if (!l->addr) exit(0);
l->length=0,l->size=20;}
void Addsetsize(Set *l)
{if (l==NULL) exit(0);
l->addr=(int *)realloc(l->addr,(l->size+ADDSIZE)*sizeof(int));
if (!l->addr) exit(0);
l->size+=ADDSIZE;}
void Assignmentset(Set *l)
{printf ("输入集合元素个数\n");
int flag,num;
scanf ("%d",&num);
l->length=num;
while (l->length>l->size) Addsetsize(l);
for (flag=0;flag<num;flag++)
{printf ("第%d个:",flag+1);
scanf ("%d",l->addr+flag);}
}
void Orderset(Set *l)
{if (l==NULL) exit(0);
int i,j,temp;
for (i=0;i<l->length-1;i++)
for (j=0;j<l->length-1-i;j++)
if (*(l->addr+j)>*(l->addr+j+1))
temp=*(l->addr+j),*(l->addr+j)=*(l->addr+j+1),*(l->addr+j+1)=temp;
}
void Intersection(Set l1,Set l2,Set *l3)
{l3->length=(l1.length>l2.length)?l1.length:l2.length;
while (l3->length>l3->size) Addsetsize(l3);
int *p1,*p2,*p3;
p1=l1.addr;
p2=l2.addr;
p3=l3->addr;
l3->length=0;
while (p1<l1.addr+l1.length&&p2<l2.addr+l2.length)
{if (*p1==*p2)
{*p3++=*p1;
p1++,p2++;
l3->length++;}
else
{if (*p1<*p2) p1++;
else p2++;}}
}
void Union(Set l1,Set l2,Set *l3)
{l3->length=l1.length+l2.length;
while (l3->length>l3->size) Addsetsize(l3);
int *p1,*p2,*p3;
p1=l1.addr;
p2=l2.addr;
p3=l3->addr;
l3->length=0;
while (p1<l1.addr+l1.length&&p2<l2.addr+l2.length)
if (*p1<*p2) {*p3++=*p1++;l3->length++;}
else if (*p2<*p1) {*p3++=*p2++;l3->length++;}
else {*p3++=*p1++;p2++;l3->length++;}
while (p1<l1.addr+l1.length) {*p3++=*p1++;l3->length++;}
while (p2<l2.addr+l2.length) {*p3++=*p2++;l3->length++;}
}
void Printset(Set l)
{int flag;
for (flag=0;flag<l.length;flag++)
printf ("%4d",*(l.addr+flag));
printf ("\n共%d个元素,存储空间为%d\n",l.length,l.size);
}
int main(void)
{Set l[4];
Initset(l);
printf ("集合A\n");
Assignmentset(l);
Initset(l+1);
printf ("集合B\n");
Assignmentset(l+1);
Orderset(l);
Orderset(l+1);
Initset(l+2);
Initset(l+3);
Union(l[0],l[1],l+2);
printf ("并集:\n");
Printset(l[2]);
Intersection(l[0],l[1],l+3);
printf ("交集:\n");
Printset(l[3]);
return 0;
}