C语言课程设计——图书馆查询系统
生活随笔
收集整理的這篇文章主要介紹了
C语言课程设计——图书馆查询系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了方便圖書查詢,我們編寫了這個圖書館查詢系統,通過將圖書的信息輸入進這個系統,我們便可以直到圖書的相關情況。圖書管理員存入圖書信息,使用者可以通過選項實現查詢圖書借閱信息,借取圖書,歸還圖書,查看圖書熱度榜等操作。
#define _CRT_SECURE_NO_WARNINGS #include "stdio.h" #include "string.h" #include "windows.h" #include "stdlib.h" struct book {char book_name[20];char book_num[20];char date[11]="0000/00/00"; int flag=1;int heat=0; }; void search(struct book *a,char b[20],int n) {int t = 0;for (int i = 0; i < n; i++){if (strcmp(b, a[i].book_name) == 0){t++;printf("\n查找結果為:(圖書名稱 圖書編號 上次借出日期 狀態)\n%s %s %s ", a[i].book_name, a[i].book_num,a[i].date); //查找書籍數據if (a[i].flag==1){printf("該書未借出\n\n");}else{printf("該書已借出\n\n");}}}if (t==0){printf("沒有該圖書信息\n");} } void borrow(struct book *a,int n) {printf("請輸入您要借取的圖書編號:\n");char name[20];scanf("%s", name);int x = 0;for (int i = 0; i < n; i++){if (strcmp(name, a[i].book_num) == 0){if (a[i].flag==1){x++;a[i].flag = 0;a[i].heat++;printf("\n請填寫借出日期:(如2020/10/31)\n");scanf("%s", a[i].date);printf("\n恭喜你成功借閱!\n\n");}else{printf("\n這本書已經借出!\n\n");}}}if (x==0){printf("\n該書不存在!\n\n");} } void returnbook(struct book* a, int n) {printf("請輸入您要歸還的圖書編號:\n");char name[20];int t = 0;scanf("%s", name);for (int i = 0; i < n; i++){if (strcmp(name, a[i].book_num) == 0&&a[i].flag==0){t++;a[i].flag = 1;printf("\n恭喜你成功歸還!\n\n");}}if (t==0){printf("\n該書未借出或圖書編號錯誤!\n\n");} } void chart(struct book *a,int n) {for (int i = 0; i < n; i++){for (int j = 0; j < n-1; j++){if (a[j].heat<a[j+1].heat){struct book x = a[j+1];a[j + 1] = a[j];a[j] = x; //冒泡排序}}}for (int i = 0; i < 5; i++){printf("NO.%d\t%s\t%d\n\n", i+1, a[i].book_name, a[i].heat);} } void sure() {int x;printf("點擊1和回車鍵返回菜單\n");scanf("%d", &x);if (x==1){system("cls");} } void menu() {printf("-=======================================================================================================-\n");printf("| **********1.圖書庫***************** |\n");printf("| **********2.查詢圖書*************** |\n");printf("| **********3.借取圖書*************** |\n");printf("| **********4.歸還圖書*************** |\n");printf("| **********5.圖書熱度排行榜********* |\n");printf("| **********6.退出系統*************** |\n");printf("-=======================================================================================================-\n");} int main() {printf("==================================歡迎光臨圖書管理系統==================================\n\n");int total;//文件中書籍數量printf("請輸入圖書總量:\n");scanf("%d", &total);printf("\n請輸入圖書庫圖書數據:(如三體 01)\n");FILE* fp;if ((fp = fopen("book.txt", "w+")) == NULL){printf("\nCan't open this file!\n");exit(0);}struct book a[50], b[50];for (int i = 0; i < total; i++){scanf("%s%s", a[i].book_name, a[i].book_num);}fwrite(a, sizeof(struct book), total, fp);rewind(fp);fread(b, sizeof(struct book), total, fp);printf("\n輸入的圖書數據為:(圖書名稱 圖書編號 上次借出日期 狀態 熱度)\n");for (int i = 0; i < total; i++){printf("%s\t%s\t%s\t%d\t%d\n", b[i].book_name, b[i].book_num, b[i].date, b[i].flag, b[i].heat);}fclose(fp); //建立文件,輸入書籍數據printf("\n5秒后進入菜單界面...");Sleep(5000);system("cls");loop: int choose;menu();printf("你想進行的操作是:\n");scanf("%d", &choose);int add; //添加的圖書量struct book c[50], d[50];char find[20]; //要查找的數據struct book data[55];if ((fp = fopen("book.txt", "a+")) == NULL){printf("\nCan't open this file!\n");exit(0);}fread(data, sizeof(struct book), total, fp);fclose(fp); //將文件中的數據存入dataswitch (choose){case 1:system("cls");printf("/***圖書庫***/\n\n");printf("請輸入要添加的圖書量:\n");scanf("%d", &add);total = total + add; //新的總量if ((fp = fopen("book.txt", "a+")) == NULL){printf("\nCan't open this file!\n");exit(0);}printf("\n請輸入需要添加的圖書數據:(如三體 01)\n");for (int i = 0; i < add; i++){scanf("%s%s", c[i].book_name, c[i].book_num);}fseek(fp, 0, SEEK_END);fwrite(c, sizeof(struct book), add, fp);rewind(fp);fread(d, sizeof(struct book), total, fp);printf("\n更新后的圖書數據為:(圖書名稱 圖書編號 上次借出日期 狀態 熱度)\n");for (int i = 0; i < total; i++){printf("%s\t%s\t%s\t%d\t%d\n", d[i].book_name, d[i].book_num, d[i].date, d[i].flag, d[i].heat);}fclose(fp); //向文件中添加書籍數據sure();goto loop;case 2:system("cls");printf("/***查詢圖書***/\n\n");printf("請輸入您要查找的圖書名稱:\n");scanf("%s", find);search(data,find,total);sure();goto loop;case 3:system("cls");printf("/***借取圖書***/\n\n");borrow(data,total);if ((fp = fopen("book.txt", "w+")) == NULL){printf("\nCan't open this file!\n");exit(0);}fwrite(data, sizeof(struct book), total, fp);rewind(fp);fclose(fp); //向文件中添加書籍數據sure();goto loop;case 4:system("cls");printf("/***歸還圖書***/\n\n");returnbook(data,total);if ((fp = fopen("book.txt", "w+")) == NULL){printf("\nCan't open this file!\n");exit(0);}fwrite(data, sizeof(struct book), total, fp);rewind(fp);fclose(fp); //向文件中添加書籍數據sure();goto loop;case 5:system("cls");printf("/***圖書熱度排行榜***/\n\n");chart(data,total);sure();goto loop;case 6:system("cls");printf("-=======================================================================================================-\n");printf("| ************************************ |\n");printf("| ************************************ |\n");printf("| **********感謝您的使用!************ |\n");printf("| ************************************ |\n");printf("| ************************************ |\n");printf("-=======================================================================================================-\n");break;default:system("cls");printf("請輸入正確的序號!\n\n");sure();goto loop;}return 0; }? 以上代碼在Visual Studio 下運行
總結
以上是生活随笔為你收集整理的C语言课程设计——图书馆查询系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是Verilog HDL?
- 下一篇: EEPROM 编程器