本系统实现了对图书信息的增删改查等功能。使用到了链表以及C语言的文件读写。

增:增加新的书籍数据

删:删除指定书名的整条书籍数据

改:修改指定书名的书籍数目(借/还书)

查:查找指定书名的书籍信息

1、主函数

输入相应序号实现对图书信息的特定操作

int main(){int action;struct LIB *const head =fileBuild(); bookMenu();while(1){printf("请输入要进行的操作序号:");scanf("%d",&action);switch(action){case 1:system("CLS");bookMenu();addBookInfo(head);break;case 2:system("CLS");bookMenu();delBookInfo(head);break;case 3:system("CLS");bookMenu();changeBookInfo(head);break;case 4:system("CLS");bookMenu();findBookInfo(head);break;case 5:system("CLS");bookMenu();showBooks(head);break;case 6:writeInFile(head);printf("已退出...\n");exit(0);break;default:printf("没有这个序号!");break;}}return 0;}

2、结构体定义

定义图书信息结构体

struct LIB{char bId[10];//书号 char bName[20];//书名 char aName[20];//作者 char pName[20];//出版社 char sort[20];//分类 float bPrice;//价格int bNum;//书籍数量struct LIB *next;};

3、创建文件

文件不存在就创建。为什么要创建这两个函数?文件1用来存图书信息,文件2用来存图书信息条数。如果图书信息进行了增加或删除操作,那么文件2的内容会更改。

存在则进行读取。将信息读取到链表中。

struct LIB * fileBuild(void){FILE *file,*f2;int i;struct LIB * const head = (struct LIB *)malloc(sizeof(struct LIB));struct LIB *pb,*pa;pa = pb = head;pb->next = NULL;if(!(fopen("LibBook.txt","r"))){printf("文件创建中...");file = fopen("LibBook.txt","w");f2 = fopen("Numcount.txt","w");fprintf(f2,"%d\n",count);fclose(f2);fclose(file);}else{ file = fopen("LibBook.txt","r");f2 = fopen("Numcount.txt","r");fscanf(f2,"%d",&count);for(i=0;ibId,pa->bName,pa->aName,pa->pName,pa->sort,&pa->bPrice,&pa->bNum);pb->next = pa;pb = pa;pb->next = NULL;}fclose(f2);fclose(file);}return head;}

4、向文件写入链表数据

每次改动之后就重新向文件写入数据

void writeInFile(struct LIB *head){struct LIB *pa = head;FILE *file,*f2;file = fopen("LibBook.txt","w");f2 = fopen("Numcount.txt","w");fscanf(file,"%d",&count);count = 0;while(pa->next!=NULL){count++;pa=pa->next; fprintf(file,"%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);}fprintf(f2,"%d\n",count);fclose(f2);fclose(file);printf("写入成功!"); }

5、查找信息

查询操作,通过书名查询对应的书籍信息

void findBookInfo(struct LIB *head){struct LIB *pa = head;char name[20];printf("请输入要查询的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){printf("找到书籍:%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);return;}}if(pa->next==NULL){printf("未找到!\n");}}

6、修改信息

修改操作,通过书名修改图书数目信息

void changeBookInfo(struct LIB *head){struct LIB *pa = head,*pb;pb = (struct LIB *)malloc(sizeof(struct LIB)); char name[10],action[10];int c,borrow,addnum;printf("请输入要修改信息的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){printf("找到书籍:%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);printf("请问想要借书还是还书?");scanf("%s",action);if(strcmp(action,"借书")==0){printf("请输入借书的数量:");scanf("%d",&borrow) ;pa->bNum = pa->bNum-borrow;printf("借书成功!\n");} else if(strcmp(action,"还书")==0){printf("请输入还书的数量:");scanf("%d",&addnum) ;pa->bNum = pa->bNum+addnum;printf("还书成功!\n");}else{printf("没有这个选项!\n");}return;}} }

7、增加信息

增加书籍信息操作(按书号插入)

void addBookInfo(struct LIB *head){struct LIB *pa,*pb,*pc;pa = pb =head;pc=(struct LIB *)malloc(sizeof(struct LIB)); printf("请输入要增加的信息(书号、书名、作者、出版社、分类、价格、数量):");scanf("%s%s%s%s%s%f%d",pc->bId,pc->bName,pc->aName,pc->pName,pc->sort,&pc->bPrice,&pc->bNum);while(pa->next!=NULL){pa = pa->next;if(atoi(pa->bId)>atoi(pc->bId)){pc->next = pa;pb->next = pc;printf("添加成功!\n");return;} pb = pa;} if(pa->next==NULL){pa->next = pc;pc->next = NULL;}}

8、删除信息

删除指定书名的信息。

