c语言集合运算
❶ 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 *
❷ C语言编程 集合运算演示系统 代码
尚缺存、取文件部分,其他部分都有,如交、并、差运算,排序等,需要的话,请追问。
❸ 数据结构 用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...!
❹ 如何用C语言做出“完成集合运算的并集,交集,补集”
可以手写平衡树,来完成c++ stl中的set功能,即可实现几何的交并补运算~
❺ c语言 集合的差,交,并,笛卡尔积的运算。例如A={2,3,4,5,6,7,8}B={2,3,4,5,11,25}
交:C={2,3,4,5} 就是既属于A的又属于B的那部分
并:C = {2,3,4,5,6,7,8,11,25} 两个集合的整合去掉重复的。A+B-AB(AB:公共部分)
差:C= {6,7,8}就是属于A但是不属于B的那部分
笛卡尔乘积:这个得出的集合就多了:举个例子。。假设集合A={a,b},集合B={c,d}则两个集合的笛卡尔积为{(a,c),(a,d),(b,c),(b,d)}
❻ C语言求集合运算
可以用线性表模拟集合,把两个线性表中一样的数提取出来就是交集,所有元素组成的就是并集,还可以用C++重载运算符实现+就求并集之类的。
❼ 编写程序,实现两个集合的交运算(用C语言)
#include<stdio.h>
#include<string.h>
intjiaoji(intA[],intB[],inta,intb)
{
inti,j,t;
t=a;
for(i=0;i<a;i++)
for(j=0;j<b;j++)
{
if(A[i]==B[j])
{
A[t]=B[j];
t++;
}
}
for(i=0;i<t-a;i++)
{
A[i]=A[a+i];
}
returnt-a;
}
intmain()
{
intA[50],B[50],a,b,t;
printf("请输入A的元素个数: ");
scanf("%d",&a);
printf("请输入A的元素: ");
for(inti=0;i<a;i++)
scanf("%d",&A[i]);
printf("请输入B的元素个数: ");
scanf("%d",&b);
printf("请输入B的元素: ");
for(inti=0;i<b;i++)
scanf("%d",&B[i]);
t=jiaoji(A,B,a,b);
for(inti=0;i<t;i++)
printf("%d",A[i]);
return0;
}
❽ 检索:集合运算 C语言
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedefstructset
{
int*array;
intlen;
}Set;
Set*new_set(intlen)
{
Set*s=(Set*)malloc(sizeof(structset));
s->len=len;
s->array=(int*)malloc(sizeof(int)*len);
memset(s->array,0,sizeof(int)*len);
returns;
}
voiddelete_set(Set**s)
{
free((*s)->array);
free(*s);
*s=NULL;
}
intis_belong_to_set(Set*a,intn)
{
inti;
for(i=0;i<a->len;i++)
{
if(n==a->array[i])
return1;
}
return0;
}
Set*set_diff(Set*a,Set*b)
{
inti=0,clen=0;
Set*c=new_set(a->len);
c->len=clen;
for(i=0;i<a->len;i++)
{
if(!is_belong_to_set(b,a->array[i]))
{
c->array[clen]=a->array[i];
clen++;
c->len=clen;
}
}
returnc;
}
Set*set_union(Set*a,Set*b)
{
inti,clen=0;
Set*c=new_set(a->len+b->len);
c->len=clen;
for(i=0;i<a->len||i<b->len;i++)
{
if(i<a->len)
if(!is_belong_to_set(c,a->array[i]))
{
c->array[clen]=a->array[i];
clen++;
c->len=clen;
}
if(i<b->len)
if(!is_belong_to_set(c,b->array[i]))
{
c->array[clen]=b->array[i];
clen++;
c->len=clen;
}
}
returnc;
}
voidsort(Set*a)
{
inti,j;
for(i=0;i<a->len;i++)
for(j=i+1;j<a->len;j++)
if(a->array[i]>a->array[j])
{
intt=a->array[i];
a->array[i]=a->array[j];
a->array[j]=t;
}
}
intmain()
{
intalen=0,blen=0,i;
Set*a,*b,*c;
scanf("%d",&alen);
a=new_set(alen);
for(i=0;i<alen;i++)
scanf("%d",&a->array[i]);
scanf("%d",&blen);
b=new_set(blen);
for(i=0;i<blen;i++)
scanf("%d",&b->array[i]);
Set*s1=set_diff(a,b);
Set*s2=set_diff(b,a);
c=set_union(s1,s2);
sort(c);
for(i=0;i<c->len;i++)
printf("%d",c->array[i]);
printf(" ");
delete_set(&a);
delete_set(&b);
delete_set(&c);
delete_set(&s1);
delete_set(&s2);
}
❾ 单链表表示的集合交,并,差运算,设计采用定义集合,用集合运算表达式求值的方式进行。C语言实现。
#include<stdio.h>
#include<stdlib.h>
typedef
struct
LNode//
定义结构体类型指针
{
char
data;
struct
LNode*next;
}*pointer;
void
readdata(pointer
head)//
定义输入集合函数
{
pointer
p;
char
tmp;
scanf("%c",&tmp);
while(tmp!='\n')
{
p=(pointer)malloc(sizeof(struct
LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
void
pop(pointer head)//定义输出集合函数
{
pointer
p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void
or(pointer head1,pointer head2,pointer head3)//定义集合的交集函数
{
pointer
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=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据,按回车键结束\n");
pointerhead1,head2,head3;
head1=(pointer)malloc(sizeof(structLNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(structLNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(structLNode));
head3->next=NULL;printf("请输入集合A:\n");
readdata(head1);//调用输入集合函数
printf("请输入集合B:\n");
readdata(head2);//调用输入集合函数A:
printf("1.交集2.结束\n");
do
{
printf("请选择序号\n");
scanf("%d",&x);
switch(x)
{
case1:
or(head1,head2,head3);//调用交集函数
printf("集合A为:");
pop(head1);
printf("集合B为:");
pop(head2);
printf("两集合的交集C=A∩B:");
pop(head3);
head3->next=NULL;
break;
case2:
break;
default:
gotoA;
}
}while(x!=2);
}
❿ 用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;
}