vb语言集合差c=a-b的运算

vb语言集合差c=a-b的运算,第1张

根据集合差的运算概念:

a-b是指存在于集合a中但是不存在于b中的所有元素

用程序来表示这样的运算,首先要考虑用哪一种类型的变量来存放对应的数据;一般可以用数组来存放集合。

比如:集合A是一个自然数集合,包含:1,2,3,4,4个元素。B是另一个自然数集合,包含:2,34,5。

那么,根据集合差的定义,1属于A的元素,而不属于B的元素,而2,3,4既是集合A的元素,也是集合B的元素,那么c=a-b,得到的集合C只有一个元素就是 1。

算法:

建立数组A,赋值给A的各个元素。

建立数组B,赋值给B的各个元素。

建立数组C,将数组A的元素一一与数组B的元素进行比较,如果不存在于B,就把该元素保存到数组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...!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12033791.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存