数据结构——C语言版 图书管理系统(简陋版)

2023-10-29 15:50

本文主要是介绍数据结构——C语言版 图书管理系统(简陋版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

数据结构——C语言版 图书管理系统(简陋版)

这是一个关于数据结构的博客,主要目的是综合运用C语言的知识,达到巩固之前所学的所有知识。以图书管理系统为例,做一个简陋版本的。此篇博客仅供参考,如有问题还请各路大神多多指教。

图书管理系统要求

在这里插入图片描述
(由于本人实力有限,选做部分功能并未实现,后期若有时间一定补上!)

Part-1 结构体定义

const int M=0x3f3f3f; //设置最大图书容量
typedef struct
{char ISBN[18]; //书号char name[50]; //书名double price;  //价格
}book;
book b[M],B;

Part-2 输出功能

void PrintBook()
{scls;FILE *fp; //文件基本操作if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("xxxx大学图书馆图书采购列表\n");printf("ISBN	                  书名	                定价\n");fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);printf("%s\t\t%s\t%.2lf\n",b[0].ISBN,b[0].name,b[0].price);while ((fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price))==3)printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}

Part-3 插入功能

void Insert() //根据位置插入
{int length=0;scls;int i;char ISBN[18];char name[50];double price;printf("请输入插入位置:");scanf("%d",&i);printf("请输入图书信息书号:");scanf("%s",&ISBN);printf("请输入图书信息书名:");scanf("%s",&name);printf("请输入图书价格:");scanf("%lf",&price);FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_tmp.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k; //统计当前列表的长度fclose(fp);for (int j=0;j<i-1;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);fprintf(fp_tmp,"%s\t%s\t%.2lf\n",ISBN,name,price); //第i组数据在位置i-1上for (int j=i-1;j<=length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);remove("book.txt"); //删除原文件rename("book_tmp.txt","book.txt"); //重命名新文件printf("已添加该图书信息\n");sp;scls;return;
}

Part-4 删除功能

void Delete() //根据位置删除
{scls;int i;printf("请选择删除位置:");scanf("%d",&i);FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_tmp.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);for (int j=0;j<i-1;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);for (int j=i;j<=length;j++) //直接跳过第i-1组数据fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);remove("book.txt");rename("book_tmp.txt","book.txt");printf("已删除该图书信息\n");sp;scls;return;
}

Part-5 查找功能

5.1 根据位置查找

void Find_Position()
{scls;int i;printf("请选择位置:");scanf("%d",&i);FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("ISBN	                  书名	                定价\n");int length=0;while (fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price)==3){length++;if (length==i) //一个很不讲武德的写法{printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);break;}}}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}

5.2 根据书名查找

void Find_Name() //如果书名相同,则输出全部同名信息
{scls;char name[50];printf("请输入书名:");scanf("%s",name);FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("ISBN	                  书名	                定价\n");fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price);while (!feof(fp)){if (strcmp(name,B.name)==0)printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price);}}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}

Part-6 修改价格

void Modify()
{scls;FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_newprice.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);for (int i=0;i<=length;i++){if (b[i].price<25.00)b[i].price*=1.2;elseb[i].price*=1.1;}for (int j=0;j<length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);printf("已修改图书信息\n");sp;scls;return;
}

Part-7 排序——按价格从小到大排序

void Sort() //采用sort函数
{scls;FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_newsort.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);sort(b,b+length+1,cmp);for (int j=1;j<=length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);printf("已重新排序\n");sp;scls;return;
}bool cmp(const book& b1,const book& b2) //自定义cmp函数
{if (b1.price!=b2.price)return b1.price<b2.price;
}

————————————————————————————————————————————————————————————
全部代码:

