当前位置:首页 » 编程语言 » c语言集合的交集并集
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言集合的交集并集

发布时间: 2023-05-16 11:01:07

1. 本人碰见一道c语言难题,寻大神帮助,利用C语言实现:求任意两个集合的交集、并集、差集,

以前写过一个纯C的, 用的是数组,模拟C++ STL里面的set_intersection,set_union和set_difference的实现。 稍作了修改,添加了些注释,希望能帮唤衡到你。注意:必须先对输入集合橡枯排序;和如做输出结果和C++ STL的测试结果吻合。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int set_intersection (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes);
int set_union (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes);
int set_difference (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes);
int compare (const void * a, const void * b);
void print_array(const int arr[], const size_t len);
int main(int argc, char** argv)
{
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
unsigned int size1, size2, size_intxn, size_union, size_diff, retcode;
int *pr_intxn, *pr_union, *pr_diff;
/* Pre-requirement, set MUST be sorted. */
size1 = sizeof(first) / sizeof(first[0]);
size2 = sizeof(second) / sizeof(second[0]);
qsort(first, size1, sizeof(int), compare);
qsort(second, size2, sizeof(int), compare);

/* Intersection */
size_intxn = (size1 > size2) ? size1 : size2; /* estimate size of result */

pr_intxn = (int *)malloc(sizeof(int) * size_intxn); /* pre-allocate result */
if (NULL == pr_intxn) {
printf("Intersection memory error. ");
return -1;
}

printf("1) Set of Intersection: ");
retcode = set_intersection(first, size1, second, size2, pr_intxn, &size_intxn);
if (retcode == 0)
print_array(pr_intxn, size_intxn);
else
printf("Error in set_intersection, code %d ", retcode);

free(pr_intxn);

/* Union */
size_union = size1 + size2; /* estimate size of result */

pr_union = (int *)malloc(sizeof(int) * size_union); /* pre-allocate result */
if (NULL == pr_union) {
printf("Union memory error. ");
return -1;
}

printf("2) Set of Union: ");
retcode = set_union(first, size1, second, size2, pr_union, &size_union);
if (retcode == 0)
print_array(pr_union, size_union);
else
printf("Error in set_union, code %d ", retcode);
free(pr_union);

/* Difference */
size_diff = size1 + size2; /* estimate size of result */

pr_diff = (int *)malloc(sizeof(int) * size_diff); /* pre-allocate result */
if (NULL == pr_diff) {
printf("Difference memory error. ");
return -1;
}

printf("3) Set of Difference: ");
retcode = set_difference(first, size1, second, size2, pr_diff, &size_diff);
if (retcode == 0)
print_array(pr_diff, size_diff);
else
printf("Error in set_difference, code %d ", retcode);

free(pr_diff);


return 0;
}
/*
Input:
Set1 - First set.
Set2 - Second set.
SizeofSet1 - Set length of First set.
SizeofSet2 - Set length of Second set.
Input/Output:
Res - Set for storing results.
pSizeofRes - Point to SizeofRes, length of result. If SizeofRes is less than
expected, a proper size will be returned to caller.
Return:
0 - If successfully.
1 - SizeofRes is smaller than expected.
-1 - Internal memory error.
*/
int set_difference (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes)
{
int i, j, k;
unsigned int size_pre;
int *pr = 0;

size_pre = SizeofSet1 + SizeofSet2;
if ( *pSizeofRes < size_pre)
{
*pSizeofRes = size_pre;
return 1;
}

pr = (int *)malloc(size_pre * sizeof(int));
if ( pr == NULL )
return -1;

i = 0; j = 0; k = 0;
while ( i < SizeofSet1 && j < SizeofSet2 )
{
if (Set1[i] < Set2[j]) pr[k++] = Set1[i++];
else if (Set2[j] < Set1[i]) ++j;
else
{ i++; j++;}
}
memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
k += SizeofSet1-i;

memcpy(Res, pr, k*sizeof(int));
*pSizeofRes = k;
free(pr);

return 0;
}
int set_union (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes)
{
int i, j, k;
unsigned int size_pre;
int *pr = 0;

size_pre = SizeofSet1 + SizeofSet2;
if ( *pSizeofRes < size_pre)
{
*pSizeofRes = size_pre;
return 1;
}

pr = (int *)malloc(size_pre * sizeof(int));
if ( pr == NULL )
return -1;

i = 0; j = 0; k = 0;
while ( 1 )
{
if (i > SizeofSet1 - 1)
{
memcpy(pr+k, Set2+j-1, sizeof(int)*(SizeofSet2-j+1));
k += SizeofSet2 - j;
break;
}

if (j > SizeofSet2 - 1)
{
memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
k += SizeofSet1 - i;
break;
}
if (Set1[i] < Set2[j]) pr[k] = Set1[i++];
else if (Set2[j] < Set1[i]) pr[k] = Set2[j++];
else { pr[k] = Set1[i]; ++i; ++j; }
++k;
}
memcpy(Res, pr, k*sizeof(int));
*pSizeofRes = k;
free(pr);

return 0;
}

