帮我看看借阅函数那串代码哪里出了问题,谢谢!
#include <stdio.h>
#include <malloc.h>
#define Max_Books 100
typedef int KeyType;
typedef struct//书籍结构体
{
KeyType no; //书号
char bookname[50]; //书名
char author[50]; //作者
int currentCount; //现存量
int totalCount; //库存量
}book;
typedef struct//图书馆结构体
{
book data[Max_Books];
int count; //藏书量
}Library;
typedef struct//借阅者结构体
{
book data[100];
int card; //借阅者书证号
int time_year; //归还时间
int time_month;
int time_day;
int length;
}Borrower;
//初始化图书馆
void initLibrary(Library *&L)
{
L=(Library*)malloc(sizeof(Library));
L->count=0;
}
void initBorrower(Borrower *&B)
{
B=(Borrower*)malloc(sizeof(Borrower));
B->length=0;
}
//查找书籍索引
int findBookIndex(Library *L,KeyType no)
{
int i;
for(i=0;i<L->count;i++)
{
if(L->data[i].no==no)
return i;
}
return -1;
}
//采编入库
void addBook(Library *&L,KeyType no)
{ book newbook;int i;
if(L->count+1>Max_Books)
{ printf("图书馆已满,无法添加新的书籍!");
return;
}
i=findBookIndex(L,no);//在图书馆查找有无这本书籍
if(i==-1)//若图书馆没有这本书
{ i=L->count; //i为图书馆新添加书籍所在的物理位序
L->data[i].no=no;
printf("请输入书籍名称:");
scanf("%s",L->data[i].bookname);
printf("请输入作者名字:");
scanf("%s",L->data[i].author);
L->data[i].currentCount=1;
L->data[i].totalCount=1;
L->count++;//最后图书馆藏书量+1
}
else
{
L->data[i].currentCount++;//在图书馆找到该书籍,只需要现存量和库存量+1
L->data[i].totalCount++;
}
printf("《%s》已采编入库完成!",L->data[i].bookname);
}
//借阅
void borrowBook(Library *&L,KeyType no,Borrower *&B)
{ int i;
i=findBookIndex(L,no);
if(i==-1)
{
printf("库存不足,无法借阅!\n");
return;
}
else
{ i=i-1;//转化为物理位序
L->data[i].currentCount--;//在图书馆找到该书籍,借出去现存量-1
printf("请输入借阅者书证号:");//登记借阅人书证号和归还期限
scanf("%d",&B->data);
printf("请输入归还日期:(年-月-日)");
scanf("%d-%d-%d",&B->time_year,&B->time_month,&B->time_day);
B->length++;
printf("《%s》借阅成功!",B->data[i].bookname);
return;
}
}
int main()
{
Library *L;int n;KeyType no;Borrower *B;
initLibrary(L);
initBorrower(B);
scanf("%d",&no);
addBook(L,no);
scanf("%d",&no);
borrowBook(L,no,B);
return 0;
}
修改完善如下,供参考:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define Max_Books 100
#define Max_Borw 50
typedef int KeyType;
typedef struct//书籍结构体
{
KeyType no; //书号
char bookname[50]; //书名
char author[50]; //作者
int currentCount; //现存量
int totalCount; //库存量
}book;
typedef struct//图书馆结构体
{
book data[Max_Books];
int count; //藏书量
}Library;
typedef struct borw {
KeyType no_data[10]; //book data[100]; 借阅者借书记录,即借阅的书号,上限 10 种/ 人
int card; //借阅者书证号
int time_year; //归还时间
int time_month;
int time_day;
int num; //借阅书本数量
}Borw;
typedef struct//借阅者结构体
{
Borw data[Max_Borw];
int length; // 借阅者人数
}Borrower;
//初始化图书馆
void initLibrary(Library*& L)
{
L = (Library*)malloc(sizeof(Library));
L->count = 0;
}
void initBorrower(Borrower*& B) // 初始化借阅者
{
B = (Borrower*)malloc(sizeof(Borrower));
B->length = 0;
memset(B->data, 0, sizeof(B->data));
}
//查找书籍索引
int findBookIndex(Library* L, KeyType no)
{
int i;
for (i = 0; i < L->count; i++)
{
if (L->data[i].no == no)
return i;
}
return -1;
}
//借阅者索引
int findBorrowIndex(Borrower* B, int card)
{
int i;
for (i = 0; i < B->length; i++)
if (B->data[i].card == card)
return i;
return -1;
}
//采编入库
void addBook(Library*& L, KeyType no)
{
book newbook; int i;
if (L->count + 1 > Max_Books)
{
printf("图书馆已满,无法添加新的书籍!");
return;
}
i = findBookIndex(L, no);//在图书馆查找有无这本书籍
if (i == -1)//若图书馆没有这本书
{
i = L->count; //i为图书馆新添加书籍所在的物理位序
L->data[i].no = no;
printf("请输入书籍名称:");
scanf("%s", L->data[i].bookname);
printf("请输入作者名字:");
scanf("%s", L->data[i].author);
L->data[i].currentCount = 1;
L->data[i].totalCount = 1;
L->count++;//最后图书馆藏书量+1
}
else
{
L->data[i].currentCount++;//在图书馆找到该书籍,只需要现存量和库存量+1
L->data[i].totalCount++;
}
printf("《%s》已采编入库完成!", L->data[i].bookname);
}
//借阅
void borrowBook(Library*& L, KeyType no, Borrower*& B)
{
int i, j, card;
i = findBookIndex(L, no);
if (i == -1)
{
printf("所借书籍不在书库中!\n");
//printf("库存不足,无法借阅!\n");
//return;
}
else{
do {
printf("请输入借阅者借书证号:");
scanf("%d", &card);
j = findBorrowIndex(B, card);
} while (j == -1);
if (B->data[j].num >= 10 || L->data[i].currentCount == 0)
printf("借阅者已借满 10 本书 或 书已借空!\n");
else {
L->data[i].currentCount--;//在图书馆找到该书籍,借出去现存量-1
B->data[j].no_data[B->data[j].num++] = L->data[i].no; // 登记书号, B->data[j].num++ 借书数量加 1
printf("请输入归还日期:(年 月 日)");
scanf("%d %d %d", &B->data[j].time_year, &B->data[j].time_month, &B->data[j].time_day);
printf("读者:%d,借阅书籍号:%d ,《%s》成功!", B->data[j].card, B->data[j].no_data[B->data[j].num - 1], L->data[i].bookname);
}
}
return;
}
int main()
{
Library* L;
Borrower* B;
int n;
KeyType no;
initLibrary(L);
scanf("%d", &no);
addBook(L, no);
initBorrower(B);
B->data[0].card = 123; // 建立一个借阅者信息
B->data[0].num = 0;
B->length++;
scanf("%d", &no);
borrowBook(L, no, B);
return 0;
}
你的图书信息都在Library结构中的data数组里,Browser结构中的data数组也没写数据进去啊
以程序员的方式撒狗粮,专业浪漫,值得拥有!