#include <stdio.h>
#include <string.h>
#include<stdlib.h>
// 图书结构体
struct Book
{
int num;
char Class[10];
char BookName[30];
char Author[30];
char Publishing[30];
int Count;
float Price;
}P[10];
int M = 10;//图书数量
char Head[200];
struct Book tmp;
// 菜单
void menu();
// 读取文件
void ReadFile();
// 按书名查询
void QryByBookName();
// 按作者查询
void QryByAuthor();
//按出版社排序
void anchubanshepaixu();
//按种类排序
void anzhongleipaixu();
// 图书总价
void tushuzongjia();
//图书的修改
void tushudexiugai();
//图书的借阅
void tushudejieyue();
void main()
{
int ind;
ReadFile();
while(ind<=8)
{
menu();
printf("请输入操作编号:");
scanf("%d",&ind);
switch(ind)
{
case 1: ReadFile();
break;
case 2: QryByBookName();
break;
case 3: QryByAuthor();
break;
case 4: anchubanshepaixu();
break;
case 5: anzhongleipaixu();
break;
case 6: tushuzongjia();
break;
case 7: tushudexiugai();
break;
case 8: tushudejieyue();
break;
case 9: printf("退出\n");
break;
default: printf("输入错误,请按菜单提示输入!");
break;
}
}
}
void menu()
{
printf(" *********************************\n");
printf(" 图书管理系统 \n");
printf(" |*******************************|\n");
printf(" | 1.读取图书基本信息 |\n");
printf(" | 2.按书名查查询 |\n");
printf(" | 3.按作者名查询 |\n");
printf(" | 4.按出版社排序 |\n");
printf(" | 5.按种类排序 |\n");
printf(" | 6.书籍总价 |\n");
printf(" | 7.图书的修改 |\n");
printf(" | 8.图书的借阅 |\n");
printf(" | 9.退出 |\n");
printf(" *********************************\n");
}
void ReadFile()
{
int i;
FILE *fp;
fp = fopen("CK0103.txt", "r");
if(fp == NULL)
{
printf("文件打开错误!");
return;
}
//标题栏
fgets(Head, 200, fp);
for(i = 0; i < M; i++)
{
fscanf(fp,"%d%s%s%s%s%d%f\n",&P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,&P[i].Count,&P[i].Price);
}
fclose(fp);
printf("%s", Head);
for(i =0; i < M; i++)
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
}
void QryByBookName()
{
char n[100];
int i,flag=0;
printf("输入书名:\n");
scanf("%s",n);
printf("%s",Head);
for(i=1;i<M;i++)
if(strcmp(n,P[i].BookName)==0)
{
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
flag=1;
}
if(flag==0)
printf("该书不存在!");
}
void QryByAuthor()
{
char n[100];
int i,flag=0;
printf("输入作者名字:\n");
scanf("%s",n);
printf("%s",Head);
for(i=0;i<M;i++)
if(strcmp(n,P[i].Author)==0)
{
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
flag=1;
}
if(flag==0)
printf("该书不存在!");
}
void QryByPublishing()
{
char n[100];
int i,flag=0;
printf("输入出版社名字:\n");
scanf("%s",n);
printf("%s",Head);
for(i=0;i<M;i++)
if(strcmp(n,P[i].Publishing)==0)
{
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
flag=1;
}
if(flag==0)
printf("该书不存在!");
}
void anzhongleipaixu()
{
int i,j,k;
for(i=0;i<M-1;i++)
{
k=i;
for(j=k+1;j<M;j++)
{
if(strcmp(P[k].Class,P[j].Class)>0)
k=j;
}
if(i!=k)
{
tmp=P[k];
P[k]=P[i];
P[i]=tmp;
}
}
printf("%s", Head);
for(i=0;i<M;i++)
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
}
void anchubanshepaixu()
{
int i,j,k;
for(i=0;i<M-1;i++)
{
for(j=i+1;j<M;j++)
{
if(strcmp(P[k].Publishing,P[j].Publishing)>0)
{
tmp=P[i];
P[i]=P[j];
P[j]=tmp;
}
}
}
printf("%s", Head);
for(i=0;i<M;i++)
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
}
void tushuzongjia()
{
int i;
float sum = 0;
FILE *fp;
fp = fopen("CK0103.txt", "w");
if(fp == NULL)
{
printf("文件打开错误!");
return;
}
//标题栏
fprintf(fp,"%s",Head);
for(i = 0; i < M; i++)
{
fprintf(fp,"%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
sum+=P[i].Count*P[i].Price;
}
printf("%s", Head);
for(i = 0; i < M; i++)
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
fprintf(fp," 总价 %-6.2f",sum);
fclose(fp);
printf(" 总价 %-6.2f",sum);
}
void tushudexiugai()
{
int i,flag=0;
int num,number;
FILE *fp;
printf("输入修改编号:");
scanf("%d",&num);
for(i=0;i<M;i++)
{
if(num==P[i].num)
{
printf("请输入修改后编号:");
scanf("%d",&number);
P[i].num=number;
}
}
printf("%s",Head);
for(i =0; i < M; i++)
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
fp = fopen("CK0103.txt", "w");
if(fp == NULL)
{
printf("文件打开错误!");
return;
}
//标题栏
fprintf(fp,"%s",Head);
for(i = 0; i < M; i++)
{
fprintf(fp,"%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n",P[i].num,P[i].Class,P[i].BookName,P[i].Author,P[i].Publishing,P[i].Count,P[i].Price);
}
fclose(fp);
}
void tushudejieyue()
{
int i, flag=0;
char BookName[20];
printf("\n请输入书名:\n");
scanf("%s", BookName);
for(i = 0; i < M; i++)
{
if(strcmp(BookName, P[i].BookName)==0)
{ printf("\n借阅的书籍是:\n");
printf("%s", Head);
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n", P[i].num, P[i].Class, P[i].BookName, P[i].Author, P[i].Publishing, P[i].Count, P[i].Price);
P[i].Count--;
flag=1;
printf("\n借阅后书籍的状态是:\n");
printf("%s", Head);
printf("%-6d%-10s%-18s%-10s%-24s%-4d%-6.2f\n", P[i].num, P[i].Class, P[i].BookName, P[i].Author, P[i].Publishing, P[i].Count, P[i].Price);
}
}
if(flag==0)
{
printf("\n未找到指定书籍!");
}
printf("按y继续借阅:");
}
网友评论