int set_intersection (const int Set1[], const unsigned int SizeofSet1,
const int Set2[], const unsigned int SizeofSet2,
int Res[], unsigned int* pSizeofRes)
{
int i, j, k;
unsigned int size_pre;
int *pr = 0;

size_pre = (SizeofSet1 > SizeofSet2) ? SizeofSet1 : SizeofSet2;
if ( *pSizeofRes < size_pre)
{
*pSizeofRes = size_pre;
return 1;
}

pr = (int *)malloc(size_pre * sizeof(int));
if ( pr == NULL )
return -1;

i = 0; j = 0; k = 0;
while ( i < SizeofSet1 && j < SizeofSet2 )
{
if (Set1[i] < Set2[j]) ++i;
else if (Set2[j] < Set1[i]) ++j;
else
{
pr[k++] = Set1[i];
i++; j++;
}
}
memcpy(Res, pr, k*sizeof(int));
*pSizeofRes = k;
free(pr);

return 0;
}
void print_array(const int arr[], const size_t len)
{
int i;

for (i = 0; i < len; i++)
printf("%d ", arr[i]);

printf(" ");
}
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

2. 实验、集合的交、并差 用c语言

#include"stdio.h"

intinput(intd[],intn)
{
inti;
//n=0;
do
{
scanf("%d",d+n);
n+=1;
}while(d[n-1]>=0);
d[n-1]='';
returnn-1;
}
voidsort(intd[],int*n)
{
inti,j,t,k;
for(i=0;i<*n-1;i++)
{
for(j=*n-1;j>i;j--)
{ if(d[j]==d[j-1])
{
*n-=1;
for(k=j;k<*n;k++)
d[k]=d[k+1];
}
if(d[j]<d[j-1])
{
t=d[j];d[j]=d[j-1];d[j-1]=t;
}

}
}
}
intfn(intd1[],intnum1,intd2[],intnum2)
{
inti,j,m;

for(i=0;i<num1;i++)
{
m=0;
for(j=0;j<num2;j++)
{
if(d1[i]==d2[j])
{
m=1;
break;
}
}
if(m==0)
printf("%d,",d1[i]);
}
}
intmain()
{
intA[100],B[100],C[200];
intnuma,numb,n;
inti,j;
//输入
printf("inputsortA:");
numa=input(A,0);
sort(A,&numa);
printf("inputsortB:");
numb=input(B,0);
sort(B,&numb);
//交集
printf("集合交集A∩B={");
for(i=0;i<numa;i++)
{
for(j=0;j<numb;j++)
{
if(A[i]==B[j])
{
printf("%d,",A[i]);
}

}
}
printf("} ");
//并集
n=numa+numb;
printf("集合并集A∪B={");
for(i=0;i<numa;i++)
{
C[i]=A[i];
}
for(i=numa;i<n;i++)
{
C[i]=B[i-numa];
}
sort(C,&n);
for(i=0;i<n;i++)
printf("%d,",C[i]);
printf("} ");
//集合差
printf("A-B={");
fn(A,numa,B,numb);
printf("} ");
}

3. 用C语言编写一个集合的交,并和差运算的程序怎么写啊

