用C语言写一个图书借阅系统,包括查看图书,添加图书,删除图书,修改图书,借阅图书,归还图书等功能
时间: 2024-03-22 17:36:49 浏览: 17
好的,我可以为您提供一些基本的代码结构和函数,但是由于时间和篇幅的限制,我无法提供完整的代码。您可以根据以下代码进行修改和完善。
首先,我们需要定义一些结构体来表示图书信息和借阅信息:
```c
struct Book {
char title[100];
char author[50];
char publisher[50];
int ISBN;
int num;
int borrowed;
};
struct Borrower {
char name[50];
int bookID;
char borrowDate[20];
char returnDate[20];
};
```
然后,我们可以定义一些函数来实现各种功能。比如,添加图书功能可以使用以下代码:
```c
void addBook(struct Book book, struct Book bookList[], int *bookCount) {
bookList[*bookCount] = book;
(*bookCount)++;
printf("添加图书成功!\n");
}
```
删除图书功能可以使用以下代码:
```c
void deleteBook(int bookID, struct Book bookList[], int *bookCount) {
int i, flag = 0;
for (i = 0; i < *bookCount; i++) {
if (bookList[i].ISBN == bookID) {
flag = 1;
break;
}
}
if (flag) {
for (; i < *bookCount - 1; i++) {
bookList[i] = bookList[i + 1];
}
(*bookCount)--;
printf("删除图书成功!\n");
} else {
printf("没有找到该图书!\n");
}
}
```
查看图书功能可以使用以下代码:
```c
void viewBooks(struct Book bookList[], int bookCount) {
int i;
printf("图书编号\t书名\t作者\t出版社\t数量\t已借出\n");
for (i = 0; i < bookCount; i++) {
printf("%d\t%s\t%s\t%s\t%d\t%d\n", bookList[i].ISBN, bookList[i].title, bookList[i].author, bookList[i].publisher, bookList[i].num, bookList[i].borrowed);
}
}
```
修改图书功能可以使用以下代码:
```c
void modifyBook(int bookID, struct Book bookList[], int bookCount) {
int i, flag = 0;
for (i = 0; i < bookCount; i++) {
if (bookList[i].ISBN == bookID) {
flag = 1;
printf("请输入修改后的书名:");
scanf("%s", bookList[i].title);
printf("请输入修改后的作者:");
scanf("%s", bookList[i].author);
printf("请输入修改后的出版社:");
scanf("%s", bookList[i].publisher);
printf("请输入修改后的数量:");
scanf("%d", &bookList[i].num);
break;
}
}
if (flag) {
printf("修改图书成功!\n");
} else {
printf("没有找到该图书!\n");
}
}
```
借阅图书功能可以使用以下代码:
```c
void borrowBook(char name[], int bookID, struct Book bookList[], int bookCount, struct Borrower borrowerList[], int *borrowerCount) {
int i, flag = 0;
for (i = 0; i < bookCount; i++) {
if (bookList[i].ISBN == bookID) {
flag = 1;
if (bookList[i].num > bookList[i].borrowed) {
bookList[i].borrowed++;
struct Borrower borrower;
strcpy(borrower.name, name);
borrower.bookID = bookID;
time_t t = time(NULL);
strftime(borrower.borrowDate, sizeof(borrower.borrowDate), "%Y-%m-%d", localtime(&t));
strftime(borrower.returnDate, sizeof(borrower.returnDate), "%Y-%m-%d", localtime(&t + 7 * 24 * 60 * 60));
borrowerList[*borrowerCount] = borrower;
(*borrowerCount)++;
printf("借阅图书成功!\n");
} else {
printf("该图书已全部借出!\n");
}
break;
}
}
if (!flag) {
printf("没有找到该图书!\n");
}
}
```
归还图书功能可以使用以下代码:
```c
void returnBook(int bookID, struct Book bookList[], int bookCount, struct Borrower borrowerList[], int *borrowerCount) {
int i, flag = 0;
for (i = 0; i < *borrowerCount; i++) {
if (borrowerList[i].bookID == bookID) {
flag = 1;
int j;
for (j = 0; j < bookCount; j++) {
if (bookList[j].ISBN == bookID) {
bookList[j].borrowed--;
break;
}
}
for (; i < *borrowerCount - 1; i++) {
borrowerList[i] = borrowerList[i + 1];
}
(*borrowerCount)--;
printf("归还图书成功!\n");
break;
}
}
if (!flag) {
printf("没有找到该借阅记录!\n");
}
}
```
请根据您的需要进行修改和完善。