《C语言接口与实现》实验——集合(Set_T)

实验程序是用vc6编译,一定注意文件扩展名为c,不是cpp,在下载:http://download.csdn.net/detail/dijkstar/4036883


如下:

#include <stdio.h>
#include <stdlib.h>	//rand函数使用
#include <string.h>
#include "include/set.h"

#pragma comment(lib, "libcii.lib")

//产生一个[M, N]区间上的随机数
int my_rand(int M,int N)  
{  
    return (int)((double)rand()/(double)RAND_MAX*(N-M+1)+M);  
}  

//遍历输出函数
void Print(const void *member, void *cl)
{
	int v = (int)member;
	printf("\t%d\n", v);
}



void main()
{
	//注意:C语言一定要将这些变量声明放在函数的头部
	Set_T s, t, m;
	int i = 0;
	int	val;
	void **ary;
	int tmp1 = 0, *tmp = &tmp1;

	//创建两个集合
	t = Set_new(0, NULL, NULL);
	s = Set_new(0, NULL, NULL);

	//向集合中添加数据
	for (; i<5; i++)
	{
		val = my_rand(1000, 9999);	//生成【val】
		Set_put(s, val);

		val = my_rand(1000, 9999);	//生成【val】
		Set_put(t, val);
	}

	//输出表长度
	printf("集合s、t长度 = %d,%d\n", Set_length(s), Set_length(t));



	//输出两个集合的成员
	printf("\n集合s:\n");
	Set_map(s, Print, NULL);
	printf("\n集合t:\n");
	Set_map(t, Print, NULL);

	//测试是否在其中
	if (Set_member(s, 2739))
	{
		printf("\n成员2739 在集合s中\n");
	}

	//删除成员
	Set_remove(s, 2739);
	Set_remove(s, 4152);
	printf("\n集合s删除两个成员后:\n");
	Set_map(s, Print, NULL);



	//并集
	m = t = Set_union(s, t);
	printf("\n集合t(或者m)并集后:\n");
	Set_map(t, Print, NULL);


	//交集——因为上述操作t已经包含了s的三个成员
	t = Set_inter(s, t);
	printf("\n集合t交集后:\n");
	Set_map(t, Print, NULL);


	//先并集,再s-t
	t = Set_minus(m, t);
	printf("\n集合t = m-t 后:\n");
	Set_map(t, Print, NULL);

	//第二种方式输出
	printf("第二种方式Set_toArray输出\n");
	ary = Set_toArray(t, NULL);
	for(i=0; i<ary[i]; i++)
	{
		printf("\t%d\n", (int)ary[i]);
	}

}

输出:

集合s、t长度 = 5,5

集合s:
        4152
        6265
        8405
        2739
        1011

集合t:
        8278
        9063
        5318
        7719
        6072

成员2739 在集合s中

集合s删除两个成员后:
        6265
        8405
        1011

集合t(或者m)并集后:
        8278
        6265
        8405
        9063
        1011
        5318
        7719
        6072

集合t交集后:
        6265
        8405
        1011

集合t = m-t 后:
        8278
        9063
        5318
        7719
        6072
第二种方式Set_toArray输出
        8278
        9063
        5318
        7719
        6072
Press any key to continue


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值