/*第一,你的题意不明,我只能输入两个集合了【互异性由输入保证】*/
#include<stdio.h>
#include<string.h>
void main()
{
char temp[60]="",str1[30]="",str2[30]="",i,j,l1,l2,ch;
printf("STR1:");
gets(str1);
printf("STR2:");
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
//交集
printf(" 交集: {");
for(i=0;i<l1;i++)
for(j=0;j<l2;j++)
if(str1[i]==str2[j]) printf("%c,",str1[i]);
printf("} ");

//并集 偷懒的算法: 合并->排序->删除相同
printf(" 并集: {");

/*合并*/sprintf(temp,"%s%s",str1,str2);
/*排序*/
for(i=0;i<l1+l2-1;i++)
for(j=i+1;j<l1+l2;j++)
if(temp[i]>temp[j])
{
char ch;
ch=temp[i];
temp[i]=temp[j];
temp[j]=ch;
}
/*删除相同字符*/
for(i=j=1;i<l1+l2;i++)
if(temp[i]!=temp[j-1]) temp[j++]=temp[i];
temp[j]=''
for(i=0;i<j;i++)
printf("%c,",temp[i]);
printf("} ");
//CuA
printf(" CuA: {");
for(ch='a'ch<='z'ch++)
{
for(i=0;i<l1;i++)
if(ch==str1[i]) goto NOT;
printf("%c,",ch);
NOT:if(0);
}
printf("} ");
//CuB
printf(" CuB: {");
for(ch='a'ch<='z'ch++)
{
for(i=0;i<l2;i++)
if(ch==str2[i]) goto NOT2;
printf("%c,",ch);
NOT2:if(0);
}
printf("} ");
}

4. 用c语言求两个集合的交集,并集,差集

#include<stdio.h>
#include<string.h>
#include<conio.h>

#defineARR_LEN255 /*数组长度上限*/
#defineelemTypechar /*集合元素数据类型*/

/*集合数据结构*/
typedefstructset{
elemTypedata[ARR_LEN];
intlength;
}set;

/*初始化集合*/
voidinitSet(set*S){
S->length=0;
}

/*交集*/
/*A与B的交集(A∩B):既属于A又属于B的元素构成的集合*/
intsetIntersection(setA,setB,set*dest){
inti=0,j=0,k=0;
dest->length=0;
for(i=0;i<A.length;i++){/*外循环遍历A*/
for(j=0;j<B.length;j++){/*内循环遍历B*/
if(A.data[i]==B.data[j]){/*既属于A又属于B的迹培元素,存入dest*/
dest->data[k]=A.data[i];
k++;
}
}
}
dest->length=k;
if(dest->length)
return1;
else
return0;
}

/*并集*/
/*A与B的并集(A∪B):A与B所有元素构成的集合*/
intsetUnion(setA,setB,set*dest){
inti=0,j=0,k=0;
dest->length=0;
for(i=0;i<A.length;i++){/*外循环遍历A*/
for(j=0;j<B.length;j++){/*内循环遍历B*/
if(A.data[i]==B.data[j])/*既属于A又属于B的元素,跳过*/
break;
}
if(j==B.length){/*属于A但不属于B的元素,存入dest*/
dest->data[k]=A.data[i];
k++;
}
}
for(j=0;j<B.length;j++){/*B的所有元素,存入dest*/
dest->data[k]=B.data[j];
k++;
}
dest->length=k;
if(dest->length)
return1;
else
return0;
}

/*补集*/
/*B在A中的相对补集(A\B):属于A但不属于B的元素构成的集合*/
intsetComplement(setA,setB,set*dest){
inti=0,j=0,k=0;
dest->length=0;
for(i=0;i<A.length;i++){/*外循环遍历A*/
for(j=0;j<B.length;j++){/*内循环遍历B*/
if(A.data[i]==B.data[j])/*既属于A又属于B的元素,跳过*/
break;
}
if(j==B.length){/*属简禅于A但不属于B的元素,存入dest*/
dest->data[k]=A.data[i];
k++;
}
}
dest->length=k;
if(dest->length)
return1;
else
return0;
}

/*打印集合内容*/
intprintSet(setS){
inti;
if(S.length==0){
puts("Thesetisempty!");
return0;
}
for(i=0;i<S.length;i++)
printf("%c",S.data[i]);
putchar(' ');
return1;
}

