📄 search.c
字号:
#include "TypeDef.h"
#include "Function.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//*****书号查询*****用二分查找实现书号查询算法***
int BinSearch(BnoIdxFile *bif, char key[])
{
int i,j,k;
int l = 0;
int h = bif->len;
int m = (h + l) / 2;
BnoRecType temp;
//选择排序
for(i = 0; i < bif->len - 1; ++i)
{
for(j = i; j < bif->len; ++j)
{
if(strcmp(bif->BnoIdx[i].bno, bif->BnoIdx[j].bno) > 0)
{
k = j;
}
else
{
k = i;
}
}
if(i != k)
{
temp = bif->BnoIdx[k];
bif->BnoIdx[k] = bif->BnoIdx[i];
bif->BnoIdx[i] = temp;
}
}
while(l < m && h > m)
{
if(strcmp(bif->BnoIdx[m].bno, key) > 0)
{
h = m;
m = (l + h) / 2;
}
else if(strcmp(bif->BnoIdx[m].bno, key) < 0)
{
l = m;
m = (l + h) / 2;
}
else
{
return bif->BnoIdx[m].RecNo;
}
}
return 0;
//折半查找
}
//***********************************************
//*****书名查询**********************************
int BnameFind(BnameIdxFile *bnif, char key[])
{
int i;
for(i = 0; i < bnif->len; ++i)
{
if(strcmp(bnif->BnameIdx[i].bname, key) == 0)
{
return bnif->BnameIdx[i].head;
}
}
return 0;
}
//***********************************************
//*****作者查询**********************************
int BauthorFind(BauthorIdxFile *baif, char key[])
{
int i;
for(i = 0; i < baif->len; ++i)
{
if(strcmp(baif->BauthorIdx[i].author, key) == 0)
{
return baif->BauthorIdx[i].head;
}
}
return 0;
}
//***********************************************
//*****出版社查询********************************
int BpressFind(BpressIdxFile *bpif, char key[])
{
int i;
for(i = 0; i < bpif->len; ++i)
{
if(strcmp(bpif->BpressIdx[i].press, key) == 0)
{
return bpif->BpressIdx[i].head;
}
}
return 0;
}
//***********************************************
//*****输出一条图书主数据库**********************
void ShowRec(BookDbFile *bf, int i)
{
printf("%-6d %-4s %-10s %-8s %-10s %-4s %-6d %d\n",
i,
bf->BookDbase[i].bno,
bf->BookDbase[i].bname,
bf->BookDbase[i].author,
bf->BookDbase[i].press,
bf->BookDbase[i].sortno,
bf->BookDbase[i].storenum,
bf->BookDbase[i].borrownum);
}
//***********************************************
//*****图书查询控制程序**************************
void SearchBook(BookDbFile *bf, BnoIdxFile *bif, BnameIdxFile *bnif, BauthorIdxFile *baif, BpressIdxFile *bpif)
{
char key[12];
int result;
int i = 0;
char choice = '\0';
int next = 0;
while(1)
{
switch(SearchMenu())
{
case 'a':
printf("请输入书号:");
scanf("%s", key);
while(strlen(key) != 4)
{
printf("输入书号位数不正确,请重新输入:");
scanf("%s", key);
}
if((result = BinSearch(bif, key)) <= 0)
{
printf("查无此书!\n");
}
else
{
system("cls");
printf("记录号 书号 书名 作者 出版社 分类 藏书量 借出数\n");
ShowRec(bf, result);
}
printf("\n按任意键返回...");
choice = getchar();
if(choice == 10) //判断接收的是否为回车符
{
choice = getchar();
}
break;
case 'b':
printf("请输入书名:");
scanf("%s", key);
if((result = BnameFind(bnif, key)) == 0)
{
printf("查无此书!\n");
}
else
{
system("cls");
printf("记录号 书号 书名 作者 出版社 分类 藏书量 借出数\n");
next = result;
while(bf->BookDbase[next].namenext != 0)
{
ShowRec(bf, next);
next = bf->BookDbase[next].namenext;
}
ShowRec(bf, next);
}
printf("\n按任意键返回...");
choice = getchar();
if(choice == 10) //判断接收的是否为回车符
{
choice = getchar();
}
break;
case 'c':
printf("请输入作者:");
scanf("%s", key);
if((result = BauthorFind(baif, key)) == 0)
{
printf("查无此书!\n");
}
else
{
system("cls");
printf("记录号 书号 书名 作者 出版社 分类 藏书量 借出数\n");
next = result;
while(bf->BookDbase[next].authnext != 0)
{
ShowRec(bf, next);
next = bf->BookDbase[next].authnext;
}
ShowRec(bf, next);
}
printf("\n按任意键返回...");
choice = getchar();
if(choice == 10) //判断接收的是否为回车符
{
choice = getchar();
}
break;
case 'd':
printf("请输入出版社:");
scanf("%s", key);
if((result = BpressFind(bpif, key)) == 0)
{
printf("查无此书!\n");
}
else
{
system("cls");
printf("记录号 书号 书名 作者 出版社 分类 藏书量 借出数\n");
next = result;
while(bf->BookDbase[next].prenext != 0)
{
ShowRec(bf, next);
next = bf->BookDbase[next].prenext;
}
ShowRec(bf, next);
}
printf("\n按任意键返回...");
choice = getchar();
if(choice == 10) //判断接收的是否为回车符
{
choice = getchar();
}
break;
case 'e':
return;
}
}
}
//***********************************************
//*****查询菜单**********************************
char SearchMenu()
{
char choice;
system("cls");
printf("\n\t\t查询菜单\n");
printf("=========================\n");
printf("\ta.按 书 号 查找\n");
printf("\tb.按 书 名 查找\n");
printf("\tc.按 作 者 查找\n");
printf("\td.按 出版社 查找\n");
printf("\te.返回上一级菜单\n");
printf("=========================\n");
printf("请选择a—e:");
while(1)
{
choice = getchar();
if(choice == 10) //判断接收的是否为回车符
{
choice = getchar();
}
if(choice < 'a' || choice > 'e')
{
printf("\n\t输入错误,重选a—e:");
}
else
{
break;
}
}
return choice;
}
//***********************************************
//*****查找读者**********************************
int ReaderFind(ReaderFile *rf, char key[])
{
int i;
for(i = 0; i < rf->len; ++i)
{
if(strcmp(rf->ReadRec[i].rno, key) == 0)
{
return i;
}
}
return -1;
}
//***********************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -