📄 findgrp.cpp
字号:
/*******************************************************************************
模 块: 查柜组编码.
功 能: 按商品自编码前三位查询柜组编码.
程序员: 雷中南.
版 本: v1.0
时 间: 1999-05-29
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <def.h>
#include "findgrp.h"
//构造函数.
FindGroup::FindGroup(char *datafname)
{
//记录数据文件名信息.
strcpy(DataFName, datafname);
RecordLen = 12;
FileLen = GetFileLength(DataFName);
RecordNum = FileLen / RecordLen;
}
//析构函数.
FindGroup::~FindGroup()
{
}
//获取文件长度.
long
FindGroup::GetFileLength(char *filename)
{
FILE *fp;
fp = fopen(filename, "rb");
long filelen = filelength(fileno(fp));
fclose(fp);
return filelen;
}
//从数据文件中读取数据.
int
FindGroup::Read(long ID)
{
//将文件指针移到指定记录.
if(fseek(DataFile, ID*((long)RecordLen), 0)!=0)
{
return L_ERROR;
}
//读记录.
if(fread(Buffer, RecordLen, 1, DataFile)==0)
{
return L_ERROR;
}
//设定临时指针.
char *temp = Buffer;
//将数据赋给字段.
memcpy((void *)GrCode, temp, 4);
temp += 4;
GrCode[3] = NULL;
memcpy((void *)Group, temp, 7);
temp += 7;
Group[6] = NULL;
return L_OK;
}
//查找数据文件.
int
FindGroup::SearchCode(char *code)
{
char temp[6];
//打开数据文件.
if((DataFile = fopen(DataFName, "rb")) == NULL)
{
fclose(DataFile);
return L_ERROR;
}
strncpy(temp, code, 6);
temp[5] = NULL;
long head = 0L, tail = RecordNum, current, answ;
current = (head + tail) / 2;
re:
//读取指定位置.
Read(current);
//比较字符串.
answ = strcmp(temp, GrCode);
//如果相同.
if(answ == 0)
{
fclose(DataFile);
return L_OK;
}
//如果头尾相邻.
if(labs(head - tail) <= 1)
{
//读头记录.
Read(head);
//比较.
answ = strcmp(temp, GrCode);
//相同.
if(answ == 0)
{
fclose(DataFile);
return L_OK;
}
//读尾记录.
Read(tail);
//比较.
answ = strcmp(temp, GrCode);
//相同.
if(answ == 0)
{
fclose(DataFile);
return L_OK;
}
//如果此时头尾都不符合条件,则认为没有符合条件记录.
fclose(DataFile);
return L_ERROR;
}
if(answ > 0)//如果比较结果大于0.
{
//头折半.
head = current;
current = (head + tail) / 2;
goto re;
}
if(answ < 0)//如果比较结果小于0.
{
//尾折半.
tail = current;
current = (head + tail) / 2;
goto re;
}
return L_ERROR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -