📄 find.cpp
字号:
/*******************************************************************************
模 块: 查询数据库.
功 能: 按编码和条码查询数据库,返回查询结果.
程序员: 雷中南.
版 本: v1.0
时 间: 1999-05-18
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <def.h>
#include "find.h"
//构造函数.
Find::Find(char *datafname, char *indexfname)
{
//记录数据文件名信息.
strcpy(DataFName, datafname);
RecordLen = 109;
FileLen = GetFileLength(DataFName);
RecordNum = FileLen / RecordLen;
//记录索引文件信息.
strcpy(IndexFName, indexfname);
iRecordLen = 29;
iFileLen = GetFileLength(IndexFName);
iRecordNum = iFileLen / iRecordLen;
}
//析构函数.
Find::~Find()
{
}
//获取文件长度.
long
Find::GetFileLength(char *filename)
{
FILE *fp;
fp = fopen(filename, "rb");
long filelen = filelength(fileno(fp));
fclose(fp);
return filelen;
}
//从数据文件中读取数据.
int
Find::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 *)BarCode, temp, 14);
temp += 14;
BarCode[13] = NULL;
memcpy((void *)Code, temp, 14);
temp += 14;
Code[13] = NULL;
memcpy((void *)Name, temp, 25);
temp += 25;
Name[24] = NULL;
strcpy(Name, Trim(Name));
memcpy((void *)Price, temp, 10);
temp += 10;
Price[9] = NULL;
memcpy((void *)VipPrice1, temp, 10);
temp += 10;
VipPrice1[9] = NULL;
memcpy((void *)VipPrice2, temp, 10);
temp += 10;
VipPrice2[9] = NULL;
memcpy((void *)VipPrice3, temp, 10);
temp += 10;
VipPrice3[9] = NULL;
memcpy((void *)PrefPrice, temp, 10);
temp += 10;
PrefPrice[9] = NULL;
memcpy((void *)Type, temp, 5);
temp += 5;
Type[4] = NULL;
return L_OK;
}
int
Find::ReadNull()
{
//设定临时指针.
strcpy(BarCode, "0000000000000");
strcpy(Code, "000000000000*");
strcpy(Name, "无编码销售");
strcpy(Price, "1.00");
strcpy(VipPrice1, "1.00");
strcpy(VipPrice2, "1.00");
strcpy(VipPrice3, "1.00");
strcpy(PrefPrice, "1.00");
strcpy(Type, "0000");
return L_OK;
}
//读索引文件.
int
Find::iRead(long ID)
{
//将文件指针移到指定记录.
if(fseek(IndexFile, ID*((long)iRecordLen), 0)!=0)
{
return L_ERROR;
}
//读记录.
if(fread(iBuffer, iRecordLen, 1, IndexFile)==0)
{
return L_ERROR;
}
char *temp = iBuffer;
memcpy((void *)iBarCode, temp, 14);
temp += 14;
iBarCode[13] = NULL;
memcpy((void *)iCode, temp, 14);
temp += 14;
iCode[13] = NULL;
return L_OK;
}
//查找数据文件.
int
Find::SearchCode(char *code)
{
char temp[14];
if(strcmp("000000000000*", code) == 0)
{
ReadNull();
return L_OK;
}
//打开数据文件.
if((DataFile = fopen(DataFName, "rb")) == NULL)
{
fclose(DataFile);
return L_ERROR;
}
strncpy(temp, code, 13);
temp[13] = NULL;
long head = 0L, tail = RecordNum, current, answ;
current = (head + tail) / 2;
re:
//读取指定位置.
Read(current);
//比较字符串.
answ = strcmp(temp, Code);
//如果相同.
if(answ == 0)
{
fclose(DataFile);
return L_OK;
}
//如果头尾相邻.
if(labs(head - tail) <= 1)
{
//读头记录.
Read(head);
//比较.
answ = strcmp(temp, Code);
//相同.
if(answ == 0)
{
fclose(DataFile);
return L_OK;
}
//读尾记录.
Read(tail);
//比较.
answ = strcmp(temp, Code);
//相同.
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;
}
//查找索引文件.
int
Find::SearchBarCode(char *barcode)
{
//打开索引文件.
if((IndexFile = fopen(IndexFName, "rb")) == NULL)
{
fclose(IndexFile);
return L_ERROR;
}
long head=0, tail=iRecordNum, current, answ;
current = (head + tail) / 2;
re:
//从索引文件读取数据.
iRead(current);
//比较.
answ = strcmp(barcode, iBarCode);
//符合.
if(answ == 0)
{
fclose(IndexFile);
return L_OK;
}
//如果头尾相邻.
if(labs(head - tail) <= 1)
{
//读头记录.
iRead(head);
//比较.
if(strcmp(barcode, iBarCode) == 0)
{
fclose(IndexFile);
return L_OK;
}
//读尾记录.
iRead(tail);
//比较.
if(strcmp(barcode, iBarCode) == 0)
{
fclose(IndexFile);
return L_OK;
}
//如果此时头尾都不符合条件,则认为没有符合条件记录.
fclose(IndexFile);
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;
}
//综合查询.
int
Find::Search(char *data)
{
FileLen = GetFileLength(DataFName);
RecordNum = FileLen / RecordLen;
iFileLen = GetFileLength(IndexFName);
iRecordNum = iFileLen / iRecordLen;
//由于使用中条码较多,所以先按条码查询.
if(SearchBarCode(data)==L_ERROR)
{
//不存在条码,按编码查询.
if(SearchCode(data)==L_ERROR)
{
return L_ERROR;
}
else
{
return L_OK;
}
}
else
{
//存在条码,先读取条码对应编码,按编码再查询.
if(SearchCode(iCode)==L_ERROR)
{
return L_ERROR;
}
else
{
return L_OK;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -