图书借阅系统(C语言版)

记录一下,帮学妹开发了一个图书借阅系统,需求如下

  1. 录入图书信息
  2. 查询图书信息
  3. 录入读者信息
  4. 查询读者信息
  5. 借书
  6. 还书
  7. 删除图书信息
  8. 删除读者信息
  9. 查询所有图书信息
  10. 查询所有读者信息
#include <stdio.h>
#include <string.h>

// 定义图书结构体
struct Book {
    char bookNumber[20];
    char bookName[50];
    char author[50];
    int totalCopies;
    int availableCopies;
};

// 定义读者结构体
struct Reader {
    char readerName[50];
    char readerNumber[20];
    char department[50];
};

// 定义借阅信息结构体
struct BorrowInfo {
    char bookNumber[20];
    char readerNumber[20];
    char borrowDate[20];
    char returnDate[20];
};

// 函数原型
void addBook(struct Book books[], int *bookCount);
void findBookByNumber(struct Book books[], int bookCount, char bookNumber[]);
void addReader(struct Reader readers[], int *readerCount);
void findReaderByName(struct Reader readers[], int readerCount, char readerName[]);
void borrowBook(struct Book books[], int bookCount, struct Reader readers[], int readerCount, struct BorrowInfo borrowInfo[], int *borrowCount);
void returnBook(struct Book books[], int bookCount, struct BorrowInfo borrowInfo[], int borrowCount);
void deleteBook(struct Book books[], int *bookCount, char bookNumber[]);
void deleteReader(struct Reader readers[], int *readerCount, char readerNumber[]);
void selectAllBook(struct Book books[], int bookCount, struct BorrowInfo borrowInfo[], int borrowCount);
void selectAllReader(struct Reader readers[], int readerCount);


// 在main函数外定义全局变量
int totalCopies = 0; // 初始化图书馆总册数 
int availableCopies = 0; // 始化图书馆在册数 

int main() {
    struct Book books[100];
    struct Reader readers[100];
    struct BorrowInfo borrowInfo[100];
    int bookCount = 0;
    int readerCount = 0;
    int borrowCount = 0;
    int choice;
    char readerName[100];
    char inputBookNumber[20];
    char readerNumber[20];
    

    do {
        printf("\n图书借阅系统\n");
        printf("1. 录入图书信息\n");
        printf("2. 查询图书信息\n");
        printf("3. 录入读者信息\n");
        printf("4. 查询读者信息\n");
        printf("5. 借书\n");
        printf("6. 还书\n");
        printf("7. 删除图书信息\n");
        printf("8. 删除读者信息\n");
        printf("9. 查询所有图书信息\n");
        printf("10. 查询所有读者信息\n");
        printf("11. 退出\n");
        printf("请选择操作:");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addBook(books, &bookCount);
                break;
            case 2:
            	printf("请输入要查找的图书书号:");
                scanf("%s", inputBookNumber);
                findBookByNumber(books, bookCount, inputBookNumber);
                break;
            case 3:
                addReader(readers, &readerCount);
                break;  
		    case 4:
		    	printf("请输入要查找的读者姓名:");
                scanf("%s", readerName);
                findReaderByName(readers, readerCount, readerName);
                break;  
            case 5:
                borrowBook(books, bookCount, readers, readerCount, borrowInfo, &borrowCount);
                break;
            case 6:
                returnBook(books, bookCount, borrowInfo, borrowCount);
                break;
            case 7:
			    printf("请输入要删除的图书书号:");
			    scanf("%s", inputBookNumber);
			    deleteBook(books, &bookCount, inputBookNumber);
			    break;
			case 8:
			    printf("请输入要删除的读者号:");
			    scanf("%s", readerNumber);
			    deleteReader(readers, &readerCount, readerNumber);
			    break;	
			case 9:
			    printf("查询所有图书信息:\n");
			    selectAllBook(books, bookCount, borrowInfo, borrowCount);
			    break;
			case 10:
			    printf("查询所有读者信息:\n");
			    selectAllReader(readers, readerCount); 
			    break;      
            case 11:
                printf("感谢使用图书借阅系统!\n");
                break;    
            default:
                printf("请输入有效的操作选项!\n");
        }
    } while (choice != 11);

    return 0;
}

 实现录入图书信息的逻辑
