當前位置:編程學習大全網 - 編程語言 - 怎樣用C語言制作圖書館管理系統

怎樣用C語言制作圖書館管理系統

這個是自己寫的

註釋很清楚地

有什麽不清楚的問我

#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]; /*出版時間*/

char loginnum[10]; /*登陸號*/

float price; /*價格*/

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 info_change(struct books_list * head);/*修改*/

void save(struct books_list * head);/*保存數據至文件*/

/*新建鏈表頭節點*/

struct books_list * Create_Books_Doc()

{

struct books_list * head;

head=(struct books_list *)malloc(sizeof(struct books_list)); /*分配頭節點空間*/

head->next=NULL; /*頭節點指針域初始化,定為空*/

return head;

}

/*保存數據至文件*/

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,"┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時間 ┃分類號┃ 價格 ┃\n");

fprintf(fp,"┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\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(" 已將圖書數據保存到 data.txt 文件\n");

}

/*插入*/

void InsertDoc(struct books_list *head)

{

/*定義結構體指針變量 s指向開辟的新結點首地址 p為中間變量*/

struct books_list *s, *p;

char flag='Y'; /*定義flag,方便用戶選擇重復輸入*/

p=head;

/*遍歷到尾結點,p指向尾結點*/

while(p->next!= NULL)

{

p=p->next;

}

/*開辟新空間,存入數據,添加進鏈表*/

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 請輸入圖書出版時間:");

fflush(stdin);

scanf("%s",s->pbtime);

printf("\n 請輸入圖書分類號:");

fflush(stdin);

scanf("%s",s->classfy);

printf("\n 請輸入圖書價格:");

fflush(stdin);

scanf("%f",&s->price);

printf("\n");

p->next=s; /*將新增加的節點添加進鏈表*/

p=s; /*p指向尾節點,向後移*/

s->next=NULL;

printf(" ━━━━ 添加成功!━━━━");

printf("\n 繼續添加?(Y/N):");

fflush(stdin);

scanf("%c",&flag);

printf("\n");

if(flag=='N'||flag=='n')

{break;}

else if(flag=='Y'||flag=='y')

{continue;}

}

save(head); /*保存數據至文件*/

return;

}

/*查詢操作*/

void search_book(struct books_list *head)

{

struct books_list * p;

char temp[20];

p=head;

if(head==NULL || head->next==NULL) /*判斷數據庫是否為空*/

{

printf(" ━━━━ 圖書庫為空!━━━━\n");

}

else

{

printf("請輸入您要查找的書名: ");

fflush(stdin);

scanf("%s",temp);

/*指針從頭節點開始移動,遍歷至尾結點,查找書目信息*/

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("出版時間: %s\t\n",p->pbtime);

printf("分類號: %s\t\n",p->classfy);

printf("價格: %.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) /*判斷數據庫是否為空*/

{

printf("\n ━━━━ 沒有圖書記錄! ━━━━\n\n");

return;

}

p=head;

printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n");

printf("┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時間 ┃分類號┃ 價格 ┃\n");

printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\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); /*循環輸出表格*/

}

printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n");

printf("\n");

}

/*修改操作*/

void info_change(struct books_list * head)

{

struct books_list * p;

int panduan=0; /*此變量用於判斷是否找到書目*/

char temp[20];

p=head;

printf("請輸入要修改的書名:");

scanf("%s",temp);

while(p->next!= NULL)

{

p=p->next;

if(strcmp(p->bookname,temp)==0)

{

printf("\n 請輸入圖書登陸卡號:");

fflush(stdin);

scanf("%s",p->loginnum);

printf("\n 請輸入圖書書名:");

fflush(stdin);

scanf("%s",p->bookname);

printf("\n 請輸入圖書作者名:");

fflush(stdin);

scanf("%s",p->author);

printf("\n 請輸入圖書出版社:");

fflush(stdin);

scanf("%s",p->publisher);

printf("\n 請輸入圖書出版時間:");

fflush(stdin);

scanf("%s",p->pbtime);

printf("\n 請輸入圖書分類號:");

fflush(stdin);

scanf("%s",p->classfy);

printf("\n 請輸入圖書價格:");

fflush(stdin);

scanf("%f",&p->price);

printf("\n");

panduan=1;

}

}

if(panduan==0)

{

printf("\n ━━━━ 沒有圖書記錄! ━━━━\n\n");

}

return;

}

/*刪除操作*/

void DeleteDoc(struct books_list * head)

{

struct books_list *s,*p; /*s為中間變量,p為遍歷時使用的指針*/

char temp[20];

int panduan; /*此變量用於判斷是否找到了書目*/

panduan=0;

p=s=head;

printf(" [請輸入您要刪除的書名]:");

scanf("%s",temp);

/*遍歷到尾結點*/

while(p!= NULL)

{

if(strcmp(p->bookname,temp)==0)

{

panduan++;

break;

}

p=p->next;

}

if(panduan==1)

{

for(;s->next!=p;) /*找到所需刪除卡號結點的上壹個結點*/

{

s=s->next;

}

s->next=p->next; /*將後壹節點地址賦值給前壹節點的指針域*/

free(p);

printf("\n ━━━━ 刪除成功! ━━━━\n");

}

else /*未找到相應書目*/

{

printf(" 您輸入的書目不存在,請確認後輸入!\n");

}

return;

}

int main(void)

{

struct books_list * head;

char choice;

head=NULL;

for(;;) /*實現反復輸入選擇*/

{

printf(" ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n");

printf(" ┃ ┃ socat 圖書管理系統 ┃ ┃\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]圖書信息刪除 ┃\n");

printf(" ┃ ┃\n");

printf(" ┃ ●[6]退出系統 ┃\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')

{

info_change(head);

}

else if(choice=='5')

{

DeleteDoc(head);

}

else if(choice=='6')

{

printf("\n");

printf(" ━━━━━━━━ 感謝使用圖書管理系統 ━━━━━━━━\n");

break;

}

else

{

printf(" ━━━━ 輸入錯誤,請重新輸入!━━━━");

break;

}

}

return 0;

}

  • 上一篇:智作壹個智能機器人需要哪些材料和工具
  • 下一篇:冷門公司特有的品牌名,霸氣又有魅力。
  • copyright 2024編程學習大全網