您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、分析過程
首先此程序需要實(shí)現(xiàn)輸入、增加、刪除、查詢、輸出的五大功能,則首先需要設(shè)置一個(gè)菜單鍵,讓用戶可以選擇不同的功能,完成不同的操作,然后編寫不同的函數(shù)實(shí)現(xiàn)不同的功能,在這個(gè)過程中注意要人性化,讓用戶方便,直觀的進(jìn)行操作。
二、算法
三、函數(shù)模塊介紹
1錄入模塊:本模塊主要執(zhí)行信息的錄入功能
2瀏覽模塊:本模塊主要是執(zhí)行把已有信息輸出瀏覽功能
3查詢模塊:本模塊主要是按照圖書名查找圖書的相關(guān)信息
4刪除模塊:主要是執(zhí)行刪除圖書信息的功能
5退出模塊:方便用戶離開
四、源程序
#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct books_list { char author[20]; /*作者名*/ char bookname[20]; /*書名*/ char publisher[20]; /*出版單位*/ char pbtime[15]; /*出版時(shí)間*/ char loginnum[10]; /*登陸號*/ float price; /*價(jià)格*/ char classfy[10]; /*分類號*/ struct books_list * next; /*鏈表的指針域*/ }; struct books_list * Create_Books_Doc(); /*新建鏈表*/ void InsertDoc(struct books_list * head); /*插入*/ void DeleteDoc(struct books_list * head , int num);/*刪除*/ void Print_Book_Doc(struct books_list * head);/*瀏覽*/ void search_book(struct books_list * head); /*查詢*/ void save(struct books_list * head);/*保存數(shù)據(jù)至文件*/ /*新建鏈表頭節(jié)點(diǎn)*/ struct books_list * Create_Books_Doc() { struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list)); /*分配頭節(jié)點(diǎn)空間*/ head->next=NULL; /*頭節(jié)點(diǎn)指針域初始化,定為空*/ return head; } /*保存數(shù)據(jù)至文件*/ void save(struct books_list * head) { struct books_list *p; FILE *fp; p=head; fp=fopen("data.txt","w+"); /*以寫方式新建并打開 data.txt文件*/ fprintf(fp,"┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n"); /*向文件輸出表格*/ fprintf(fp,"┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時(shí)間 ┃分類號┃ 價(jià)格 ┃\n"); fprintf(fp,"┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n"); /*指針從頭節(jié)點(diǎn)開始移動,遍歷至尾結(jié)點(diǎn),依次輸出圖書信息*/ while(p->next!= NULL) { p=p->next; fprintf(fp,"┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); } fprintf(fp,"┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n"); fclose(fp); printf(" 已將圖書數(shù)據(jù)保存到 data.txt 文件\n"); } /*插入*/ void InsertDoc(struct books_list *head) { /*定義結(jié)構(gòu)體指針變量 s指向開辟的新結(jié)點(diǎn)首地址 p為中間變量*/ struct books_list *s, *p; char flag='Y'; /*定義flag,方便用戶選擇重復(fù)輸入*/ p=head; /*遍歷到尾結(jié)點(diǎn),p指向尾結(jié)點(diǎn)*/ while(p->next!= NULL) { p=p->next; } /*開辟新空間,存入數(shù)據(jù),添加進(jìn)鏈表*/ while(flag=='Y'||flag=='y') { s=(struct books_list *)malloc(sizeof(struct books_list)); printf("\n 請輸入圖書登陸號:"); fflush(stdin); scanf("%s",s->loginnum); printf("\n 請輸入圖書書名:"); fflush(stdin); scanf("%s",s->bookname); printf("\n 請輸入圖書作者名:"); fflush(stdin); scanf("%s",s->author); printf("\n 請輸入圖書出版社:"); fflush(stdin); scanf("%s",s->publisher); printf("\n 請輸入圖書出版時(shí)間:"); fflush(stdin); scanf("%s",s->pbtime); printf("\n 請輸入圖書分類號:"); fflush(stdin); scanf("%s",s->classfy); printf("\n 請輸入圖書價(jià)格:"); fflush(stdin); scanf("%f",&s->price); printf("\n"); p->next=s; /*將新增加的節(jié)點(diǎn)添加進(jìn)鏈表*/ p=s; /*p指向尾節(jié)點(diǎn),向后移*/ s->next=NULL; printf(" ━━━━ 添加成功!━━━━"); printf("\n 繼續(xù)添加?(Y/N):"); fflush(stdin); scanf("%c",&flag); printf("\n"); if(flag=='N'||flag=='n') {break;} else if(flag=='Y'||flag=='y') {continue;} } save(head); /*保存數(shù)據(jù)至文件*/ return; } /*查詢操作*/ void search_book(struct books_list *head) { struct books_list * p; char temp[20]; p=head; if(head==NULL || head->next==NULL) /*判斷數(shù)據(jù)庫是否為空*/ { printf(" ━━━━ 圖書庫為空!━━━━\n"); } else { printf("請輸入您要查找的書名: "); fflush(stdin); scanf("%s",temp); /*指針從頭節(jié)點(diǎn)開始移動,遍歷至尾結(jié)點(diǎn),查找書目信息*/ while(p->next!= NULL) { p=p->next; if(strcmp(p->bookname,temp)==0) { printf("\n圖書已找到!\n"); printf("\n"); printf("登錄號: %s\t\n",p->loginnum); printf("書名: %s\t\n",p->bookname); printf("作者名: %s\t\n",p->author); printf("出版單位: %s\t\n",p->publisher); printf("出版時(shí)間: %s\t\n",p->pbtime); printf("分類號: %s\t\n",p->classfy); printf("價(jià)格: %.2f\t\n",p->price); } if(p->next==NULL) { printf("\n查詢完畢!\n"); } } } return; } /*瀏覽操作*/ void Print_Book_Doc(struct books_list * head) { struct books_list * p; if(head==NULL || head->next==NULL) /*判斷數(shù)據(jù)庫是否為空*/ { printf("\n ━━━━ 沒有圖書記錄! ━━━━\n\n"); return; } p=head; printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n"); printf("┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時(shí)間 ┃分類號┃ 價(jià)格 ┃\n"); printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n"); /*指針從頭節(jié)點(diǎn)開始移動,遍歷至尾結(jié)點(diǎn),依次輸出圖書信息*/ while(p->next!= NULL) { p=p->next; printf("┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); /*循環(huán)輸出表格*/ } printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n"); printf("\n"); } /*刪除操作*/ void DeleteDoc(struct books_list * head) { struct books_list *s,*p; /*s為中間變量,p為遍歷時(shí)使用的指針*/ char temp[20]; int panduan; /*此變量用于判斷是否找到了書目*/ panduan=0; p=s=head; printf(" [請輸入您要?jiǎng)h除的書名]:"); scanf("%s",temp); /*遍歷到尾結(jié)點(diǎn)*/ while(p!= NULL) { if(strcmp(p->bookname,temp)==0) { panduan++; break; } p=p->next; } if(panduan==1) { for(;s->next!=p;) /*找到所需刪除卡號結(jié)點(diǎn)的上一個(gè)結(jié)點(diǎn)*/ { s=s->next; } s->next=p->next; /*將后一節(jié)點(diǎn)地址賦值給前一節(jié)點(diǎn)的指針域*/ free(p); printf("\n ━━━━ 刪除成功! ━━━━\n"); } else /*未找到相應(yīng)書目*/ { printf(" 您輸入的書目不存在,請確認(rèn)后輸入!\n"); } return; } int main() { struct books_list * head; char choice; head=NULL; for(;;) /*實(shí)現(xiàn)反復(fù)輸入選擇*/ { printf(" ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n"); printf(" ┃ ┃ tony 圖書館管理系統(tǒng) ┃ ┃\n"); printf(" ┃ ┗━━━━━━━━━━━━━━━━━━━┛ ┃\n"); printf(" ┃ [1]圖書信息錄入 ┃\n"); printf(" ┃ ┃\n"); printf(" ┃ [2]圖書信息瀏覽 ┃\n"); printf(" ┃ ┃\n"); printf(" ┃ [3]圖書信息查詢 ┃\n"); printf(" ┃ ┃\n"); printf(" ┃ [4]圖書信息刪除 ┃\n"); printf(" ┃ ┃\n"); printf(" ┃ [5]退出系統(tǒng) ┃\n"); printf(" ┃ ┃\n"); printf(" ┃ ┃\n"); printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━┛\n"); printf(" 請選擇:"); fflush(stdin); scanf("%c",&choice); if(choice=='1') { if(head==NULL) { head=Create_Books_Doc(); } InsertDoc(head); } else if(choice=='2') { Print_Book_Doc(head); } else if(choice=='3') { search_book(head); } else if(choice=='4') { DeleteDoc(head); } else if(choice=='5') { printf("\n"); printf(" ━━━━━━━━ 感謝使用圖書管理系統(tǒng) ━━━━━━━━\n"); break; } else { printf(" ━━━━ 輸入錯(cuò)誤,請重新輸入!━━━━\n"); } } return 0; }
五、課程設(shè)計(jì)總結(jié)
通過這一周的課程設(shè)計(jì),我感受到做程序一定要有自己的清晰計(jì)劃,否則會無處用力,感覺會很亂,當(dāng)高屋建瓴的有計(jì)劃后,然后開始認(rèn)真嚴(yán)謹(jǐn)?shù)脑O(shè)計(jì)每一個(gè)模塊,要知道用戶的一切輸入都是不可信的,必須想得要全面,要仔細(xì)的對待每一個(gè)細(xì)節(jié),任何一個(gè)差錯(cuò)都有可能讓程序崩潰,這其中有許多的困難,剛開始感覺到束手無策,但是放一放,換個(gè)角度想就會豁然開朗,我覺得從這個(gè)中感覺到了充實(shí),滿足感,也提醒我以后要多多練練,多上機(jī),積累經(jīng)驗(yàn),爭取設(shè)計(jì)出更多的好的程序。
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。