//void addBook(struct Book books[], int *bookCount) {
//    printf("请输入书号:");
//    scanf("%s", books[*bookCount].bookNumber);
//    printf("请输入书名:");
//    scanf("%s", books[*bookCount].bookName);
//    printf("请输入作者:");
//    scanf("%s", books[*bookCount].author);
//    printf("请输入图书册数:");
//    scanf("%d", &books[*bookCount].totalCopies);
//    books[*bookCount].availableCopies = books[*bookCount].totalCopies;
//    (*bookCount)++;
//    printf("图书信息录入成功!\n");
//    
//}

// 实现录入图书信息的逻辑
void addBook(struct Book books[], int *bookCount) {
    printf("请输入书号:");
    scanf("%s", books[*bookCount].bookNumber);
    printf("请输入书名:");
    scanf("%s", books[*bookCount].bookName);
    printf("请输入作者:");
    scanf("%s", books[*bookCount].author);
    books[*bookCount].totalCopies = totalCopies; // 使用全局变量
    books[*bookCount].availableCopies = availableCopies;
    (*bookCount)++;
    totalCopies++;
    availableCopies++; 
    printf("图书信息录入成功!\n");
    
}


// 按书号查找图书信息的方法
void findBookByNumber(struct Book books[], int bookCount, char bookNumber[]) {
    int bookIndex = -1;
    int i;
    for (i = 0; i < bookCount; i++) {
        if (strcmp(books[i].bookNumber, bookNumber) == 0) {
            bookIndex = i;
            break;
        }
    }
    if (bookIndex != -1) {
        printf("书号: %s, 书名: %s, 作者: %s, 总册数: %d, 在馆册数: %d\n",
               books[bookIndex].bookNumber, books[bookIndex].bookName, books[bookIndex].author,
               totalCopies, availableCopies);
    } else {
        printf("未找到对应的图书信息\n");
    }
}


// 实现录入读者信息的逻辑
void addReader(struct Reader readers[], int *readerCount) {
    printf("请输入读者姓名:");
    scanf("%s", readers[*readerCount].readerName);
    printf("请输入读者号:");
    scanf("%s", readers[*readerCount].readerNumber);
    printf("请输入院系:");
    scanf("%s", readers[*readerCount].department);
    (*readerCount)++;
    printf("读者信息录入成功!\n");
}

// 按读者姓名查找读者信息的方法
void findReaderByName(struct Reader readers[], int readerCount, char readerName[]) {
    int readerIndex = -1;
    int i;
    for (i = 0; i < readerCount; i++) {
        if (strcmp(readers[i].readerName, readerName) == 0) {
            readerIndex = i;
            break;
        }
    }
    if (readerIndex != -1) {
        printf("读者姓名: %s, 读者号: %s, 院系: %s\n",
               readers[readerIndex].readerName, readers[readerIndex].readerNumber, readers[readerIndex].department);
    } else {
        printf("未找到对应的读者信息\n");
    }
}

// 实现借书功能的逻辑
void borrowBook(struct Book books[], int bookCount, 
				struct Reader readers[], int readerCount,
  				struct BorrowInfo borrowInfo[], int *borrowCount) {
    char bookNumber[20];
    char readerNumber[20];
    int bookIndex = -1;
    int readerIndex = -1;
    int i;
    int j;

    printf("请输入书号:");
    scanf("%s", bookNumber);
    printf("请输入读者号:");
    scanf("%s", readerNumber);

    // 查找图书和读者
    for (i = 0; i < bookCount; i++) {
        if (strcmp(books[i].bookNumber, bookNumber) == 0) {
            bookIndex = i;
            break;
        }
    }
    for (j = 0; j < readerCount; j++) {
        if (strcmp(readers[j].readerNumber, readerNumber) == 0) {
            readerIndex = j;
            break;
        }
    }

    if (bookIndex != -1 && readerIndex != -1) {
        if (availableCopies > 0) {
        	
        	time_t now;
            time(&now);
            struct tm *local = localtime(&now);
            char borrowDate[20]; // 添加借阅日期属性
            strftime(borrowDate, 20, "%Y-%m-%d", local);
            
            printf("借阅成功!\n");
            strcpy(borrowInfo[*borrowCount].bookNumber, bookNumber);
            strcpy(borrowInfo[*borrowCount].readerNumber, readerNumber);
            strcpy(borrowInfo[*borrowCount].borrowDate, borrowDate); // 记录借阅日期
            availableCopies--;
            (*borrowCount)++;
            
        } else {
            printf("该图书无可借阅册数!\n");
        }
    } else {
        printf("未找到对应的图书或读者!\n");
    }
}


