📄 labrarysysterm.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#define SIZE 10000
FILE *fp;
struct Data
{
int year;
int month;
int day;
}; /*定义日期的结构体*/
struct bookstype
{
int number; /*书的编号,从1开始编号*/
struct Data date; /*书的出版日期*/
char name[20]; /*书名*/
char writer[8]; /*作者名*/
char form[4]; /*书的类型,一般用大写的英文字母(A-Z)表示*/
char lend[5]; /*租借情况1有库存0无库存*/
}books[SIZE];
/*struct reader
{
char name[10];
char banji[20];
int old;
int nub; /*借阅统计*/
/*}student[SIZE_stu];*/
char path(); /*进入系统的路径设置函数*/
void input(); /* 输入书籍 */
int save(); /* 保存书籍 */
void output(); /* 输出书籍 */
void seeknumber(); /* 按书号查询 */
void seekdate(); /* 按日期查询 */
void seekname(); /* 按书名查询 */
void seekwriter(); /* 按作者名查询 */
void seekform(); /* 按书的类型查询 */
void ordernumber(); /* 按书的编号排序 */
void orderdate(); /* 按书的日期排序 */
void orderform(); /* 按书的类型排序 */
void delenumber(); /* 按书号删除书籍 */
void delename(); /* 按书名删除书籍 */
/*int book_borrowed(); /* 书籍借出 */
/*void returnbooks(); /* 还书函数 */
char path()
{
char s[100];
char password[]="1";
/*字符数组password存放正确的口令1*/
int flag=0,n=3;
/*设置口令对错标志flag,初值为0,n为允许输入错误次数*/
char choice='y';
do
{
printf("请输入口令:");
gets(s);
if(!(strcmp(s,password)))
{
printf("\n 欢迎使用该系统! \n");
flag=1;
break;
}
else
if(n>0)
{
printf("输入错误,请再输一次!\n");
n--;
}
}while(n>0);
if(!flag)
{
printf("对不起,你不能使用该系统,再见!\n");
exit(0);
}
return 1;
}
void input()
{
FILE *fp;
struct bookstype books[SIZE];
int i=1;
char choice='y';
if((fp=fopen("D:\\计035.txt","a+"))!=NULL)
{ /*以读或追加写的方式打开文件*/
while(choice=='y'||choice=='Y')
{
printf("————请输入第%d本书的书号,日期,书名,作者,类别以及租借情况:————\n",i);
printf("++++++++ 请输入第%d本书的书号: ++++++++\n",i);
scanf("%d",&books[i].number);
printf("———— 请输入第%d本书的日期: ++++++++\n",i);
scanf("%d.%d.%d",&books[i].date.year,&books[i].date.month,&books[i].date.day);
printf("请输入书名:");
scanf("%s",books[i].name);
printf("请输入书的作者:");
scanf("%s",books[i].writer);
printf("请输入书的类别:");
scanf("%s",books[i].form);
printf("书的库存状态:");
scanf("%s",books[i].lend);
fwrite(&books[i],sizeof(struct bookstype),1,fp); /*将books[i]追加入磁盘文件*/
printf("是否要输入下一本书Y/N\n");
scanf("%s",&choice);
i++;
}
}
fclose(fp);
}
int save() /*将修改过的文件结果写到文件里*/
{
FILE *fp;
int i ;
if ((fp=fopen("D:\\计035.txt","w"))==NULL)
{
printf("\n 不能打开输出文件");
return 0;
}
for (i=0;i<SIZE;i++)
if (fwrite(&books[SIZE],sizeof(struct bookstype),1,fp)!=1)
{
printf("\n写文件时出错");
return 0;
}
fclose(fp);
return 1;
}
void output()
{
FILE *fp;
int i,n=0;
struct bookstype books[SIZE];
fp=fopen("D:\\计035.txt","r");
if((fp=fopen("D:\\计035.txt","r"))==NULL)
{
printf("打开文件出错!\n");
return;
}
else
if(fread(&books[0],sizeof(struct bookstype),1,fp)==0)
{
printf("库存已空!\n");
return;
}
else
{
printf("显示所有书籍:\n");
printf("编号 日期 书名 作者名 类型 状态\n");
printf("********************************************************************\n");
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
printf("%d ",books[i].number);
printf(" %d.%d.%d\t",books[i].date.year,books[i].date.month,books[i].date.day);
printf("%s ",books[i].name);
printf("%s ",books[i].writer);
printf("%s ",books[i].form);
printf("%d\n",books[i].lend);
n++; /*统计书籍*/
}
printf("********************************************************************\n");
}
fclose(fp);
printf("\n总共有%d本书!\n",n);
}
/*以下为查询部分*/
void seeknumber()
{
FILE *fp;
struct booksnumber;
int i,flag=0;
int num;
char choice='y';
fp=fopen("D:\\计035.txt","r");
/*以读的方式打开文件*/
printf("\n请输入你要查询的书的书号:");
scanf("%d",&num);
fflush(stdin); /*缓冲*/
while(choice=='y'||choice=='Y')
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(num==books[i].number)
{
printf("找到所要查的书来,具体资料如下:\n");
printf("编号:%2d\n",books[i].number);
printf("日期:%d,%d,%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("书名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("类型:%s\n",books[i].form);
printf("租借情况:%d<*0为已借走,1为未借*>\n",books[i].lend);
flag=1;
}
}
if(flag==0)
printf("\n没有发现书号为%d的书\n",num);
printf("是否要继续查找Y/N");
scanf("%c",&choice);
};
fclose(fp);
}
void seekdate()
{
FILE *fp;
struct booksdate;
int i,flag=0;
int year1,month1,day1;
char choice='y';
fp=fopen("D:\\计035.txt","r");
printf("\n请输入要查询的书本的日期:");
scanf("%d.%d.%d",&year1,&month1,&day1);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(year1==books[i].date.year&&month1==books[i].date.month&&books[i].date.day)
{
printf("书本的编号是:%2d\n",books[i].number);
printf("书本的日期是:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("书本的名字是:%s\n",books[i].name);
printf("书本的作者是:%s\n",books[i].writer);
printf("书本的类型是:%s\n",books[i].form);
printf("书本是否借了:0为借1为未借%-3s:\n",books[i].lend);
flag=1;
}
}
if(flag==0)
printf("\n没有发现所要找的书!\n");
printf("\n是否要继续查找Y/N\n");
scanf("%c",&choice);
}while(choice=='y'&&choice=='Y');
fclose(fp);
}
void seekname()
{
FILE *fp;
struct booksname;
int i,flag=0;
char choice='y';
char name[10];
fp=fopen("D:\\计035.txt","r");
printf("请输入要查询的书本的书名:");
scanf("%s",name);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(strcmp(name,books[i].name)==0)
{
printf("找到要查找的书,具体资料如下:");
printf("编号:%2d\n",books[i].number);
printf("日期:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("书名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("类型:%s\n",books[i].form);
printf("租借情况(0为已借出,1为未借出):%d\n",books[i].lend);
fflush(stdin);
flag=1;
}
}
if(flag!=1)
{
printf("找不到书名为%s的书!",name);
}
printf("是否要继续查找Y/N");
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
fclose(fp);
}
void seekwriter()
{
FILE *fp;
struct bookswriter;
int i,flag=0;
char writer1[10];
char choice='y';
fp=fopen("D:\\计035.txt","r");
printf("\n请输入要查询的书本的作者名:");
scanf("%s",writer1);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(strcmp(writer1,books[i].writer)==0)
{
printf("找到作者名为%s的书,具体资料如下:\n",writer1);
printf("编号:%2d\n",books[i].number);
printf("日期:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("书名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("类型:%s\n",books[i].form);
printf("租借情况(0为已借出,1为未借出):%s\n",books[i].lend);
flag=1;
}
}
if(flag!=1)
{
printf("\n找不到作者名为%s的书!\n",writer1);
}
printf("是否要继续查找Y/N:");
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
fclose(fp);
}
void seekform()
{
FILE *fp;
struct bookform;
int i,flag=0;
char form1[4];
char choice='y';
fp=fopen("D:\\计035.txt","r");
printf("\n请输入书本的种类:");
scanf("%s",form1);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(form1==books[i].form)
{
printf("\n找到要查询的书,具体资料如下:\n");
printf("编号:%2d\n",books[i].number);
printf("日期:%d.%d.%d",books[i].date.year,books[i].date.month,books[i].date.day);
printf("书名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("类型:%s\n",books[i].form);
printf("租借情况:%s\n",books[i].lend);
flag=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -