From c903f0f79be8a6cd469c4641f0b34943cbfbb19a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E8=89=BA=E5=93=B2?= <446062334@qq.com> Date: Sun, 21 May 2023 16:12:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d44b52a..795601c 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,58 @@ Reader* LendBook(Reader* reader1, Book* book1) 函数原形:void returnBook(Reader* reader, Book* book); 功能:接受两个形参:读者链表地址和图书链表地址,用读者的编号进行匹配,找到该读者后,用图书的编号进行匹配,进行还书,否则返回 ``` - +void returnBook(Reader* reader1, Book* book1) +{ + Reader* reader = reader1; + Book* book = book1; + printf("请输入读者的id:"); + int id; + scanf("%d", &id); + getchar(); + + if (reader != NULL) + { + while (reader != NULL) + { + if (id == reader->iNum) + { + printf("请输入要还的书的编号:"); + int id_book; + scanf("%d", &id_book); + getchar(); + for (int i = 0; i < reader->iMax; i++) + { + if (reader->aiBookId[i] == id_book) + { + reader->aiBookId[i] = 0; + while (book) + { + if (id_book == book->iNum) + { + book->iAmount++; + printf("还书成功!\n"); + printf("按任意键返回\n"); + getchar(); + return reader1; + } + book = book->next; + } + } + } + printf("没有找到该图书,检查图书的Id\n"); + + printf("按任意键返回\n"); + getchar(); + return reader1; + } + reader = reader->next; + } + printf("没有找到该读者,检查读者id是否输入有误\n"); + printf("按任意键返回\n"); + getchar(); + return reader1; + } +} ``` @@ -363,9 +414,35 @@ Reader* LendBook(Reader* reader1, Book* book1) 1)函数原形: void save(Book* book); 2) 功能:接受一个形参:book的链表地址,新建一个文件,将链表中的信息保存到硬盘中 ``` - - - +void save(Book* book1) +{ + FILE* fp; + Book* pCur = book1; + int iCount = 0; + + if (pCur == NULL) + { + printf("\n没有学生记录!\n"); + return; + } + + if ((fp = fopen("book.txt", "wb")) == NULL) + { + printf("创建文件失败!\n"); + getchar(); + exit(1); + } + while (pCur) + { + fwrite(pCur, sizeof(Book), 1, fp); + pCur = pCur->next; + iCount++; + } + printf("\n"); + + printf("保存文件的数据数目为:%d\n", iCount); + fclose(fp); +} ``` ## C12:读取信息模块 @@ -373,6 +450,33 @@ Reader* LendBook(Reader* reader1, Book* book1) 1)函数原形: Book* read1(); 2) 功能:打开一个文件,将文件中的信息读取到内存中,并返回一个Book类型的指针 ``` +Book* read1() +{ + FILE* fp; + Book* pHead = NULL, * pTemp = NULL, * pCur = NULL; + + if ((fp = fopen("book.txt", "r")) == NULL) + { + printf("\n文件打开失败!请检查文件名!\n"); + exit(0); + } + pTemp = (Book*)malloc(sizeof(Book)); + while (fread(pTemp, sizeof(Book), 1, fp)) + { + if (!pHead) + { + pHead = pCur = pTemp; + } + else + { + pCur->next = pTemp; + pCur = pTemp; + } + pTemp = (Book*)malloc(sizeof(Book)); + } + fclose(fp); + return pHead; +} ```