// 实现还书功能的逻辑
void returnBook(struct Book books[], int bookCount, struct BorrowInfo borrowInfo[], int borrowCount) {
    char bookNumber[20];
    int bookIndex = -1;
    int i;
    int j;
    int k;

    printf("请输入书号:");
    scanf("%s", bookNumber);

    // 查找借阅记录
    for (i = 0; i < borrowCount; i++) {
        if (strcmp(borrowInfo[i].bookNumber, bookNumber) == 0) {
            bookIndex = i;
            break;
        }
    }

    if (bookIndex != -1) {
        // 找到对应的借阅记录,更新图书信息
        for (j = 0; j < bookCount; j++) {
            if (strcmp(books[j].bookNumber, bookNumber) == 0) {
                books[j].availableCopies++;
                availableCopies++;
                break;
            }
        }
        
        // 记录还书日期
        time_t now;
        time(&now);
        struct tm *local = localtime(&now);
        char returnDate[20];
        strftime(returnDate, 20, "%Y-%m-%d", local);
        strcpy(borrowInfo[bookIndex].returnDate, returnDate); // 记录还书日期

        // 删除借阅记录
        for (k = bookIndex; k < borrowCount - 1; k++) {
            borrowInfo[k] = borrowInfo[k + 1];
        }
        borrowCount--;

        printf("还书成功!\n");
    } else {
        printf("未找到对应的借阅记录!\n");
    }
}


// 删除图书信息的方法
void deleteBook(struct Book books[], int *bookCount, char bookNumber[]) {
    int bookIndex = -1;
    int i;
    for (i = 0; i < *bookCount; i++) {
        if (strcmp(books[i].bookNumber, bookNumber) == 0) {
            bookIndex = i;
            break;
        }
    }
    if (bookIndex != -1) {
        // 将要删除的图书信息后面的信息向前移动
        for (i = bookIndex; i < *bookCount - 1; i++) {
            books[i] = books[i + 1];
        }
        (*bookCount)--;
        totalCopies--;
        availableCopies--;
        printf("图书信息删除成功!\n");
    } else {
        printf("未找到对应的图书信息\n");
    }
}

// 删除读者信息的方法
void deleteReader(struct Reader readers[], int *readerCount, char readerNumber[]) {
    int readerIndex = -1;
    int i;
    for (i = 0; i < *readerCount; i++) {
        if (strcmp(readers[i].readerNumber, readerNumber) == 0) {
            readerIndex = i;
            break;
        }
    }
    if (readerIndex != -1) {
        // 将要删除的读者信息后面的信息向前移动
        for (i = readerIndex; i < *readerCount - 1; i++) {
            readers[i] = readers[i + 1];
        }
        (*readerCount)--;
        printf("读者信息删除成功!\n");
    } else {
        printf("未找到对应的读者信息\n");
    }
}

// 查询所有图书 
void selectAllBook(struct Book books[], int bookCount, struct BorrowInfo borrowInfo[], int borrowCount) {
	int i; 
    for (i = 0; i < bookCount; i++) {
        printf("书号: %s, 书名: %s, 作者: %s\n",
               books[i].bookNumber, books[i].bookName, books[i].author,
               books[i].totalCopies, books[i].availableCopies);
               
        // 查找对应的借阅记录并显示借阅日期和还书日期
        int j;
        for (j = 0; j < borrowCount; j++) {
            if (strcmp(books[i].bookNumber, borrowInfo[j].bookNumber) == 0) {
                printf("书号: %s的借书日期: %s, 还书日期: %s\n", books[i].bookNumber, borrowInfo[j].borrowDate, borrowInfo[j].returnDate);
                break;
            }
        }
    }
       printf("总册数: %d, 在馆册数: %d\n",
               totalCopies, availableCopies);
}

// 查询所有读者 
void selectAllReader(struct Reader readers[], int readerCount) {
	int i;
    for (i = 0; i < readerCount; i++) {
        printf("读者姓名: %s, 读者号: %s, 院系: %s\n",
               readers[i].readerName, readers[i].readerNumber, readers[i].department);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值