《编程珠玑》中的问题用C实现——1
问题描述:一顺序文件中至多存在10000000个记录,每条记录都是一个7位整数,请对此文件中数据进行排序。
要求:1.程序可使用内存只有1MB。2.程序运行时间尽可能的短。
补充说明:每个记录都是一个7位正整数,并且没有其他的关联数据,每个整数至多只能出现一次。
实现纲要:
在现实中,位图和位向量很常见,我们可以使用一个20位的字符串来表示一个小型的小于20的非负整数集合。例如:我们可以将集合{1,2,3,5,8,13}存储在下面这个字符串中:01110100100001000000.集合中代表数字 的各个位设置为1,而其他的位全部都设为0.
在现实问题中,每个整数的7个十进制数字表示了一个小于千万的数字。我们将使用一个具有一千万位的字符串表示该文件,在该字符串中,当且公当整数i在该文件中时,第i个位才打开(设为1)。
实现代码:
1.位操作头文件:bit.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef BIT_H
#define BIT_H
#define BIT_SIZE 10000000
#define BIT_UNIT int
unsigned int GetBitUnitSize(void);
unsigned int GetBitArraySize(void);
void InitBitArray(BIT_UNIT *p);
void SetBitValue(BIT_UNIT *p, unsigned int bit, short val);
unsigned short GetBitValue(BIT_UNIT *p, unsigned int bit);
void PrintArrayList(BIT_UNIT *p);
BIT_UNIT SetIndexBitValue(unsigned short index);
#endif
2.位操作c文件:bit.c
#include "bit.h"
unsigned int GetBitUnitSize(void)
{
return sizeof(BIT_UNIT) * 8;
}
unsigned int GetBitArraySize(void)
{
return BIT_SIZE / GetBitUnitSize();
}
void InitBitArray(BIT_UNIT *p)
{
int array_size = GetBitArraySize();
int unit_size = GetBitUnitSize();
int i = 0;
for (i = 0; i < array_size; i++)
*(p + i) = *(p + i) << unit_size;
}
void SetBitValue(BIT_UNIT *p, unsigned int bit, short val)
{
if (bit >= BIT_SIZE) return;
unsigned int array_size = GetBitArraySize();
unsigned short unit_size = GetBitUnitSize();
unsigned int unit_index = bit / unit_size;
unsigned short bit_index = bit % unit_size;
if (bit_index == 0) {
unit_index--;
bit_index = unit_size;
}
相关文档:
Bioscom使用注意:串口接线方式为4,6;7,8分别短接。仅此函数要求。
否则会造成只能接受无法发送数据。函数返回值错误!
函数名: bioscom
功 能: 操作port指定的RS232异步通讯口
用 ......
【原型】
type fun( type arg1, type arg2, ...
);
【描述】
主要用在参数个数不确定的函数中,例如:printf函数。
【使用方法】
参考:glib/manual/Add.c
#include <stdarg.h>
#include <stdio.h>
int add_em_up (int coun ......
用c语言做了个通讯录,系统一运行时便将数据文件加载进内存,并用链表存储。退出系统时,自动将链表中的所有节点再存入文件。
可是现在,每次退出系统,文件里都会比链表多存储一条记录。
如:现在只有两条记录,退出后在启动时一查询,就会多一条乱记录(系统自己加的)。
加载文件的部分代码如下:
/*判断文件是否 ......
VC中下面几个结构体大小分别是多少呢
struct MyStruct
{
double m4;
char m1;
int m3;
};
struct MyStruct {
char m1;
double m4;
int m3;
};
#pragma pack(push)   ......