(C++)使用链表编写图书管理系统
一、程序实现功能:
1.录入书籍:将书籍录入图书管理系统
2.浏览书籍:查看图书管理系统里的所有书籍
3.借阅书籍:书籍存在可以借阅,库存-1,书的库存不足则无法借阅
4.归还书籍:库存+1,如果该书不是图书馆里的书籍,则无法录入
5.删除书籍:以书名为基础从图书管理系统中删除该书籍
6.查找书籍:按书名查找书籍,显示书籍的基本信息
7.排序书籍:按价格将书籍排序(降序)
二、要求
使用函数、指针和链表编写。
三、程序功能图
四、具体函数
五、程序代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>struct bookinfo
{char name[20]; //书名char author[10]; //作者char date[20]; //出版日期float price; //价格int num; //数量
};struct Node
{struct bookinfo data;struct Node* next;
};/*全局链表*/
struct Node* list = NULL;/*创建表头*/
struct Node* createhead()
{/*动态内存申请*/struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));headNode->next = NULL;return headNode;
}/*创建节点*/
struct Node* createNode(struct bookinfo data)
{struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = data;newNode->next = NULL;return newNode;
}void printList();
void display_menu();
void savebookfile();
void insertbook();
void readbookfile();
void deletebook();
struct Node* searchbook();
void sortbook();
void selectkey();/*打印链表*/
void printList(struct Node* headNode)
{struct Node* Bmove = headNode->next;printf("书名\t作者\t出版日期\t价格\t库存\n");while(Bmove != NULL){printf("%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);Bmove = Bmove->next;}}/*菜单登录界面*/
void display_menu()
{char str[100];FILE *fp;char *txt;fp = fopen("menu.txt","r");txt = fgets(str,100,fp);while(txt != NULL){printf("%s",str);txt = fgets(str,100,fp);}fclose(fp);
}/*将信息存到文件中*/
void savebookfile(const char* filename,struct Node* headNode)
{FILE* fp = fopen(filename,"w");struct Node* Bmove = headNode->next;while(Bmove != NULL){fprintf(fp,"%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);Bmove = Bmove->next;}fclose(fp);
}/*录入书籍*/
void insertbook(struct Node* headNode,struct bookinfo data)
{struct Node* newNode = createNode(data);newNode->next = headNode->next;headNode->next = newNode;}/*读取文件*/
void readbookfile(const char* filename, struct Node* headNode)
{ FILE* fp = fopen(filename,"r");if(fp == NULL){fp = fopen(filename,"w+");}struct bookinfo tempinfo;while(fscanf(fp, "%s\t%s\t%s\t%.1f\t%d\n",tempinfo.name,&tempinfo.author,&tempinfo.date,&tempinfo.price,&tempinfo.num ) != EOF){insertbook(list,tempinfo);}fclose(fp);
}/*删除书籍*/
void deletebook(struct Node* headNode,char *bookname)
{struct Node* leftNode = headNode;struct Node* rightNode = headNode->next;while(rightNode != NULL && strcmp(rightNode->data.name,bookname)){leftNode = rightNode;rightNode = leftNode->next;}if(leftNode == NULL){return;}else{printf("删除书籍成功!\n");leftNode->next = rightNode->next;free(rightNode);rightNode = NULL;}
}/*查找书籍*/
struct Node* searchbook(struct Node* headNode, char* bookname)
{struct Node* rightNode = headNode->next;while (rightNode != NULL && strcmp(rightNode->data.name, bookname)){rightNode = rightNode->next;}return rightNode;
}/*排序书籍*/
void sortbook(struct Node* headNode)
{for(struct Node* i = headNode->next; i != NULL; i = i->next){for(struct Node* j = headNode->next;j->next != NULL;j = j->next){/*排序书籍(按价格降序)*/if (j->data.price < j->next->data.price) {/*交换值*/struct bookinfo tempdata = j->data;j->data = j->next->data;j->next->data = tempdata;}}}/*排序后查看效果*/printList(headNode);
}/*交互界面*/
void selectkey()
{int userkey = 0;struct bookinfo tempbook; //生成一个临时的变量存储书籍信息struct Node* searchname = NULL; //生成一个临时变量存储查找的书名struct Node* borrowbook = NULL; //生成一个临时变量存储要借阅的书名struct Node* returnbook = NULL; //生成一个临时变量存储要归还的书名scanf("%d",&userkey);switch(userkey){case 1:printf("[ 录入书籍 ]\n");printf("输入书籍的信息(name,author,date,price,num):");scanf("%s%s%s%f%d",tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num);insertbook(list,tempbook);/*把书籍信息保存到booksinfo文本文件里*/savebookfile("bookinfo.txt",list);break;case 2:printf("[ 浏览书籍 ]\n");printList(list);break;case 3:printf("[ 借阅书籍 ]\n"); /*书籍存在可以借阅,库存-1,书的库存不足则无法借阅*/printf("请输入要借阅的书名:");scanf("%s",tempbook.name);borrowbook = searchbook(list,tempbook.name);if(borrowbook == NULL){printf("不存在该书,无法借阅!\n");}else{if(borrowbook->data.num > 0){borrowbook->data.num--;printf("借阅成功!\n");printList(list);}else{printf("当前书籍库存不足,借阅失败!\n");}}break;case 4:printf("[ 归还书籍 ]\n"); //库存+1printf("请输入要归还的书名:");scanf("%s",tempbook.name);returnbook = searchbook(list,tempbook.name);if(returnbook == NULL){printf("该书不是图书馆里的书籍!\n");}else{returnbook->data.num++;printf("书籍归还成功!\n");printList(list);}break;case 5:printf("[ 删除书籍 ]\n");printf("请输入要删除的书名:");scanf("%s",tempbook.name); deletebook(list,tempbook.name); /*按书名删除书籍*/printList(list);break;case 6:printf("[ 查找书籍 ]\n");printf("请输入要查询的书名:");scanf("%s",tempbook.name);searchname = searchbook(list,tempbook.name);if(searchname == NULL){printf("不存在该书,请加购录入!\n");}else{/*输出该书的信息*/printf("书名\t作者\t出版日期\t价格\t库存\n");printf("%s\t%s\t%s\t%.1f\t%d\n",searchname->data.name,searchname->data.author,searchname->data.date,searchname->data.price,searchname->data.num);}break;case 7:printf("[ 排序书籍 ]\n"); /*按价格排序(降序)*/sortbook(list);break;case 8:printf("[ 退出系统 ]\n");printf("退出成功\n");system("pause");exit(0); /*关闭整个程序*/break;default:printf("[ 错误 ]\n");break;}
}/*主界面*/
int main()
{list = createhead();readbookfile("bookinfo.txt",list);while(1){display_menu();selectkey();system("pause");system("cls");}system("pause");return 0;
}
六、效果
1.录入书籍
2.浏览书籍
3.借阅书籍
存在该书时,借阅成功,库存-1:
不存在该书时,无法借阅:
4.归还书籍
当图书管理系统里本不存在该书,则归还失败:
5.查找书籍
不存在该书时,则查找失败:
6.排序书籍
再录入书籍:
排序(降序):
7.删除书籍
以上为该程序的所有功能。
(C++)使用链表编写图书管理系统相关推荐
- 单链表实现图书管理系统(销售系统,马踏棋盘)
这篇文章主要介绍三个数据结构的课程设计,一共设涉及到三个课程设计,分别是图书管理系统,销售管理系统,马踏棋盘. 声明:图书管理系统为作者所写,其他两个来源于网络,如有侵权,请通知作者删除. 以下代码经 ...
- c语言图书管理系统用什么软件,编写c语言的软件 纯C语言编写图书管理系统.doc...
编写c语言的软件 纯C语言编写图书管理系统 编写c语言的软件 纯C语言编写图书管理系统WORD文档bbszp 导读:就爱阅读网友为您分享以下"纯C语言编写图书管理系统WORD文档bbszp& ...
- c语言之bbs管理系统,编写c语言的软件 纯C语言编写图书管理系统WORD文档bbszp.doc...
编写c语言的软件 纯C语言编写图书管理系统WORD文档bbszp 编写c语言的软件 纯C语言编写图书管理系统WORD文档bbszp 导读:就爱阅读网友为您分享以下"纯C语言编写图书管理系统W ...
- 数据结构练习:运用单链表实现图书管理系统(c/c++)(内含带头结点的单链表的基本操作)
数据结构练习:运用单链表实现图书管理系统 正文 注意 部分功能运行展示 所包含头文件及结构体的定义 自定义函数 主函数 完整代码 结束语 正文 本程序使用带头节点的单链表存储结构实现,共有六个基本功能 ...
- JavaWeb笔记(五)后端(Thymeleaf)(Tomcat类加载机制)(编写图书管理系统)
使用Thymeleaf模板引擎 虽然JSP为我们带来了便捷,但是其缺点也是显而易见的,那么有没有一种既能实现模板,又能兼顾前后端分离的模板引擎呢? Thymeleaf(百里香叶)是一个适用于Web和独 ...
- Java用链表写图书管理_C语言链表实现图书管理系统
之前参照网上的资料用链表实现了图书管理系统,包括简单的增删改查功能以及借书还书功能,我是VC6.0下写的一个控制台程序,格式参照的网上的.在动手编码之前,你需要理清自己的思路.首先,需要确定图书馆里系 ...
- 简单编写图书管理系统
首先我们先梳理一下图书管理系统的功能 图书管理系统功能梳理 一.系统简介:能够对图书进行增删改查 二.步骤分析:1.写菜单 - 图书系统的所有功能2.实现每一个功能增加学生:a.将图书的信息加到列表里 ...
- c语言编写图书检索系统,求C语言编写图书管理系统
答案:#include #include #include #include #include using namespace std; const int Maxb=10000; //最 ...
- java编写图书管理系统
** 基本思路 **此系统的对象有 1:书籍 2:使用者(用户和图书管理员) 1. 图书:它的属性有书名,编号,作者,类别,价格,状态(是否被借出) 2. 使用者 2.1 管理员:姓名,增加书籍,删除 ...
最新文章
- mysql怎么加全局锁_MySQL锁机制/管理(并发锁,行锁,表锁,预加锁,全局锁等等)
- 模块化数据机房具备的五大优势
- 亲测可用centos7安装git_centos7安装git踩坑记
- 使用adb查看数据库的一些命令
- c和go 两种语言结合使用 (一)
- 2017年个人工作总结
- JS难点之hoist
- 程序员面试金典——2.3访问单个结点的删除
- ListView优化方案及其原理
- Git以及Githup的使用
- 汽车零部件行业PLM解决方案
- windows各版本序列号集合
- iwork09破解方法及解决SFCompatibility错误方法
- xyoj 6042:让人头疼的“双十一”
- 如何换ionic里面的图标
- 运维笔试题1(转载)
- Java 程序性能优化
- 信道特征(码元、比特、波特率等概念)
- DIY 3D打印机——【有啥用啥版】
- scandisk常用方法