⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findoper.cpp

📁 C++写的POS前台程序
💻 CPP
字号:
/*******************************************************************************
	模  块:	查操作员数据库.
	功  能:	按编码和口令查询数据库,返回查询结果.
	程序员:	雷中南.
	版  本:	v1.0
	时  间:	1999-05-24
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <def.h>
#include "findoper.h"

//构造函数.
FindOperator::FindOperator(char *datafname)
{
	//记录数据文件名信息.
	strcpy(DataFName, datafname);
	RecordLen = 38;
	FileLen = GetFileLength(DataFName);
	RecordNum = FileLen / RecordLen;
}

//析构函数.
FindOperator::~FindOperator()
{
}

//获取文件长度.
long
FindOperator::GetFileLength(char *filename)
{
	FILE *fp;
	fp = fopen(filename, "rb");
	long filelen = filelength(fileno(fp));
	fclose(fp);
	return filelen;
}

//从数据文件中读取数据.
int
FindOperator::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 *)OpCode, temp, 6);
		temp += 6;
		OpCode[5] = NULL;
	memcpy((void *)OpPassword, temp, 9);
		temp += 9;
		OpPassword[8] = NULL;
	memcpy((void *)OpName, temp, 11);
		temp += 11;
		OpName[10] = NULL;
	memcpy((void *)OpPower, temp, 11);
		temp += 11;
		OpPower[10] = NULL;
	return L_OK;
}

//查找数据文件.
int
FindOperator::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, OpCode);
	//如果相同.
	if(answ == 0)
	{
		fclose(DataFile);
		return L_OK;
	}
	//如果头尾相邻.
	if(labs(head - tail) <= 1)
	{
		//读头记录.
		Read(head);
		//比较.
		answ = strcmp(temp, OpCode);
		//相同.
		if(answ == 0)
		{
			fclose(DataFile);
			return L_OK;
		}
		//读尾记录.
		Read(tail);
		//比较.
		answ = strcmp(temp, OpCode);
		//相同.
		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 + -