#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>
#define M 2
struct date
{
int year;
int month;
int day;
};
struct library
{
int book_num;
char book_name[40];
char author_name[10];
float book_price;
char publisher[40];
struct date publish_time;
char book_state[2];
char reader_name[10];
int reader_num;
};
int input(struct library book[]);
int add(struct library book[],int n);
void list(struct library book[],int n);
void search_num(struct library book[],int n);
void search_name(struct library book[],int n);
int Delete(struct library book[],int n);
void sort(struct library book[],int n);
void count(struct library book[],int n);
void unborrow(struct library book[],int n);
void saverecord(struct library book[],int n);
int menu();
void main()
{
struct library b[M];
int length;
while(1)
{
switch(menu())
{
case 1:length=input(b);
if(length==-1) break;
else
saverecord(b,length);
break;
case 2:length=loadrecord(b);
length=add(b,length);
saverecord(b,length);
break;
case 3:length=loadrecord(b);
list(b,length);
break;
case 4:length=loadrecord(b);
search_num(b,length);
break;
case 5:length=loadrecord(b);
search_name(b,length);
break;
case 6:length=loadrecord(b);
length=Delete(b,length);
saverecord(b,length);
break;
case 7:length=loadrecord(b);
sort(b,length);
saverecord(b,length);
list(b,length);
break;
case 8:length=loadrecord(b);
count(b,length);
saverecord(b,length);
break;
//case 9:length=loadrecord(b);
unborrow(b,length);
break;
case 0:exit(0);
}
}
}
int menu()
{
int c;
system("mode con:cols=400 lines=30000");
system("color 5f");
printf("\t\t\t\t\t-------------------------------------------------\n");
printf("\t\t\t\t\t+ 图书馆管理系统 +\n");
printf("\t\t\t\t\t-------------------------------------------------\n");
printf("\t\t\t\t\t\t\t *************\n");
printf("\t\t\t\t\t\t\t 1.初始化图书信息\n");
printf("\t\t\t\t\t\t\t 2.添加图书信息\n");
printf("\t\t\t\t\t\t\t 3.显示图书信息\n");
printf("\t\t\t\t\t\t\t 4.按图书编号查询图书信息\n");
printf("\t\t\t\t\t\t\t 5.按图书名称查询图书信息\n");
printf("\t\t\t\t\t\t\t 6.按图书名称删除图书信息\n");
printf("\t\t\t\t\t\t\t 7.按图书名称从小到大排序图书信息\n");
printf("\t\t\t\t\t\t\t 8.按价格统计图书信息\n");
printf("\t\t\t\t\t\t\t 9.显示未借出图书信息\n");
printf("\t\t\t\t\t\t\t 0.退出\n");
printf("\t\t\t\t\t\t\t *************\n");
do{
printf("\n 请输入你的选择(0-9):");
if(scanf("%d",&c)!=1)
{ while(getchar()!='\n')
continue;
continue;
}
}while(c<0||c>9);
return c;}
int input(struct library book[])
{
int i,n;
system("cls");
printf("\t输入图书的数量: \n");
scanf("\t\t\t\t%d",&n);
for(i=0;i<n;i++)
{
printf("\n\t第%d条记录的图书编号: ",i+1);
scanf("\t%d",&book[i].book_num);
printf("\t第%d条记录的图书名称: ",i+1);
scanf("\t%s",book[i].book_name);
printf("\n\t第%d条记录的图书作者姓名: ",i+1);
scanf("\t%s",book[i].author_name);
printf("\n\t第%d条记录的图书价格: ",i+1);
scanf("\t%f",&book[i].book_price);
printf("\n\t第%d条记录的图书出版社: ",i+1);
scanf("\t%s",book[i].publisher);
printf("\t第%d条记录的图书出版时间: ",i+1);
scanf("\t%d-%d-%d",&book[i].publish_time.year,&book[i].publish_time.month,&book[i].publish_time.day);
printf("\t第%d条记录的图书存在状态(y/n):",i+1);
scanf("\t%s",book[i].book_state);
printf("\t第%d条记录的读者姓名: ",i+1);
scanf("\t%s",book[i].reader_name);
printf("\t第%d条记录的读者学号:",i+1);
scanf("\t%d",&book[i].reader_num);
}
printf("\t***********************************************************\n");
printf("\t 已输入成功!\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
return n;
}
int add(struct library book [],int n)//添加图书信息
{
int add(struct library book[],int n);
int i,m;
system("cls");
printf("\n 请输入新图书的记录:\n");
scanf("%d",&m);
printf("开始追加记录\n");
for(i=n;i<n+m;i++)
{system("cls");
printf("\n\t第%d条记录的图书编号: ",i+1);
scanf("\t%d",&book[i].book_num);
printf("\t第%d条记录的图书名称: ",i+1);
scanf("\t%s",book[i].book_name);
printf("\n\t第%d条记录的图书作者姓名: ",i+1);
scanf("\t%s",book[i].author_name);
printf("\n\t第%d条记录的图书价格: ",i+1);
scanf("\t%f",&book[i].book_price);
printf("\n\t第%d条记录的图书出版社: ",i+1);
scanf("\t%s",book[i].publisher);
printf("\t第%d条记录的图书出版时间: ",i+1);
scanf("\t%d-%d-%d",&book[i].publish_time.year,&book[i].publish_time.month,&book[i].publish_time.day);
printf("\t第%d条记录的图书存在状态(y/n):",i+1);
scanf("\t%s",book[i].book_state);
printf("\t第%d条记录的读者姓名: ",i+1);
scanf("\t%s",book[i].reader_name);
printf("\t第%d条记录的读者学号:",i+1);
scanf("\t%d",&book[i].reader_num);
}
return(n+m);
}
void list(struct library book[],int m)//显示图书信息
{
int i;
system("cls");
printf("\t图书馆图书信息如下:\n");
printf("\t图书编号\t图书名称\t作者\t单价\t出版社\t\t出版时间\t存在状态\t读者姓名\t读者学号\n");
for(i=0;i<m;i++)
{
printf("\t%6d\t%12s\t%10s\t%4.2f\t%6s\t",book[i].book_num,book[i].book_name,
book[i].author_name,book[i].book_price,book[i].publisher);
printf("\t%4d-%2d-%2d\t%2s\t%6s\t%8d\t\n",book[i].publish_time,book[i].book_state,
book[i].reader_name,book[i].reader_num);
}
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
void search_num(struct library book[],int n)//按图书编号查询图书信息
{
int i,flag=0;
char bnum[10];
system("cls");
printf("\n请输入要查询的图书编号:");
scanf("%s",bnum);
for(i=0;i<n;i++)
{
if(strcmp(bnum,book[i].book_num)==0)
{
flag=1;
system("cls");
printf("\t此书的信息如下:\n");
printf("\t图书编号\t图书名称\t作者\t单价\t出版社\t\t出版时间\t存在状态\t读者姓名\t读者学号\n");
printf("\t%6d\t%12s\t%10s\t%4.2f\t%6s\t",book[i].book_num,book[i].book_name,
book[i].author_name,book[i].book_price,book[i].publisher);
printf("\t%4d-%2d-%2d\t%2s\t%6s\t%8d\t\n",book[i].publish_time,book[i].book_state,
book[i].reader_name,book[i].reader_num);
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
}
if(flag==0)
{
printf(" 没有此书!\n");
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
}
void search_name(struct library book[],int n)//按图书名称查询图书信息
{
int i,flag=0;
char bname[10];
system("cls");
printf("\n请输入要查询的图书名:");
scanf("%s",bname);
for(i=0;i<n;i++)
{
if(strcmp(bname,book[i].book_name)==0)
{
flag=1;
system("cls");
printf("\t此书的信息如下:\n");
printf("\t图书编号\t图书名称\t作者\t单价\t出版社\t\t出版时间\t存在状态\t读者姓名\t读者学号\n");
printf("\t%6d\t%12s\t%10s\t%4.2f\t%6s\t",book[i].book_num,book[i].book_name,
book[i].author_name,book[i].book_price,book[i].publisher);
printf("\t%4d-%2d-%2d\t%2s\t%6s\t%8d\t\n",book[i].publish_time,book[i].book_state,
book[i].reader_name,book[i].reader_num);
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");//system函数代表执行系统命令,system("cls")就是执行命令”清屏“的意思。//
}
}
if(flag==0)
{
printf(" 没有此书!\n");
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
}
int Delete(struct library book[],int n)
{
int i,j,flag=0;
char bname[10];
char ch='n';
system("cls");
printf("请输入要删除的图书名字\n");
scanf("%s",bname);
for(i=0;i<n;i++)
{
if(strcmp(bname,book[i].book_name)==0)
{
flag=1;
printf("\t此书的信息如下:\n");
printf("\t图书编号\t图书名称\t作者\t单价\t出版社\t\t出版时间\t存在状态\t读者姓名\t读者学号\n");
printf("\t%6d\t%12s\t%10s\t%4.2f\t%6s\t",book[i].book_num,book[i].book_name,
book[i].author_name,book[i].book_price,book[i].publisher);
printf("\t%4d-%2d-%2d\t%2s\t%6s\t%8d\t\n",book[i].publish_time,book[i].book_state,
book[i].reader_name,book[i].reader_num);
printf("是否要删除此条信息!(Y/N): ");
ch=getchar();
ch=getchar();
if(ch=='y'||ch=='Y')
{
for(j=i;j<n;j++)
book[j]=book[j+1];
n--;
printf("成功删除图书!\n");
}
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
}
if(flag==0)
{
printf("\t 查无此书!\n");
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
return n;
}
void sort(struct library book[],int n)//按图书名称从小到大排序
{
//未编写
}
void count(struct library book[],int n)
{
int i,count1=0;float p;
system("cls");
printf("请输入任意一个价格:");
scanf("%f",&p);
for(i=0;i<n;i++)
{
if(book[i].book_price>p)
count1++;
}
printf("\t图书的数量为:%d \n\n\n\n",count1);
printf("\t***********************************************************\n");
printf("\t press any key enter menu……\n");
printf("\t***********************************************************\n");
getchar();
getchar();
system("cls");
}
void unborrow(struct library book[],int n)//显示未借出信息
{
//未编写
}
void saverecord(struct library book[],int m)
{
int i;
FILE *fp;
if((fp=fopen("record.txt","w"))==NULL)
{
printf("can not open file\n");
exit(1);
}
for(i=0;i<m;i++)
fwrite(&book[i],sizeof(struct library),1,fp);
fclose(fp);
}
int loadrecord(struct library book[])
{int n=0;
FILE *fp;
if((fp=fopen("record.txt","r"))==NULL)
{
printf("Can not open file\n");
exit(0);
}
while(fread(&book[n],sizeof(struct library),1,fp)) n++;
fclose(fp);
printf("从文件中成功读出记录\n");
return n;
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。