intmain(void){
setA,B;
setAIB,AUB,ACB;拦州尘/*交集、并集、补集*/

initSet(&A);initSet(&B);
initSet(&AIB);initSet(&AUB);initSet(&ACB);

strcpy(A.data,"123");
A.length=strlen(A.data);
strcpy(B.data,"4532");
B.length=strlen(B.data);

printf("A: ");
printSet(A);
printf("B: ");
printSet(B);
putchar(' ');

printf("A∩B: ");
setIntersection(A,B,&AIB);
printSet(AIB);

printf("A∪B: ");
setUnion(A,B,&AUB);
printSet(AUB);

printf("A\B: ");
setComplement(A,B,&ACB);
printSet(ACB);

getch();/*屏幕暂留*/
return0;
}

5. C语言用函数指针和数组指针求两个集合的交、并集

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUMBER1 7
#define NUMBER2 5
void createCollect(int [],int);
void display(int [],int);
int main()
{
int collection1[NUMBER1];
int collection2[NUMBER2];
int collection3[NUMBER1+NUMBER2];
int i,j,label=0;
createCollect(collection1,NUMBER1);

for(j = 0;j < NUMBER2;j++)
{
collection2[j]=j*2;
}

printf("集合1:");
display(collection1,NUMBER1);

printf("集合2:");
display(collection2,NUMBER2);

for(i = 0; i < NUMBER1;i++)
{
//从集合1中取出元素,去遍历集合2中的所有元素
for(j = 0;j < NUMBER2;j++)
{
//如果相同则跳出遍历
if(collection1[i]==collection2[j])
break;
}
//判断:此时存在两种情况1) 当前集合1的元素与集合2的元素相同
//2)遍历完了集合2的数组后不存在相同的元素
if(j == NUMBER2)
{
collection3[label]=collection1[i];
label++;
}

}

//把集合2的所有元素存进collection3中
for(j = 0;j < NUMBER2;j++)
{
collection3[label++]=collection2[j];
}

printf("集合1与集合2的并集为:\n");
display(collection3,label);

return 0;
}
//随机生成一个不含重复元素的数组
void createCollect(int num[],int count)
{
//randValue:临时随机数存放变量
//condition:循环生成不重复的条件
int i,j,randValue,condition;
srand(time(NULL));
for(i=0;i<count;i++)
{
condition=1;
while(condition){
randValue=1+(int)rand()%10;
for(j = 0;j <= i;j++){
if(i==0){//第一个数不可能存在重复数,可以直接赋值
condition=0;
break;
}
if(randValue==num[j])
break;
if(randValue!=num[j]&&j==i-1)//当生成的随机数与当前数组最后一位不同并且下标相等时
{
condition=0;
break;
}
}
}
num[i]=randValue;
}
}
//打印数组
void display(int num[],int count)
{
int i;
for(i=0;i<count;i++)
{
printf("%d\t",num[i]);
}
printf("\n");
}

6. 数据结构 用c语言写的 集合的并、交和差运算的程序

可以用二个一维数组,
再用两个for循环来判断结果:交,并,差
在for循环中,用一个if来判断一下,是不是a[0]==b[j],只要有相等的,就令之放在c[0]
这就是交集!!

