这个程序已经能运行了,可结果就是达不到要求,好象有逻辑错误,本人愚钝调了两天了还是找不到错误,请各位朋友帮个忙吧。十分感谢!!
(C语言)
#define BookSize 100
#define BLHnum 50
#define RRnum 50
#include<stdio.h>
#include<string.h>
/*#include"type.h"
#include"createfile.c"
#include"search.c"
#include"reader.c"
#include"borrow.c"
#include"writefile.c"
#include"readfile.c"*/
typedef struct{ /*主数据库文件*/
char bno[5]; /*书号*/
char bname[21]; /*书名*/
int namenext; /*书名指针链*/
char author[9]; /*作者*/
int authnext; /*作者指针链*/
char press[11]; /*出版社*/
int prenext; /*出版社指针链*/
char sortno[4]; /*分类号*/
int storenum; /*藏书量*/
int borrownum; /*借出书数*/
}BookRecType;
typedef struct {
BookRecType BookDbase[BookSize];
int len; /*文件当前长度*/
}BookDbaseFile; /*定义图书数据库文件类型*/
typedef struct {
char bno[5]; /*书号*/
int RecNo; /*记录指针*/
}BidxRecType; /*索引文件记录类型*/
typedef struct {
BidxRecType BnoIdx[BookSize];
int len; /*当前记录个数*/
}BnoIdxFile; /*书号索引文件类型*/
typedef struct { /*书名链头索引文件*/
char bname[21]; /*书名*/
int lhead; /*链头指针*/
int RecNum; /*长度*/
}BNRecType; /*书名链头文件记录类型*/
typedef struct{
BNRecType LHFrec1[BLHnum];
int len1; /*链头文件当前长度*/
}LHFile1;/*书名链头文件类型*/
typedef struct{ /*作者链头文件索引文件*/
char author[9];/*作者*/
int lhead;/*链头指针*/
int RecNum;/*长度*/
}BARecType;/*作者链头文件记录类型*/
typedef struct{
BARecType LHFrec2[BLHnum];
int len2;
}LHFile2;/*作者链头文件类型*/
typedef struct{/*出版社链头索引文件*/
char press[11];/*出版社*/
int lhead;/*链头指针*/
int RecNum;/*出版社链头文件记录类型*/
}BPRecType;
typedef struct{
BPRecType LHFrec3[BLHnum];
int len3;
}LHFile3;/*出版社链头文件类型*/
typedef struct{
char rno[4];/*读者号*/
char name[8];/*读者名*/
int bn1;/*可借书数*/
int bn2;/*已借书数*/
}RRecType;/*读者文件记录类型*/
typedef struct{
RRecType ReadRec[RRnum];
int len;/*当前读者数*/
}ReadFile;/*读者文件类型*/
typedef struct{/*借还书文件*/
char rno[4];/*读者号*/
char bno[5];/*书号*/
char date1[9];/*借书日期*/
char date2[9];/*还书日期*/
}BbookRecType;/*借还书文件记录类型*/
typedef struct{
BbookRecType Bbook[BookSize];
int len;/*当前借书数*/
}BbookFile; /*借还书文件类型*/
void AppeDBaseRec(BookDbaseFile df)/*追加一条图书主数据库记录*/
{
int i; /*图书主数据库长度加1*/
i=++df.len;
printf("shu hao shu ming zuo zhe chubanshe fen lei cangshuliang\n");
scanf("%d%s",df.BookDbase[i].bno,df.BookDbase[i].bname);
scanf("%s%s",df.BookDbase[i].author,df.BookDbase[i].press);
scanf("%d%d",df.BookDbase[i].sortno,&df.BookDbase[i].storenum);
df.BookDbase[i].borrownum=0;/*借出数置零*/
}
void ChangeBnoIdxF(BookDbaseFile df,BnoIdxFile bif) /*修改书号索引表*/
{
int i,j,k;
char sh[4];
i=df.len; /*图书主文件的当前长度,也就是要处理的当前记录号*/
strcpy(sh,df.BookDbase[i].bno); /*取记录中书号送至变量SH中*/
j=bif.len;k=1;
while(j>=1) /*查找插入位置*/
{
if(strcmp(sh,bif.BnoIdx[j].bno)>0)
{ k=j+1;break;}
j--;
}
if(bif.len>0) /*有序表的插入*/
for(j=bif.len;j>=k;j--)
bif.BnoIdx[j+1]=bif.BnoIdx[j]; /*记录后移*/
strcpy(bif.BnoIdx[k].bno,sh);
bif.BnoIdx[k].RecNo=i;
bif.len++; /*表长加1*/
}
void ChangeLinkHeadF1(BookDbaseFile df,LHFile1 lhf1)/*修改书名索引以及书名链头索引表*/
{
int i,j,k,m;
char sm[20];
i=df.len; /*图书主文件的当前长度,也就好似要处理的当前记录号*/
strcpy(sm,df.BookDbase[i].bname); /*取记录中的书名送至变量sm中*/
j=1;k=0;
while(j<=lhf1.len1)
{
if(strcmp(sm,lhf1.LHFrec1[j].bname)==0)
{ k=j;break;}
j++;
}
if(k!=0)
{
df.BookDbase[i].namenext=lhf1.LHFrec1[k].lhead;
lhf1.LHFrec1[k].lhead=i; /*i为主文件的当前记录号*/
lhf1.LHFrec1[k].RecNum++;
}
else
{
m=++lhf1.len1; /*索引关键字个数加1*/
df.BookDbase[i].namenext=0; /*用头插法建立链表,指针指空*/
lhf1.LHFrec1[m].lhead=i;/*i为主文件的当前记录号*/
lhf1.LHFrec1[m].RecNum=1; /*计数器置1*/
strcpy(lhf1.LHFrec1[m].bname,sm);
}
}
void ChangeLinkHeadF2(BookDbaseFile df,LHFile2 lhf2)
{
int i,j,k,m;
char zz[8];
i=df.len;
strcpy(zz,df.BookDbase[i].author);
j=1;k=0;
while(j<=lhf2.len2)
{
if(strcmp(zz,lhf2.LHFrec2[j].author)==0)
{k=j;break;}
j++;
}
if(k!=0)
{
df.BookDbase[i].authnext=lhf2.LHFrec2[k].lhead;
lhf2.LHFrec2[k].lhead=i;
lhf2.LHFrec2[k].RecNum++;
}
else
{
m=++lhf2.len2;
df.BookDbase[i].authnext=0;
lhf2.LHFrec2[m].lhead=i;
lhf2.LHFrec2[m].RecNum=1;
strcpy(lhf2.LHFrec2[m].author,zz);
}
}
void ChangeLinkHeadF3(BookDbaseFile df,LHFile3 lhf3)
{
int i,j,k,m;
char cbs[10];
i=df.len;
strcpy(cbs,df.BookDbase[i].press);
j=1;k=0;
while(i<=lhf3.len3)
{
if(strcmp(cbs,lhf3.LHFrec3[j].press)==0)
{ k=j;break;}
j++;
}
if(k!=0)
{
df.BookDbase[i].prenext=lhf3.LHFrec3[k].lhead;
lhf3.LHFrec3[k].lhead=i;
lhf3.LHFrec3[k].RecNum++;
}
else
{
m=++lhf3.len3;
df.BookDbase[i].prenext=0;
lhf3.LHFrec3[m].lhead=i;
lhf3.LHFrec3[m].RecNum=1;
strcpy(lhf3.LHFrec3[m].press,cbs);
}
}
CreateBook(BookDbaseFile bf,BnoIdxFile bif,LHFile1 f1,LHFile2 f2,LHFile3 f3)
{
char yn='y';
while(yn=='y'||yn=='y')
{
AppeDBaseRec(bf);
ChangeBnoIdxF(bf,bif);
ChangeLinkHeadF1(bf,f1);
ChangeLinkHeadF2(bf,f2);
ChangeLinkHeadF3(bf,f3);
printf("ji xu shu ru ma? y/n: \n");
scanf("%c",&yn);
}
}
int BinSearch(BnoIdxFile bif,char key[])
{
int low,high,mid;
low=1;
high=bif.len;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(key,bif.BnoIdx[mid].bno)==0)
return bif.BnoIdx[mid].RecNo;
else if(strcmp(key,bif.BnoIdx[mid].bno)<0)
high=mid-1;
else low=mid+1;
}
return 0;
}
int BnameFind(LHFile1 lhf1,char key[])
{
int i,k=0;
for(i=1;i<=lhf1.len1;i++)
{
if(strcmp(key,lhf1.LHFrec1[i].bname)==0)
{ k=lhf1.LHFrec1[i].lhead;break;}
}
return k;
}
int BauthFind(LHFile2 lhf2,char key[])
{
int i,k=0;
for(i=1;i<=lhf2.len2;i++)
{
if(strcmp(key,lhf2.LHFrec2[i].author)==0)
{ k=lhf2.LHFrec2[i].lhead;break;}
}
return k;
}
int BpressFind(LHFile3 lhf3,char key[])
{
int i,k=0;
for(i=1;i<=lhf3.len3;i++)
{
if(strcmp(key,lhf3.LHFrec3[i].press)==0)
{ k=lhf3.LHFrec3[i].lhead;break;}
}
return k;
}
void ShowRec(BookDbaseFile df,int i)
{
printf("shu hao shu ming zou zhe chubanshe fenleihao\n");
printf("==========================================================\n");
printf("%-6s%20s",df.BookDbase[i].bno,df.BookDbase[i].bname);
printf("%8s%10s",df.BookDbase[i].author,df.BookDbase[i].press);
printf("%-6s\n",df.BookDbase[i].sortno);
printf("==========================================================\n");
}
void SearchBook(BookDbaseFile df,BnoIdxFile bif,LHFile1 f1,LHFile2 f2,LHFile3 f3)
{
char sh[4],sm[20],zz[8],cbs[10];
int i,j,k,choose=1;
while(choose>=1&&choose<=5)
{ printf("tu shu cha xun zi xi tong\n");
printf("-------------------------\n");
printf("1. shu hao");
printf("2. shu ming");
printf("3. zou zhe");
printf("4. chu ban she");
printf("5. tui chu");
printf("-------------------------\n");
printf("qing ni xuan zhe chao zuo");
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("shu ru shu hao:\n");scanf("%s",sh);
k=BinSearch(bif,sh);
if(k==0)
{ printf("mei you yao cha zhao de tu shu,jian cha shu ru shi fou you
cou\n");
break;
}
ShowRec(df,k);
break;
case 2:
printf("shu ru shu ming:\n");scanf("%s",sm);
k=BnameFind(f1,sm);
if(k==0)
{
printf("mei you yao cha zhao de tu shu,jian cha shu ru shi fou you
cuo\n");
break;
}
for(i=k;i;i=df.BookDbase[i].namenext)
ShowRec(df,i);
break;
case 3:
printf("shu ru zou zhe: \n");scanf("%s",zz);
k=BauthFind(f2,zz);
if(k==0)
{
printf("mei you yao cha zhao de shu,jian cha shi fou shu ru you
cuo\n");
break;
}
for(i=k;i;i=df.BookDbase[i].authnext)
ShowRec(df,i);
break;
case 4:
printf("shu ru chu ban she:\n");scanf("%s",cbs);
k=BpressFind(f3,cbs);
if(k==0)
{
printf("mei you yao cha zhao de tu shu,jian cha shi fou shu ru you
cuo\n");
break;
}
for(i=k;i;i=df.BookDbase[i].prenext)
ShowRec(df,i);
break;
case 5: return;
}
}
}
void BorrowBook(BookDbaseFile bf,BnoIdxFile bif,BbookFile bbf,ReadFile rf)
{
char dzh[8],sh[4],jyrq[8];
int i,j,k=0;
printf("shu ru du zhe hao shu hao jie yue ri qi\n");
scanf("%s%s%s",dzh,sh,jyrq);
for(i=1;i<=rf.len;i++)
if(strcmp(dzh,rf.ReadRec[i].rno)==0)
{ k=i;break;}
if(k==0)
{
printf("fei fa du zhe!\n"); return;}
if(rf.ReadRec[k].bn2>=rf.ReadRec[k].bn1)
{ printf("shu yi jie man!\n"); return;}
j=BinSearch(bif,sh);
if(j==0) { printf("fei fa shu hao!\n"); return;}
if(bf.BookDbase[j].borrownum>=bf.BookDbase[j].storenum)
{ printf("tu shu yi jie chu\n"); return;}
i=++bbf.len;
strcpy(bbf.Bbook[i].rno,dzh);
strcpy(bbf.Bbook[i].bno,sh);
strcpy(bbf.Bbook[i].date1,jyrq);
rf.ReadRec[k].bn2++;
bf.BookDbase[j].borrownum++;
printf("jie shu cheng gong!");
}
void BackBook(BookDbaseFile bf,BnoIdxFile bif,BbookFile bbf,ReadFile rf)
{
char dzh[8],sh[4],hsrq[8];
int i,j,k=0,m=0;
printf("shu ru du zhe hao shu hao huan shu ri qi \n");
scanf("%s%s%s",dzh,sh,hsrq);
for (i=1;i<=rf.len;i++)
if(strcmp(dzh,rf.ReadRec[i].rno)==0)
{k=i;break;}
if(k==0) { printf("fei fa du zhe!\n"); return;}
for(i=1;i<=bbf.len;i++)
if(strcmp(sh,bbf.Bbook[i].bno)==0)
{m=i;break;}
if(m==0) { printf("fei fa shu hao!\n"); return;}
j=BinSearch(bif,sh);
if(j==0) { printf("fei fa shu hao!\n"); return;}
rf.ReadRec[k].bn2--;
bf.BookDbase[j].borrownum--;
strcpy(bbf.Bbook[m].date2,hsrq);
printf("huan shu cheng gong!\n");
}
void ReaderManage(ReadFile rf)
{
int i;
char yn='y';
i=++rf.len;
while(yn=='y'||yn=='y')
{
printf("shu ru du zhe hao du zhe ming ke jie shu shu\n");
scanf("%s%s",rf.ReadRec[i].rno,rf.ReadRec[i].name);
scanf("%d",&rf.ReadRec[i].bn1);
printf("ji xu shu ru ma? y/n: \n");
scanf("%c",&yn);i++;
}
rf.len=i-1;
}
void writefile(BookDbaseFile bf,BnoIdxFile bif,LHFile1 f1,LHFile2 f2,LHFile3 f3,ReadFile
rf,BbookFile bbf)
{
FILE *fp;int i;
fp=fopen("book","wb");
for(i=1;i<=bf.len;i++)
fwrite(&bf.BookDbase[i],sizeof(BookRecType),1,fp);
fclose(fp);
fp=fopen("bidx","wb");
for(i=1;i<=bif.len;i++)
fwrite(&bif.BnoIdx[i],sizeof(BidxRecType),1,fp);
fclose(fp);
fp=fopen("nidx","wb");
for(i=1;i<=f1.len1;i++)
fwrite(&f1.LHFrec1[i],sizeof(BNRecType),1,fp);
fclose(fp);
fp=fopen("aidx","wb");
for(i=1;i<=f2.len2;i++)
fwrite(&f2.LHFrec2[i],sizeof(BARecType),1,fp);
fclose(fp);
fp=fopen("pidx","wb");
for(i=1;i<=f3.len3;i++)
fwrite(&f3.LHFrec3[i],sizeof(BPRecType),1,fp);
fclose(fp);
fp=fopen("read","wb");
for(i=1;i<=rf.len;i++)
fwrite(&rf.ReadRec[i],sizeof(RRecType),1,fp);
fclose(fp);
fp=fopen("bbff","wb");
for(i=1;i<=bbf.len;i++)
fwrite(&bbf.Bbook[i],sizeof(BbookRecType),1,fp);
fclose(fp);
}
void readfile(BookDbaseFile bf,BnoIdxFile bif,LHFile1 f1,LHFile2 f2,LHFile3 f3,ReadFile
rf,BbookFile bbf)
{
FILE *fp;int i;
fp=fopen("book","rb");
i=1;
while(!feof(fp))
{
fread(&bf.BookDbase[i],sizeof(BookRecType),1,fp);
i++;if(feof(fp))break;
}
bf.len=i-1;fclose(fp);
fp=fopen("bidx","rb");
i=1;
while(!feof(fp))
{
fread(&bif.BnoIdx[i],sizeof(BidxRecType),1,fp);
i++;
}
bif.len=i-1;fclose(fp);
fp=fopen("nidx","rb");
i=1;
while(!feof(fp))
{
fread(&f1.LHFrec1[i],sizeof(BNRecType),1,fp);
i++;
}
f1.len1=i-1;fclose(fp);
fp=fopen("aidx","rb");
i=1;
while(!feof(fp))
{
fread(&f2.LHFrec2[i],sizeof(BARecType),1,fp);
i++;
}
f2.len2=i-1;fclose(fp);
fp=fopen("pidx","rb");
i=1;
while(!feof(fp))
{
fread(&f3.LHFrec3[i],sizeof(BPRecType),1,fp);
i++;
}
f3.len3=i-1;fclose(fp);
fp=fopen("read","rb");
i=1;
while(!feof(fp))
{
fread(&rf.ReadRec[i],sizeof(RRecType),1,fp);
i++;
}
rf.len=i-1;fclose(fp);
fp=fopen("bbff","rb");
i=1;
while(!feof(fp))
{
fread(&bbf.Bbook[i],sizeof(BbookRecType),1,fp);
i++;
}
bbf.len=i-1;fclose(fp);
}
void main()
{
int i,j,m,k=1;
char xz='n';
BookDbaseFile bf;
BnoIdxFile bif;
LHFile1 f1;LHFile2 f2;LHFile3 f3;
ReadFile rf;BbookFile bbf;
rf.len=0;
while(k<=5)
{
printf("tu shu guan li xi tong\n");
printf("======================\n");
printf("1. xi tong wei hu\n");
printf("2. du zhe guan li\n");
printf("3. tu shu guan li\n");
printf("4. tu shu liu tong\n");
printf("5. tui chu xi tong\n");
printf("======================\n");
printf("qing xuan zhe 1-5:\n");
scanf("%d",&k);
switch(k)
{
case 1:
printf("xi tong wei hu\n");
printf("--------------\n");
printf("1. chu shi hua\n");
printf("2. du pan\n");
printf("--------------\n");
printf("qing xuan zhe:\n");
scanf("%d",&m);
switch(m)
{
case 1:
printf("zhi neng yi ci chu shi hua y/n:\n");
scanf("%c",&xz);
if(xz=='y'||xz=='y')
{
bf.len=bif.len=f1.len1=f2.len2=0;
f3.len3=rf.len=bbf.len=0;
}
break;
case 2: readfile(bf,bif,f1,f2,f3,rf,bbf);
break;
}
break;
case 2: ReaderManage(rf);
break;
case 3: printf("tu shu guan li zi xi tong\n");
printf("-------------------------\n");
printf("1. tu shu cai bian ru ku\n");
printf("2. tu shu xin xi cha xun\n");
printf("-------------------------\n");
scanf("%d",&j);
if(j==1)
CreateBook(bf,bif,f1,f2,f3);
else
SearchBook(bf,bif,f1,f2,f3);
break;
case 4: printf("tu shu liu tong zi xi tong\n");
printf("--------------------------\n");
printf("1. jie shu chu li\n");
printf("2. huan shu chu li\n");
printf("--------------------------\n");
printf("qing xuan zhe\n");
scanf("%d",&j);
if(j==1)
BorrowBook(bf,bif,bbf,rf);
else if(j==2)
BackBook(bf,bif,bbf,rf);
break;
case 5: printf(" xi tong zheng zai xie pan, shao deng...\n");
writefile(bf,bif,f1,f2,f3,rf,bbf);
printf("zai jian!\n");
return;
}
}
}