當前位置:首頁 » 編程語言 » 集合的交並差c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

集合的交並差c語言

發布時間: 2023-04-03 21:53:16

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

B. 集合的並,交,差以及它們的混合運算的演算法(c語言)

/*diff.c:差運算*/
#include<stdio.h>
#include<stdlib.h>
intset_a[5]={1,3,5,9,7};//非0元素構成的集合
intset_b[5]={1,5,2,8,4};
intset_c[10]={0};
intisin(inta_or_b,intelem)
{
int*set_tmp;
intcnt;
switch(a_or_b)
{
case1:
set_tmp=set_b;
break;
case2:
set_tmp=set_a;
break;
default:
printf("parametererror! ");
}
for(cnt=0;cnt<5;cnt++)
{
if(set_tmp[cnt]==elem)
{
return1;
}
}
return0;
}
intmain(intargc,char*argv[])
{
intcnt,cnt2=0;
for(cnt=0;cnt<5;cnt++)
{
if(isin(1,set_a[cnt])==1)//如果集合a中的元素在集合b中
{
set_c[cnt2++]=set_a[cnt];
}
}
printf("Theresultofdiff: ");
for(cnt=0;set_c[cnt]!=0;cnt++)//輸出非0元素
{
printf("%d ",set_c[cnt]);
}
putchar(' ');
return0;
}

親是要這樣做差運算嗎?

C. 關於C語言數據結構線性表之集合的並交差

#include<stdio.h>
#include<stdlib.h>

#define N 1024

void sort(char *pt);
void bingji();
void jiaoji();
void chaji();

char str1[N],str2[N];
int main()
{
printf("請輸入兩個全由漏高仿小寫字母構成的字念冊符串,每行一個:\n");
gets(str1);
gets(str2);
bingji();
jiaoji();
chaji();
system("PAUSE");
return EXIT_SUCCESS;
}
void sort(char *pt)
{
int i,j;
char c;

for(i=0;i<strlen(pt)-1;i++)
{
for(j=strlen(pt)-1;j>i;j--)
{
if(pt[j]<pt[j-1])
{
c=pt[j-1];
pt[j-1]=pt[j];
pt[j]=c;
}
}
}
}
int iscontain(char a[],int len,char c)
{
int i;

for(i=0;i<len;i++)
{
if(c==a[i])
{
return 1;
}
}
return 0;
}
void bingji()
{
int i,j,len,len1=strlen(str1),len2=strlen(str2);
char* buf=(char*)calloc(len1+len2+1,sizeof(char));

for(i=0,j=0,len=0;i<len1||j<len2;)
{
if(i<len1)
{
if(!iscontain(buf,len,str1[i]))
{
buf[len++]=str1[i];
}
i++;
}
if(j<len2)
{
if(!iscontain(buf,len,str2[j]))
{
buf[len++]=str2[j];
}
j++;
}
}
buf[len]='\0';
sort(buf);
printf("%s\返纖n",buf);
free(buf);
}
void jiaoji()
{
int i,len,len1=strlen(str1),len2=strlen(str2);
char buf[N];

for(i=0,len=0;i<strlen(str1);i++)
{
if(!iscontain(buf,len,str1[i]) && iscontain(str2,len2,str1[i]))
{
buf[len++]=str1[i];
}
}
buf[len]='\0';
sort(buf);
printf("%s\n",buf);
}
void chaji()
{
int i,len,len1=strlen(str1),len2=strlen(str2);
char buf[N];

for(i=0,len=0;i<len1;i++)
{
if(!iscontain(buf,len,str1[i]) && !iscontain(str2,len2,str1[i]))
{
buf[len++]=str1[i];
}
}
buf[len]='\0';
sort(buf);
printf("%s\n",buf);
}

D. 怎樣用C語言實現集合的並交呢

#include<stdio.h>
#include<stdlib.h>

typedef struct pointer{
char dat;
struct pointer *link;
} pointer;

void readdata(pointer *head)
{ //讀集合
pointer *p;
char tmp;
printf("input data ('0' for end):");
scanf("%c",&tmp);
while(tmp!='0')
{
if((tmp<'a')||(tmp>'z'))
{
printf("輸入錯誤!必須為小寫字母!\n");
return;
}
p=(pointer *)malloc(sizeof(struct pointer));
p->dat=tmp;
p->link=head->link;
head->link=p;
scanf("%c",&tmp);
}
}

void disp(pointer *head)
{ //顯示集合數據
pointer *p;
p=head->link;
while(p!=NULL)
{
printf("%c ",p->dat);
p=p->link;
}
printf("\n");
}

void bing(pointer *head1,pointer *head2, pointer *head3)
{ //計算集合1與集合2的並
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
p1=p1->link;
}
p2=head2->link;
while(p2!=NULL)
{
p1=head1->link;
while((p1!=NULL)&&(p1->dat!=p2->dat))
p1=p1->link;
if(p1==NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p2->dat;
p3->link=head3->link;
head3->link=p3;
}
p2=p2->link;
}
}

void jiao(pointer *head1,pointer *head2, pointer *head3)
{ //計算集合1與集合2的交
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p2=head2->link;
while((p2!=NULL)&&(p2->dat!=p1->dat))
p2=p2->link;
if((p2!=NULL)&&(p2->dat=p1->dat))
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}

main()
{
pointer *head1,*head2,*head3;
head1=(pointer *)malloc(sizeof(struct pointer));
head1->link=NULL;
head2=(pointer *)malloc(sizeof(struct pointer));
head2->link=NULL;
head3=(pointer *)malloc(sizeof(struct pointer));
head3->link=NULL;
printf("輸入集合1:\n");
readdata(head1);
getchar();
printf("輸入集合2:\n");
readdata(head2);
printf("集合1為:\n");
disp(head1);
printf("集合2為:\n");
disp(head2);
printf("集合1與集合2的並為:\n");
bing(head1,head2,head3);
disp(head3);
head3->link=NULL;
printf("集合1與集合2的交為:\n");
jiao(head1,head2,head3);
disp(head3);
}

E. C語言求集合運算

可以用線性表模擬集合,把兩個線性表中一樣的數提取出來就是交集,所有元素組成的就是並集,還可以用C++重載運算符實現+就求並集之類的。

F. 實驗、集合的交、並差 用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("} ");
}

G. 數據結構 用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...!

H. 用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;

}

I. 用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;
}

J. C++在類中實現集合的交,並,差運算,怎麼做呀

1,是否屬於集合,就拿這個元素和集合裡面的比較啊,如果想等就存在。
2,實現並,就是拿一個集合A裡面的一個元素,和另外一個集合B的元素比較,如果想等就不要這個A中元素。不想等就把A中這個元素加入到B中,前提是B數組足夠大。也可以再建立個C集合。
3,實現交 ,就是把集合A中元素和集合B中元素想等的取出來,放到C中。
上面主要是利用for 循環進行。