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