void delBookInfo(struct LIB *head){struct LIB *pa,*pb;pa = pb =head;char name[10];printf("请输入要删除信息的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){pb->next = pa->next;free(pa);printf("删除成功!");break;}pb = pa;} } 

9、菜单

菜单显示可以使用哪些功能

void bookMenu(void){printf(" =========图书管理系统==========\n");printf("| 1、添加书籍数据|\n");printf("| 2、删除书籍数据|\n");printf("| 3、借书以及进货|\n");printf("| 4、查找指定书籍|\n");printf("| 5、显示全部书籍|\n");printf("| 6、退出管理系统|\n");printf(" ===============================\n");}

10、显示所有图书信息

遍历链表,显示所有的图书信息

void showBooks(struct LIB *head){struct LIB *pa = head;printf("书号\t\t书名\t\t作者\t\t出版社\t\t分类\t\t价格\t\t数量\t\t\n");while(pa->next!=NULL){pa = pa->next;printf("%-16s%-16s%-16s%-16s%-16s%-17.2f%-18d\n",pa->bId,pa->bName, pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);}} 

完整代码

#include #include #include struct LIB{char bId[10];//书号 char bName[20];//书名 char aName[20];//作者 char pName[20];//出版社 char sort[20];//分类 float bPrice;//价格int bNum;//书籍数量struct LIB *next;};struct LIB * fileBuild(void);//文件创建void writeInFile(struct LIB *head);//文件写入void addBookInfo(struct LIB *head);//增void delBookInfo(struct LIB *head);//删void changeBookInfo(struct LIB *head);//改void findBookInfo(struct LIB *head);//查 void showBooks(struct LIB *head);//显示所有书籍 void bookMenu(void);//显示菜单 int count;int main(){int action;struct LIB *const head =fileBuild(); bookMenu();while(1){printf("请输入要进行的操作序号:");scanf("%d",&action);switch(action){case 1:system("CLS");bookMenu();addBookInfo(head);break;case 2:system("CLS");bookMenu();delBookInfo(head);break;case 3:system("CLS");bookMenu();changeBookInfo(head);break;case 4:system("CLS");bookMenu();findBookInfo(head);break;case 5:system("CLS");bookMenu();showBooks(head);break;case 6:writeInFile(head);printf("已退出...\n");exit(0);break;default:printf("没有这个序号!");break;}}return 0;}struct LIB * fileBuild(void){FILE *file,*f2;int i;struct LIB * const head = (struct LIB *)malloc(sizeof(struct LIB));struct LIB *pb,*pa;pa = pb = head;pb->next = NULL;if(!(fopen("LibBook.txt","r"))){printf("文件创建中...");file = fopen("LibBook.txt","w");f2 = fopen("Numcount.txt","w");fprintf(f2,"%d\n",count);fclose(f2);fclose(file);}else{ file = fopen("LibBook.txt","r");f2 = fopen("Numcount.txt","r");fscanf(f2,"%d",&count);for(i=0;ibId,pa->bName,pa->aName,pa->pName,pa->sort,&pa->bPrice,&pa->bNum);pb->next = pa;pb = pa;pb->next = NULL;}fclose(f2);fclose(file);}return head;}void writeInFile(struct LIB *head) {struct LIB *pa = head;FILE *file,*f2;file = fopen("LibBook.txt","w");f2 = fopen("Numcount.txt","w");fscanf(file,"%d",&count);count = 0;while(pa->next!=NULL){count++;pa=pa->next; fprintf(file,"%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);}fprintf(f2,"%d\n",count);fclose(f2);fclose(file);printf("写入成功!"); }void addBookInfo(struct LIB *head) {struct LIB *pa,*pb,*pc;pa = pb =head;pc=(struct LIB *)malloc(sizeof(struct LIB)); printf("请输入要增加的信息(书号、书名、作者、出版社、分类、价格、数量):");scanf("%s%s%s%s%s%f%d",pc->bId,pc->bName,pc->aName,pc->pName,pc->sort,&pc->bPrice,&pc->bNum);while(pa->next!=NULL) {pa = pa->next;if(atoi(pa->bId)>atoi(pc->bId)){pc->next = pa;pb->next = pc;printf("添加成功!\n");return;} pb = pa;} if(pa->next==NULL){pa->next = pc;pc->next = NULL;}}void delBookInfo(struct LIB *head){struct LIB *pa,*pb;pa = pb =head;char name[10];printf("请输入要删除信息的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){pb->next = pa->next;free(pa);printf("删除成功!");break;}pb = pa;} } void changeBookInfo(struct LIB *head){struct LIB *pa = head,*pb;pb = (struct LIB *)malloc(sizeof(struct LIB)); char name[10],action[10];int c,borrow,addnum;printf("请输入要修改信息的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){printf("找到书籍:%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);printf("请问想要借书还是还书?");scanf("%s",action);if(strcmp(action,"借书")==0){printf("请输入借书的数量:");scanf("%d",&borrow) ;pa->bNum = pa->bNum-borrow;printf("借书成功!\n");} else if(strcmp(action,"还书")==0){printf("请输入还书的数量:");scanf("%d",&addnum) ;pa->bNum = pa->bNum+addnum;printf("还书成功!\n");}else{printf("没有这个选项!\n");}return;}} }void findBookInfo(struct LIB *head){struct LIB *pa = head;char name[20];printf("请输入要查询的书名:");scanf("%s",name);while(pa->next!=NULL){pa = pa->next; if(strcmp(pa->bName,name)==0){printf("找到书籍:%s %s %s %s %s %0.2f %d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);return;}}if(pa->next==NULL){printf("未找到!\n");}}void bookMenu(void){printf(" =========图书管理系统==========\n");printf("| 1、添加书籍数据|\n");printf("| 2、删除书籍数据|\n");printf("| 3、借书以及还书|\n");printf("| 4、查找指定书籍|\n");printf("| 5、显示全部书籍|\n");printf("| 6、退出管理系统|\n");printf(" ===============================\n");}void showBooks(struct LIB *head){struct LIB *pa = head;printf("书号\t\t书名\t\t作者\t\t出版社\t\t分类\t\t价格\t\t数量\t\t\n");while(pa->next!=NULL){pa = pa->next;printf("%-16s%-16s%-16s%-16s%-16s%-17.2f%-18d\n",pa->bId,pa->bName,pa->aName,pa->pName,pa->sort,pa->bPrice,pa->bNum);}} 

缺点

虽然基本实现了增删改查的功能,但是还有不足:

1.查找书籍只能找书名全部匹配的。
2.添加书籍数据时,只能创建一条新的数据,若数据已存在不能选择修改部分信息。
3.不能连续操作,只能每输入一次操作号进行一次操作。