现在的位置: 首页 > 综合 > 正文

《C语言接口与实现》实验——位向量(Bit_T)

2013年09月01日 ⁄ 综合 ⁄ 共 2150字 ⁄ 字号 评论关闭

实验程序是用vc6编译,一定注意文件扩展名为c,不是cpp,下载前面几个测试程序(链表、表、原子中有下载链接)中直接将下面源程序覆盖1.c的内容即可!

强烈建议在这些函数上设上断点,按F11跟进去把源码走一遍!

源程序如下:

#include <stdio.h>
#include <string.h>
#include "include/bit.h"

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


//打印函数
void Print(int n, int bit, void *cl)
{
	printf("位置: %d---值: %d\n", n, bit);
}


void main()
{
	//注意:C语言一定要将这些变量声明放在函数的头部
	Bit_T b1, b2, b3;
	int i = 0;
	int val = 0;

	//创建【5】位的bit位(位数取得这么少,是为了打印方便观察!)
	//	≤32位时,分配1个int(32位)4字节;
	//	32-64位时,分配2个int(32位)4字节;
	//	..................................;
	b1 = Bit_new(5);
	
	//清除后打印
	printf("\nBit_clear清除后打印\n");
	Bit_clear(b1, 0, 4);
	Bit_map(b1, Print, NULL);


	//设置后打印
	printf("\nBit_set设置后打印\n");
	Bit_set(b1, 0, 4);
	Bit_map(b1, Print, NULL);


	//设置后打印
	printf("\nBit_put设置后打印\n");
	Bit_put(b1, 1, 0);
	Bit_put(b1, 4, 0);
	Bit_map(b1, Print, NULL);



	printf("\nBit_get 函数 = %d\n", Bit_get(b1, 1));
	printf("\nBit_length 函数 = %d\n", Bit_length(b1));
	printf("\nBit_count 函数 = %d\n", Bit_count(b1));

	//将b1的bit位清零后再“取反”,打印
	printf("\nBit_not设置后打印\n");
	Bit_clear(b1, 0, 4);
	Bit_not(b1, 1, 3);
	Bit_map(b1, Print, NULL);

	
	//【集合包含】
	// 做下列操作时,两个位向量的长度必须相等!
	b2 = Bit_new(5);
	Bit_clear(b2, 0, 4);
	Bit_not(b2, 1, 3);
	if (Bit_lt(b1, b2))
		printf("b1 是 b2的真子集\n");
	else
		printf("b1 不是 b2的真子集\n");

	if (Bit_eq(b1, b2))
		printf("b1 == b2\n");
	else
		printf("b1 != b2\n");

	if (Bit_leq(b1, b2))
		printf("b1 是 b2的子集\n");
	else
		printf("b1 不是 b2的子集\n");
	
	//【集合包含】
	// 做下列操作时,两个位向量的长度必须相等!
	b3 = Bit_new(5);
	b3 = Bit_union(b1, b2);
	printf("\n打印:b1+b2\n");
	Bit_map(b3, Print, NULL);	//打印:b1+b2


	Bit_put(b1, 1, 0);	
	b3 = Bit_inter(b1, b2);
	printf("\n打印:b1*b2\n");
	Bit_map(b3, Print, NULL);	//打印:b1*b2

	b3 = Bit_minus(b1, b2);
	printf("\n打印:b1-b2\n");
	Bit_map(b3, Print, NULL);	//打印:b1-b2

	b3 = Bit_diff(b1, b2);
	printf("\n打印:b1/b2(异或)\n");
	Bit_map(b3, Print, NULL);	//打印:b1/b2

}

输出:

Bit_clear清除后打印
位置: 0---值: 0
位置: 1---值: 0
位置: 2---值: 0
位置: 3---值: 0
位置: 4---值: 0

Bit_set设置后打印
位置: 0---值: 1
位置: 1---值: 1
位置: 2---值: 1
位置: 3---值: 1
位置: 4---值: 1

Bit_put设置后打印
位置: 0---值: 1
位置: 1---值: 0
位置: 2---值: 1
位置: 3---值: 1
位置: 4---值: 0

Bit_get 函数 = 0

Bit_length 函数 = 5

Bit_count 函数 = 3

Bit_not设置后打印
位置: 0---值: 0
位置: 1---值: 1
位置: 2---值: 1
位置: 3---值: 1
位置: 4---值: 0
b1 不是 b2的真子集
b1 == b2
b1 是 b2的子集

打印:b1+b2
位置: 0---值: 0
位置: 1---值: 1
位置: 2---值: 1
位置: 3---值: 1
位置: 4---值: 0

打印:b1*b2
位置: 0---值: 0
位置: 1---值: 0
位置: 2---值: 1
位置: 3---值: 1
位置: 4---值: 0

打印:b1-b2
位置: 0---值: 0
位置: 1---值: 0
位置: 2---值: 0
位置: 3---值: 0
位置: 4---值: 0

打印:b1/b2(异或)
位置: 0---值: 0
位置: 1---值: 1
位置: 2---值: 0
位置: 3---值: 0
位置: 4---值: 0
Press any key to continue

抱歉!评论已关闭.