实验程序是用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