您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用C語言實現圖書管理系統”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么用C語言實現圖書管理系統”文章能幫助大家解決問題。
設計圖書管理系統
要求如下:
1.對書名,作者,編號,出版單位,出版日期,類別,存放位置進行添加、刪除、修改
2.查詢相關信息
3.保存所有信息
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> typedef struct book /*定義結構體*/ { char bookname[100] ; /*書名*/ char writer[100]; /*作者*/ char classnumber[100]; /*編號*/ char publisher[100]; /*出版單位*/ char time[100]; /*出版日期*/ char type[100]; /*類別*/ char place[100]; /*存放位置*/ struct book *next; }Book; int mima() { int i=0; char user[10]="aa"; char Code[10]="123456"; char getuser[10]; char Getskey[10]; while(1) { printf("\t\t\t請輸入賬號:"); scanf("%s",getuser); printf("\t\t\t請輸入密碼:"); while(1) { Getskey[i]=getch(); if(Getskey[i]=='\r') break; else if(Getskey[i]=='\b') { if(i==0) continue; printf("\b"); printf(" "); printf("\b"); i--; } else { printf("*"); i++; } } Getskey[6]='\0'; if(strcmp(user, getuser)==0&&strcmp(Code, Getskey)==0) return 1; else { printf("\n\n\t\t\t賬號不存在或密碼輸入錯誤,請重新輸入\n"); i = 0; } } } void showmenu1() /*顯示管理員菜單*/ { system("cls"); printf("\n\n\n\n\n"); printf("\t \n"); printf("\t 歡迎來到圖書信息管理系統 \n"); printf("\t \n"); printf("\t \n"); printf("\t 主菜單 \n"); printf("\t \n"); printf("\t 1.錄入圖書信息 \n"); printf("\t \n"); printf("\t 2.刪除圖書信息 \n"); printf("\t \n"); printf("\t 3.修改圖書信息 \n"); printf("\t \n"); printf("\t 4.查詢圖書信息 \n"); printf("\t \n"); printf("\t 5.顯示所有圖書信息 \n"); printf("\t \n"); printf("\t 6.返回登錄界面 \n"); printf("\t \n"); printf("\t 7.保存數據并退出系統 \n"); printf("\t \n"); } void showmenu2() /*顯示游客菜單*/ { system("cls"); printf("\n\n\n\n\n"); printf("\t \n"); printf("\t 歡迎來到圖書信息管理系統 \n"); printf("\t \n"); printf("\t \n"); printf("\t 主菜單 \n"); printf("\t \n"); printf("\t 1.查詢圖書信息 \n"); printf("\t \n"); printf("\t 2.顯示所有圖書信息 \n"); printf("\t \n"); printf("\t 3.返回登錄界面 \n"); printf("\t \n"); printf("\t 4.退出系統 \n"); printf("\t \n"); } int savebookdata(Book *head) /*將數據保存到文件中*/ { Book *p; FILE*fp; fp=fopen("D:\\java\\zuoye.txt","w"); p=head->next; do { fprintf(fp,"%s\t",p->bookname); /* 把這個結點的數據全部寫入至文件內由于next指針不需要寫入至文件,*/ fprintf(fp,"%s\t",p->writer); /*因此,數據大小就是sizeof(book)-sizeof(book*)總數據大小減去指針的大小 */ fprintf(fp,"%s\t",p->classnumber); fprintf(fp,"%s\t",p->publisher); fprintf(fp,"%s\t",p->time); fprintf(fp,"%s\t",p->type); fprintf(fp,"%s\t\n",p->place); fprintf(fp,"\n"); p=p->next; /*轉換到下一個結點*/ }while(p!=NULL); fclose(fp); return 0; } void addnewbook(Book **head) /*錄入圖書信息*/ { int i; Book *p,*s; system("cls"); if(*head == NULL) { /* 如果表頭為空,先為它分配內存 */ *head = (Book *)calloc(1, sizeof(Book)); p = *head; } else { /* 否則,就說明有內容,直接切換到尾結點 */ p = *head; while(p->next) p = p->next; } printf("\n\n\n"); printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("\t\t\t請問添加幾本書:"); scanf("%d",&i); printf("\t--------------------------------------------------------\n"); while(i) { s = (Book *)calloc(1, sizeof(Book)); /*為這個結點分配內存 */ printf("\t\t\t請輸入書名:"); scanf("%s",s->bookname); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入作者:"); scanf("%s",s->writer); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入編號:"); scanf("%s",s->classnumber); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入出版單位:"); scanf("%s",s->publisher); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入出版日期:"); scanf("%s",s->time); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入類別:"); scanf("%s",s->type); printf("\t--------------------------------------------------------\n"); printf("\t\t\t請輸入存放位置:"); scanf("%s",s->place); printf("\t--------------------------------------------------------\n"); printf("\n"); printf("\t--------------------------------------------------------\n"); i--; p->next=s; /*保存輸進去的這個結點*/ p=p->next; /*切換到下一個結點*/ p->next=NULL; /*最后一個結點為NULL*/ } printf("\t\t\t圖書添加成功!!!\n"); system("pause"); } int readbookdata(Book **head) /*將數據從文件中讀取*/ { int n; Book *s, *p; FILE *fp; fp=fopen("d:\\java\\zuoye.txt","r"); /* 如果文件打開失敗,就返回-1 */ if(NULL == fp) { *head = NULL; return -1; } *head = (Book *)calloc(1, sizeof(Book)); p = *head; while (!feof(fp)) { s = (Book *)calloc(1, sizeof(Book)); /* 為下個結點分配內存空間 */ fscanf(fp,"%s",s->bookname); fscanf(fp,"%s",s->writer); fscanf(fp,"%s",s->classnumber); fscanf(fp,"%s",s->publisher); fscanf(fp,"%s",s->time); fscanf(fp,"%s",p->type); fscanf(fp,"%s\n",p->place); p->next=s; /*保存該結點*/ p=p->next; /*切換到下一個結點*/ p->next=NULL; /*保證最后一個結點為NULL*/ } fclose(fp);/* 關閉文件 */ return 0; } void deletebook(Book *head) /*刪除圖書信息*/ { system("cls"); int flag=0; char name[100],ch[10]; Book *p,*t; /* 如果表頭為空 */ if(head == NULL || head->next == NULL) { printf("沒有記錄圖書信息!\n"); system("pause"); return; } p=head; printf("\n\n\n"); printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("請輸入要刪除的書名;"); scanf("%s",&name); printf("----------------------------------------------------------------------\n"); printf("書名\t\t作者名\t\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------\n"); while(p->next) { t=p; p=p->next; /*轉換到下一個結點*/ if(strcmp(name,p->bookname)==0) { flag=1; printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); break; } } if(flag==1) { printf("是否刪除該本圖書信息(y/n)"); scanf("%s",ch); printf("----------------------------------------------------------------------\n"); if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) { t->next=p->next; printf("該書已刪除!\n"); } else return; } else if(flag==0) printf("沒找到那本書!\n"); system("pause"); } void modifybook(Book *head) /*修改圖書信息*/ { system("cls"); int flag=0; char name[100],ch[10]; Book *p,*t; /* 如果表頭為空 */ if(head == NULL || head->next == NULL) { printf("沒有記錄圖書信息!\n"); system("pause"); return ; } p=head; printf("\n\n\n"); printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("請輸入要修改的書名:"); scanf("%s",name); printf("--------------------------------------------------------------------\n"); while(p->next) { p=p->next; /*切換到下一個結點*/ if(strcmp(name,p->bookname)==0) { flag=1; t=p; printf("書名\t\t作者名\t\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("--------------------------------------------------------------------\n"); printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); } } if(flag==1) { printf("--------------------------------------------------------------------\n"); printf("\t\t是否修改該本圖書信息(y/n):"); scanf("%s",ch); printf("\t--------------------------------------------------------------------\n"); if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) { int c; printf("\t\t【1】書名\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【2】作者\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【3】編號\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【4】出版單位\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【5】出版日期\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【6】類別\n"); printf("--------------------------------------------------------------------\n"); printf("\t\t【7】存放位置\n"); printf("--------------------------------------------------------------------\n"); printf("\t請輸入你要修改的內容;"); scanf("%d",&c); printf("--------------------------------------------------------------------\n"); p=t; switch(c) { case 1: printf("\t請輸入書名:"); scanf("%s",p->bookname); break; case 2: printf("\t請輸入作者:"); scanf("%s",p->writer); break; case 3: printf("\t請輸入編號:"); scanf("%s",p->classnumber); break; case 4: printf("\t請輸入出版單位:"); scanf("%s",p->publisher); break; case 5: printf("\t請輸入出版日期:"); scanf("%s",p->time); break; case 6: printf("\t請輸入類別:"); scanf("%s",p->type); break; case 7: printf("\t請輸入存放位置:"); scanf("%s",p->place); break; } printf("--------------------------------------------------------------------\n"); printf("\t恭喜大佬修改成功!!!\n"); } } else if(flag==0) printf("對不起,該書庫沒有該書\n"); system("pause"); } void searchbook(Book *head) /*查詢圖書信息*/ { system("cls"); int choose,flag=0; Book *p; char b[100]; /* 如果表頭為空 */ if(head == NULL || head->next == NULL) { printf("沒有記錄圖書信息!\n"); system("pause"); return ; } do { system("cls"); printf("\n\n\n"); printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("\t\t1.按書名查找\n"); printf("----------------------------------------------------------------------\n"); printf("\t\t2.按作者名查找\n"); printf("----------------------------------------------------------------------\n"); printf("\t\t3.按編號查找\n"); printf("----------------------------------------------------------------------\n"); printf("\t\t4.按出版單位查找\n"); printf("----------------------------------------------------------------------\n"); printf("\t\t返回主菜單(其他數字)\n"); printf("----------------------------------------------------------------------\n"); printf("\t\t請輸入您的選擇:"); scanf("%d",&choose); printf("----------------------------------------------------------------------\n"); p=head; if(choose==1) { printf("輸入所查書名:"); scanf("%s",b); printf("----------------------------------------------------------------------\n\n"); while(p->next) { p=p->next; /*轉換到下一個結點*/ if(strcmp(b,p->bookname)==0) /*判斷是否找到并輸出*/ { flag=1; printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("書名\t作者名\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------\n"); printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); } } if(flag==0) printf("不存在該信息\n"); } else if(choose==2) { printf("輸入所查作者名:"); scanf("%s",b); printf("----------------------------------------------------------------------\n\n"); while(p->next) { p=p->next; /*轉換到下一個結點*/ if(strcmp(b,p->writer)==0) /*判斷是否找到并輸出*/ { flag=1; printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("書名\t作者名\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------\n"); printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); } } if(flag==0) printf("不存在該信息\n"); } else if(choose==3) { printf("輸入所查編號:"); scanf("%s",b); printf("----------------------------------------------------------------------\n\n"); while(p->next) { p=p->next; /*轉換到下一個結點*/ if(strcmp(b,p->classnumber)==0) /*判斷是否找到并輸出*/ { flag=1; printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("書名\t作者名\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------\n"); printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); } } if(flag==0) printf("不存在該信息\n"); } else if(choose==4) { printf("輸入所查出版單位:"); scanf("%s",b); printf("----------------------------------------------------------------------\n\n"); while(p->next) { p=p->next; /*轉換到下一個結點*/ if(strcmp(b,p->publisher)==0) /*判斷是否找到并輸出*/ { flag=1; printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("書名\t作者名\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------\n"); printf("%s\t%s\t%s\t%s\t%s%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); } } if(flag==0) printf("不存在該信息\n"); } else return; system("pause"); }while(1); } int printfbook(Book *head) /*顯示所有圖書信息*/ { Book *p; system("cls"); /* 如果表頭為空 */ if(head == NULL || head->next == NULL) { printf("沒有記錄圖書信息!\n"); system("pause"); return 0; } p=head->next; printf("\n\n\n"); printf("**********************************************************************************\n"); printf(" 圖書列表 \n"); printf("----------------------------------------------------------------------------------\n"); printf("書名\t作者\t編號\t出版單位\t出版時間\t類別\t存放位置\n"); printf("----------------------------------------------------------------------------------\n"); do { printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place); p=p->next; /*轉換到下一個結點*/ }while(p!=NULL); system("pause"); } int main(void) { system("cls"); Book *head=NULL; readbookdata(&head); int choice,z,c; printf("\n\n\n"); printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"); printf("\t\t\t歡迎光臨圖書信息管理系統\n"); printf("\t--------------------------------------------------------\n"); printf("\t\t\t【1】游客\n"); printf("\t\t\t【2】管理員\n"); printf("\t\t\t【3】退出系統\n"); printf("\t--------------------------------------------------------\n"); while(printf("\t\t\t請選擇身份:")&&scanf("%d",&c)!=EOF) { if(c==1) { showmenu2(); while (printf("\t請輸入選擇項(1-3):")&&scanf("%d",&choice)!=EOF) { switch (choice) { case 1: /*查詢圖書信息*/ searchbook(head); break; case 2: /*顯示所有圖書信息*/ printfbook(head); break; case 3: /*返回登錄界面*/ main(); break; case 4: exit(0); default: printf("\t輸入錯誤!!!\n"); system("pause"); break; } showmenu2(); } } else if(c==2) { z=mima(); showmenu1(); while (printf("\t請輸入選擇項(1-6):")&&z==1&&scanf("%d",&choice)!=EOF) { switch (choice) { case 1: /*錄入圖書信息*/ addnewbook(&head); break; case 2: /*刪除圖書信息*/ deletebook(head); break; case 3: /*修改圖書信息*/ modifybook(head); break; case 4: /*查詢圖書信息*/ searchbook(head); break; case 5: /*顯示所有圖書信息*/ printfbook(head); break; case 6: /*返回登錄界面*/ main(); break; case 7: /*保存數據并退出系統*/ savebookdata(head); exit(0); default: printf("\t輸入錯誤!!!\n"); system("pause"); break; } showmenu1(); } } else if(c==3) exit(0); else { printf("\t\t\t輸入錯誤!!!\n\n"); } } return 0; }
這里提供的是管理員身份
這里提供游客身份(供選擇的功能相對管理員少)
關于“怎么用C語言實現圖書管理系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。