数据结构——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

相关文章

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

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

STL经典案例(四)——实验室预约综合管理系统(项目涉及知识点很全面,内容有点多,耐心看完会有收获的!)

项目干货满满,内容有点过多,看起来可能会有点卡。系统提示读完超过俩小时,建议分多篇发布,我觉得分篇就不完整了,失去了这个项目的灵魂 一、需求分析 高校实验室预约管理系统包括三种不同身份:管理员、实验室教师、学生 管理员:给学生和实验室教师创建账号并分发 实验室教师:审核学生的预约申请 学生:申请使用实验室 高校实验室包括:超景深实验室(可容纳10人)、大数据实验室(可容纳20人)、物联网实验

【408数据结构】散列 (哈希)知识点集合复习考点题目

苏泽  “弃工从研”的路上很孤独,于是我记下了些许笔记相伴,希望能够帮助到大家    知识点 1. 散列查找 散列查找是一种高效的查找方法,它通过散列函数将关键字映射到数组的一个位置,从而实现快速查找。这种方法的时间复杂度平均为(

使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统

引言 在企业级应用开发中,数据库操作是非常重要的一环。Spring Data JPA提供了一种简化的方式来进行数据库交互,它使得开发者无需编写复杂的JPA代码就可以完成常见的CRUD操作。此外,设计模式如单例模式可以帮助我们更好地管理和控制对象的创建过程,从而提高系统的性能和可维护性。本文将展示如何结合Spring Boot、Spring Data JPA以及单例模式来构建一个基本的库存管理系统

浙大数据结构:树的定义与操作

四种遍历 #include<iostream>#include<queue>using namespace std;typedef struct treenode *BinTree;typedef BinTree position;typedef int ElementType;struct treenode{ElementType data;BinTree left;BinTre

【干货分享】基于SSM的体育场管理系统的开题报告(附源码下载地址)

中秋送好礼 中秋佳节将至,祝福大家中秋快乐,阖家幸福。本期免费分享毕业设计作品:《基于SSM的体育场管理系统》。 基于SSM的体育场管理系统的开题报告 一、课题背景与意义 随着全民健身理念的深入人心,体育场已成为广大师生和社区居民进行体育锻炼的重要场所。然而,传统的体育场管理方式存在诸多问题,如资源分配不均、预约流程繁琐、数据统计不准确等,严重影响了体育场的使用效率和用户体验。