并集就好求吧,
只要令c[i]=a[i],再来一个就是c[i+j+1]=b[j](因为我这里是考虑j=0开始的,然后自加差就是在交上改动一下就可以了,只要是a[0]!=b[j],就把它放到c[]这个数组里面去~!!!!

1:并集的程序。

求集合LA和集合LB的并集

#define NULL 0

struct JD
{ int data;
struct JD *next;
};

int find(int number,struct JD *h)
{ while(h->data)
{ if(h->data!=number)
{ h=h->next;
continue;
}
else
return 0;
}
return 1;
}

struct JD * make()
{ struct JD *h=NULL,*p=NULL;
int number,tf;
h=(struct JD *)malloc(sizeof(struct JD));
scanf("%d",&h->data);
p=h;
while(p->data)
{ p->next=(struct JD *)malloc(sizeof(struct JD));
p=p->next;
p->data=0;
scanf("%d",&number);
tf=find(number,h);
if(tf)
p->data=number;
else
continue;
}
return h;
}

void print(struct JD *h)
{ while(h->data)
{ printf("%d ",h->data);
h=h->next;
}
}

struct JD * change(struct JD *la,struct JD *lb)
{ struct JD *h,*p,*s,*q;
int number,tf;
p=lb;
while(p->data)
{ number=p->data;
tf=find(number,la);
p=p->next;
if(tf)
{ s=(struct JD *)malloc(sizeof(struct JD));
s->data=number;
s->next=la;
la=s;
}
else
continue;
}
return la;
}

void del(struct JD *h)
{ struct JD *p=h->next;
while(h->data)
{ free(h);
h=p;
p=p->next;
}
free(h);
}

main()
{ struct JD *la,*lb;
printf("\n\nGive the number to LA :\n\n");
la=make();
printf("\nLA is: ");
print(la);
printf("\n\nGive the number to LB :\n\n");
lb=make();
printf("\nLB is: ");
print(lb);
la=change(la,lb);
printf("\n\n\nThe new LA=LA||LB is: ");
print(la);
del(la);
del(lb);
printf("\n\n\nPass any key to exit...!\n");
getch();
}

********** 程序运行结果 **********
Give the number to LA :
1↓
2↓
3↓
5↓
0↓

LA is: 1 2 3 5

Give the number to LB :

6↓
7↓
3↓
2↓
9↓
0↓

LB is: 6 7 3 2 9

The new LA=LA||LB is: 9 7 6 1 2 3 5

--------------------------------------------------
Pass any key to exit...!

7. C语言求两个字符集合的交集和并集

int i,count=0,ch1[256]={0},ch2[256]={0};char s1[20],s2[20];//统计字符串1的字符情况for(i=0;i<20&&s1[i]!='\0';i++)ch1(s1[i])++;//统计字符串2的字符情况for(i=0;i<20&&s2[i]!='\0';i++)ch2(s2[i])++;//交集for(i=0;i<256;i++)if(ch1[i]>0&&ch2[i]>0){ putchar(i);count++;}if(count==0)printf("NULL");putchar('\n');//并集count=0;for(i=0;i<256;i++)if(ch1[i]>0&||ch2[i]>0) { putchar(i);count++;}if(count==0)printf("NULL");putchar('\n');

8. 用C语言实现: 已知两个集合A,B(成员为整数),求两个集合的交集,并集,结果存 放于A中,按递增排列。

#include<stdio.h>
#include<malloc.h>

typedefstructnode{
intnum;
structnode*next;
}AGG;

AGG*CreateList(){//创建单循环链表,返回链表头
AGG*head,*p;
inti,n;
printf("结点个数n=");
scanf("%d",&n);
head=p=(AGG*)malloc(sizeof(AGG));//专用头结点
head->num=0;
printf("输入%d整数(空格隔开): ",n);
for(i=0;i<n;++i){
p->next=(AGG*)malloc(sizeof(AGG));
scanf("%d",&p->next->num);
p=p->next;
}
p->next=head;
returnhead;
}

voidRiseSort(AGG*head){//上升排序
AGG*p,*s,*pt;
p=head;
s=p->next;
while(p->next!=head){
while(s->next!=head){
if(p->next->num>s->next->num){
pt=p->next;
p->next=s->next;
s->next=p->next->next;
p->next->next=pt;
}
elses=s->next;
}
p=p->next;
s=p->next;
}
}

voidSimplification(AGG*head){//去除相同的集合元素
AGG*p,*q,*s;
p=head->next;
q=p->next;
while(q!=head){
if(p->num==q->num){
p->next=q->next;
s=q;
q=q->next;
deletes;
}
else{
p=p->next;
q=q->next;
}
}
}

AGG*CreateAgg(){
AGG*head;
head=CreateList();
RiseSort(head);;
Simplification(head);
returnhead;
}

voidInsertNode(AGG*head,intnum){
AGG*t,*p=head;
while(p->next!=head){
if(p->next->num==num)return;
if(p->next->num<num)p=p->next;
else{
t=(AGG*)malloc(sizeof(AGG));
t->num=num;
t->next=p->next;
p->next=t;
return;
}
}
t=(AGG*)malloc(sizeof(AGG));
t->num=num;
p->next=t;
t->next=head;//插入在链表尾的处理
}

AGG*MergeAgg(AGG*A,AGG*B){//A∪B
AGG*head,*pa,*pb,*pc,*qc;
head=pc=(AGG*)malloc(sizeof(AGG));
pa=A->next;
while(pa!=A){
qc=(AGG*)malloc(sizeof(AGG));
qc->num=pa->num;
pc->next=qc;
pc=qc;
pa=pa->next;
}
pc->next=head;
pb=B->next;
while(pb!=B){
InsertNode(head,pb->num);
pb=pb->next;
}
returnhead;
}

AGG*MutualAgg(AGG*A,AGG*B){//A∩B
AGG*C,*pa,*pb,*pc,*qc;
C=pc=(AGG*)malloc(sizeof(AGG));
pc->num=0;
pa=A->next;
pb=B;
while(pa!=A){
pb=B->next;
while(pb!=B){
if(pb->num==pa->num){
qc=(AGG*)malloc(sizeof(AGG));
qc->num=pb->num;
pc->next=qc;
pc=qc;
}
pb=pb->next;
}
pa=pa->next;
}
pc->next=C;
returnC;
}

AGG*DifferAgg(AGG*A,AGG*B){//返回A、B的差集A-B
AGG*head,*p,*q,*r;
inttag;
head=r=(AGG*)malloc(sizeof(AGG));
for(p=A->next;p!=A;p=p->next){
tag=1;
for(q=B->next;q!=B&&tag;q=q->next)
tag=p->num!=q->num;
if(tag){
r->next=(AGG*)malloc(sizeof(AGG));
r=r->next;
r->num=p->num;
}
}
for(p=B->next;p!=B;p=p->next){
tag=1;
for(q=A->next;q!=A&&tag;q=q->next)
tag=p->num!=q->num;
if(tag){
r->next=(AGG*)malloc(sizeof(AGG));
r=r->next;
r->num=p->num;
}
}
r->next=head;
RiseSort(head);
returnhead;
}

voidPrintList(AGG*head){
AGG*p=head->next;
shortcounter=0;
while(p!=head){
if(counter&&counter%10==0)printf(" ");
printf("%5d",p->num);
counter++;
p=p->next;
}
if(counter%10)printf(" ");
}

voidfreeheap(AGG*head){
AGG*p,*q;
p=head;
q=p->next;
while(q!=head){
p=q;
q=p->next;
free(p);
}
free(head);
}

intmain(){
AGG*A,*B,*C,*D,*E;
printf("创建集合A: ");
A=CreateAgg();
printf("创建集合B: ");
B=CreateAgg();
printf("集合A的元素有: ");
PrintList(A);
printf("集合B的元素有: ");
PrintList(B);
C=MutualAgg(A,B);
printf("交集C=A∩B: ");
PrintList(C);
printf("并集D=A∪B: ");
D=MergeAgg(A,B);
PrintList(D);
printf("差集D=A-B: ");
E=DifferAgg(A,B);
PrintList(E);
freeheap(A);
freeheap(B);
freeheap(C);
freeheap(D);
freeheap(E);
printf(" ");
return0;
}

9. 如何用C语言编写求交集和并集的程序

char c[20];//存储交集的字符int count=0;//统计交集个数for (n=1;n<j;n++)
for (m=1;m<=k;m++)
{
if(a[n]==b[m]) { c[count]=a[n]; count++; }
}
} c[count]='\0';printf("交集为%s",c);

10. 怎样用语言c语言实现集合的合并,交集

通过你描述的问题,正确的交集代码如下:
void
bing(char
a[],char
b[],int
m,int
n)
{
char
d[400];
int
i=0,j=0,s=m;
for(i=0;i<m;i++)
d[i]=a[i];
for(i=0;i<n;i++){
for(j=0;j<m;j++)
{
if(b[i]==a[j])
break;
}
if(j==m)
d[s++]=b[i];
}
cout<<"集合并集是:";
for(i=0;i<s;i++)
cout<<d[i]<<"
";
}