本篇文章给大家谈谈c语言实现集合的交并差,以及c语言实现集合的交并差用单链表对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、C语言编写集合的并交差2、数据结构 用c语言写的 集合的并、交和差运算的程序3、用C语言编写一个集合的交,并和差运算的程序怎么写啊?4、用c语言求两个集合的交集,并集,差集5、实验、集合的交、并差 用c语言6、关于C语言数据结构线性表之集合的并交差
C语言编写集合的并交差
不知道这个是不是你想要的,
如果是的话,不懂我们可以继续探讨。
#include stdio.h
#include string.h
int main(void)
{
char str1[128], str2[128], str3[128];
int i, len1, len2;
printf(“Please input string1:”);
gets(str1);
printf(“Please input string2:”);
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
strcpy(str3, str1);/*把第一个数组赋给另外一个数组*/
printf(“%s,%s”, str2, str3);
if (len1 len2)/*str1数组长的时候交叉*/
{
for (i = 0; i len1; i++)
{
str1[2 * i] = str3[i];/*str1在前面的交叉*/
str1[2 * i + 1] = str2[i];
}
for (i = len1 * 2; i len1 + len2; i++)
{
str1[i] = str2[i – len1];
}
}
else if (len1 == len2)/*str1数组与str2相等时候的交叉*/
{
for (i = 0; i len1; i++)
{
str1[2 * i] = str3[i];
str1[2 * i + 1] = str2[i];
}
}
else if (len1 len2)/*str1数组长小于str2相等时候的交叉*/
{
for (i = 0; i len2; i++)
{
str1[2 * i] = str3[i];
str1[2 * i + 1] = str2[i];
}
for (i = len2 * 2; i len1 + len2; i++)
{
str1[i] = str3[i – len2];
}
}
str1[len1 + len2] = ‘\0’;
printf(“\nstring1:%s\n”, str1);
/*for (i = 0; str1[i] != ‘\0’; i++)
{
printf(“%c”, str1[i]);
}*//*也可以这样输出*/
return 0;
}
数据结构 用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语言编写一个集合的交,并和差运算的程序怎么写啊?
/*第一,你的题意不明,我只能输入两个集合了【互异性由输入保证】*/
#includestdio.h
#includestring.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(“\n交集:\n{“);
for(i=0;il1;i++)
for(j=0;jl2;j++)
if(str1[i]==str2[j]) printf(“%c,”,str1[i]);
printf(“\b}\n”);
//并集 偷懒的算法: 合并-排序-删除相同
printf(“\n并集:\n{“);
/*合并*/sprintf(temp,”%s%s”,str1,str2);
/*排序*/
for(i=0;il1+l2-1;i++)
for(j=i+1;jl1+l2;j++)
if(temp[i]temp[j])
{
char ch;
ch=temp[i];
temp[i]=temp[j];
temp[j]=ch;
}
/*删除相同字符*/
for(i=j=1;il1+l2;i++)
if(temp[i]!=temp[j-1]) temp[j++]=temp[i];
temp[j]=’\0′;
for(i=0;ij;i++)
printf(“%c,”,temp[i]);
printf(“\b}\n”);
//CuA
printf(“\nCuA:\n{“);
for(ch=’a’;ch=’z’;ch++)
{
for(i=0;il1;i++)
if(ch==str1[i]) goto NOT;
printf(“%c,”,ch);
NOT:if(0);
}
printf(“\b}\n”);
//CuB
printf(“\nCuB:\n{“);
for(ch=’a’;ch=’z’;ch++)
{
for(i=0;il2;i++)
if(ch==str2[i]) goto NOT2;
printf(“%c,”,ch);
NOT2:if(0);
}
printf(“\b}\n”);
}
用c语言求两个集合的交集,并集,差集
#include stdio.h
#include string.h
#include conio.h
#define ARR_LEN 255/*数组长度上限*/
#define elemType char/* 集合元素数据类型 */
/* 集合数据结构 */
typedef struct set {
elemType data[ARR_LEN];
int length;
} set;
/* 初始化集合 */
void initSet (set *S) {
S-length = 0;
}
/* 交集 */
/* A与B的交集(A∩B):既属于A又属于B的元素构成的集合 */
int setIntersection (set A, set B, set *dest) {
int i = 0, j = 0, k = 0;
dest-length = 0;
for (i=0; iA.length; i++) { /* 外循环遍历A */
for (j=0; jB.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)
return 1;
else
return 0;
}
/* 并集 */
/* A与B的并集(A∪B):A与B所有元素构成的集合 */
int setUnion (set A, set B, set *dest) {
int i = 0, j = 0, k = 0;
dest-length = 0;
for (i=0; iA.length; i++) { /* 外循环遍历A */
for (j=0; jB.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; jB.length; j++) { /* B的所有元素,存入dest */
dest-data[k] = B.data[j];
k++;
}
dest-length = k;
if (dest-length)
return 1;
else
return 0;
}
/* 补集 */
/* B在A中的相对补集(A\B):属于A但不属于B的元素构成的集合 */
int setComplement (set A, set B, set *dest) {
int i = 0, j = 0, k = 0;
dest-length = 0;
for (i=0; iA.length; i++) { /* 外循环遍历A */
for (j=0; jB.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)
return 1;
else
return 0;
}
/* 打印集合内容 */
int printSet (set S) {
int i;
if (S.length == 0) {
puts (“The set is empty! “);
return 0;
}
for (i=0; iS.length; i++)
printf (“%c”, S.data[i]);
putchar (‘\n’);
return 1;
}
int main (void) {
set A, B;
set AIB, 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:\t”);
printSet (A);
printf (“B:\t”);
printSet (B);
putchar (‘\n’);
printf (“A∩B:\t”);
setIntersection (A, B, AIB);
printSet (AIB);
printf (“A∪B:\t”);
setUnion (A, B, AUB);
printSet (AUB);
printf (“A\B:\t”);
setComplement (A, B, ACB);
printSet (ACB);
getch (); /*屏幕暂留*/
return 0;
}
实验、集合的交、并差 用c语言
#include “stdio.h”
int input(int d[],int n)
{
int i;
//n=0;
do
{
scanf(“%d”,d+n);
n+=1;
}while(d[n-1]=0);
d[n-1]=’\0′;
return n-1;
}
void sort(int d[],int *n)
{
int i,j,t,k;
for(i=0;i*n-1;i++)
{
for(j=*n-1;ji;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;
}
}
}
}
int fn(int d1[],int num1,int d2[],int num2)
{
int i,j,m;
for(i=0;inum1;i++)
{
m=0;
for(j=0;jnum2;j++)
{
if(d1[i]==d2[j])
{
m=1;
break;
}
}
if(m==0)
printf(“%d,”,d1[i]);
}
}
int main()
{
int A[100],B[100],C[200];
int numa,numb,n;
int i,j;
//输入
printf(“input sort A: “);
numa=input(A,0);
sort(A,numa);
printf(“input sort B: “);
numb=input(B,0);
sort(B,numb);
//交集
printf(“集合交集A ∩B={“);
for(i=0;inuma;i++)
{
for(j=0;jnumb;j++)
{
if(A[i]==B[j])
{
printf(“%d,”,A[i]);
}
}
}
printf(“\b}\n\n”);
//并集
n=numa+numb;
printf(“集合并集A∪B={“);
for(i=0;inuma;i++)
{
C[i]=A[i];
}
for(i=numa;in;i++)
{
C[i]=B[i-numa];
}
sort(C,n);
for(i=0;in;i++)
printf(“%d,”,C[i]);
printf(“\b}\n\n”);
//集合差
printf(“A-B={“);
fn(A,numa,B,numb);
printf(“\b}\n\n”);
}
关于C语言数据结构线性表之集合的并交差
#includestdio.h
#includestdlib.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;istrlen(pt)-1;i++)
{
for(j=strlen(pt)-1;ji;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;ilen;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;ilen1||jlen2;)
{
if(ilen1)
{
if(!iscontain(buf,len,str1[i]))
{
buf[len++]=str1[i];
}
i++;
}
if(jlen2)
{
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;istrlen(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;ilen1;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);
}
c语言实现集合的交并差的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言实现集合的交并差用单链表、c语言实现集合的交并差的信息别忘了在本站进行查找喔。