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

怎么用c语言编写集合运算

发布时间: 2023-02-12 06:06:58

1. 数据结构 用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...!

2. 如何利用c语言实现集合的运算

这种集合问题,一般是使用链表来处理。
比如说有两个链表list1,list2,它们分别用来存储集合A,B
如果要对A,B进行并运算,
那么可以首先定义一个新的链表list3,然后让list1和list2的值进行循环的比较,如果有相同的值,那么就将这个值放入list3中,直到没有相同的值为止。
大致的算法就是这样了。

3. C语言求集合运算

可以用线性表模拟集合,把两个线性表中一样的数提取出来就是交集,所有元素组成的就是并集,还可以用C++重载运算符实现+就求并集之类的。

4. 用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("} ");
}

5. 怎样用语言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]<<"
";
}

6. C语言 集合运算

Deletetable函数有两个地方把pcollelm写成了collelm
Addition函数体第4行没加分号
Addition函数中3次调用AppendToTable时都写了3个参数,但AppendToTable只声明了2个参数
Multiply的返回类型写了collelm,应该是pcollelm
Multiply的函数参数是x,y,但函数体里面写的是a,b
Multiply里调用AppendToTable时,p的前面不用写类型
main的第3行,p=collp;改成p=&collp;
main里面case 2调用Deletetable时,第一个参数是Collelm类型的colla,但Deletetable对应的参数类型是Collelm *

7. 用c语言编写两个集合的运算

记得采纳哦

集合

#include "stdafx.h"

#include <stdio.h>

int fun(int a,int M[])//判断元素是否在集合里 在返回1 不在返回0

{

int i=0;

for(i=0;M[i]!=0;i++)

if(a==M[i]) return 1;

return 0;

}

void get(int M[])//输入集合元素

{

int i=0;

printf(" ");

do

{

scanf("%d",&M[i++]);

}

while(M[i-1]!=0);

}

void print(int M[])//打印集合

{

int i=0;

printf(" ");

while(M[i]!=0)

{

printf("%d ",M[i++]);

}

printf(" ");

}

void clear(int M[])

{

int i=0;

do

{

M[i++]=0;

}

while(M[i]!=0);

}

void fun_sum(int A[],int B[],int C[])//集合A和集合B的并集

{

int i,j;

for(i=0;A[i]!=0;i++)

{

C[i]=A[i];

}

for(j=0;B[j]!=0;j++)

{

if(!fun(B[j],C)) C[i++]=B[j];

}

}

void fun_sub(int A[],int B[],int C[])//集合A和集合B的差集

{

int i,j=0;

for(i=0;A[i]!=0;i++)

{

if(!fun(A[i],B)) C[j++]=A[i];

}

}

void fun_J(int A[],int B[],int C[])//集合A和集合B的交集

{

int i,j=0;

for(i=0;A[i]!=0;i++)

{

if(fun(A[i],B)) C[j++]=A[i];

}

}

int main(int argc, char* argv[])

{

int A[50]={0},B[50]={0},C[100]={0};

printf("请输入集合A以0结束 ");

get(A);

printf("请输入集合B以0结束 ");

get(B);

fun_sum(A,B,C);

printf("集合A与集合B的并: ");

print(C);

clear(C);

fun_sub(A,B,C);

printf("集合A与集合B的差: ");

print(C);

clear(C);

fun_J(A,B,C);

printf("集合A与集合B的交: ");

print(C);

return 0;

}