数据结构——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;
else
b[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();
else
Else();
}
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;
else
b[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);
else
Else();
}
return 0;
}