#include <bits/stdc++.h>
using namespace std;#define scls system("cls")
#define sp system("pause")
const int M=0x3f3f3f;
typedef struct
{char ISBN[18];char name[50];double price;
}book;
book b[M],B;void Init()
{int length=0;FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{while (fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price)==3)length++;} fclose(fp);
}
void Menu()
{printf("一个非常简陋的图书管理系统\n");printf("1.输出图书信息\n");printf("2.插入图书信息\n");printf("3.删除图书信息\n");printf("4.查找图书信息\n");printf("5.修改图书价格\n");printf("6.排序\n");printf("7.退出\n");printf("\n");printf("请选择服务:");
}
void Else()
{printf("指令无效,请重新输入!\n");sp;scls;return;
}
void PrintBook()
{scls;FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("北京林业大学图书馆计算机类图书采购列表\n");printf("ISBN	                  书名	                定价\n");fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);printf("%s\t\t%s\t%.2lf\n",b[0].ISBN,b[0].name,b[0].price);while ((fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price))==3)printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}
void Insert()
{int length=0;scls;int i;char ISBN[18];char name[50];double price;printf("请输入插入位置:");scanf("%d",&i);printf("请输入图书信息书号:");scanf("%s",&ISBN);printf("请输入图书信息书名:");scanf("%s",&name);printf("请输入图书价格:");scanf("%lf",&price);FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_tmp.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);for (int j=0;j<i-1;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);fprintf(fp_tmp,"%s\t%s\t%.2lf\n",ISBN,name,price);for (int j=i-1;j<=length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);remove("book.txt");rename("book_tmp.txt","book.txt");printf("已添加该图书信息\n");sp;scls;return;
}
void Delete()
{scls;int i;printf("请选择删除位置:");scanf("%d",&i);FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_tmp.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);for (int j=0;j<i-1;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);for (int j=i;j<=length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);remove("book.txt");rename("book_tmp.txt","book.txt");printf("已删除该图书信息\n");sp;scls;return;
}
void Find_Position()
{scls;int i;printf("请选择位置:");scanf("%d",&i);FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("ISBN	                  书名	                定价\n");int length=0;while (fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price)==3){length++;if (length==i){printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);break;}}}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}
void Find_Name()
{scls;char name[50];printf("请输入书名:");scanf("%s",name);FILE *fp;if ((fp=fopen("book.txt","r"))==NULL){printf("文件不存在!\n");return;}else{printf("ISBN	                  书名	                定价\n");fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price);while (!feof(fp)){if (strcmp(name,B.name)==0)printf("%s\t\t%s\t%.2lf\n",B.ISBN,B.name,B.price);fscanf(fp,"%s%s%lf",&B.ISBN,&B.name,&B.price);}}fclose(fp);printf("已显示所有信息\n");sp;scls;return;
}
void Find()
{scls;printf("1.按位置查找\n");printf("2.按书名查找\n");printf("请选择服务:");char x;scanf(" %c",&x);if (x=='1')Find_Position();else if (x=='2')Find_Name();elseElse();
}
void Modify()
{scls;FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_newprice.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);for (int i=0;i<=length;i++){if (b[i].price<25.00)b[i].price*=1.2;elseb[i].price*=1.1;}for (int j=0;j<length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);printf("已修改图书信息\n");sp;scls;return;
}
bool cmp(const book& b1,const book& b2)
{if (b1.price!=b2.price)return b1.price<b2.price;
}
void Sort()
{scls;FILE *fp,*fp_tmp;if ((fp=fopen("book.txt","r+"))==NULL||(fp_tmp=fopen("book_newsort.txt","w"))==NULL){printf("文件不存在!\n");return;}else{int k=0,length=0;fscanf(fp,"%s%s%lf",&b[0].ISBN,&b[0].name,&b[0].price);while (!feof(fp)){k++;fscanf(fp,"%s%s%lf",&b[k].ISBN,&b[k].name,&b[k].price);}length=k;fclose(fp);sort(b,b+length+1,cmp);for (int j=1;j<=length;j++)fprintf(fp_tmp,"%s\t%s\t%.2lf\n",b[j].ISBN,b[j].name,b[j].price);}fclose(fp_tmp);printf("已重新排序\n");sp;scls;return;
}int main()
{Init();while (true){Menu();char x;scanf(" %c",&x);if (x=='1')PrintBook();else if (x=='2')Insert();else if (x=='3')Delete();else if (x=='4')Find();else if (x=='5')Modify();else if (x=='6')Sort();else if (x=='7')exit(0);elseElse();}return 0;
}

这篇关于数据结构——C语言版 图书管理系统(简陋版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/301673

相关文章

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

基于Spring Boot 的小区人脸识别与出入记录管理系统功能

《基于SpringBoot的小区人脸识别与出入记录管理系统功能》文章介绍基于SpringBoot框架与百度AI人脸识别API的小区出入管理系统,实现自动识别、记录及查询功能,涵盖技术选型、数据模型... 目录系统功能概述技术栈选择核心依赖配置数据模型设计出入记录实体类出入记录查询表单出入记录 VO 类(用于

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In