?? 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 + -