18011 【课程设计】电子图书馆
时间限制:1000MS 代码长度限制:200KB
提交次数:0 通过次数:0
题型: 人工评判题 语言: 不限定
Description
设计一个电子图书馆,该程序具有以下功能:
(1)多用户,用户登录:输入用户名和密码,密码正确才允许登录。
(2)录入个人图书信息;
(3)给定图书名或编号,修改该图书信息;
(4)给定图书名或编号,删除该图书信息;
(5)根据输入的图书品名或编号,显示查找到的相应图书信息;
(6)根据输入的分类,显示该分类的所有图书信息;
(7)根据输入的时间,查询共享借出到期或1周内将到期的图书信息;
(8)统计功能:统计各分类的图书。
(9)实现单词组合查询图书名,例如输入“设计+教程”查询,可以查到如下图书
《C语言程序设计教程》
《工业设计视频教程》
《JAVA程序设计简易教程》
注:因为上面图书中均包含“设计”和“教程”两个单词
同理,输入“JAVA+设计+教程”查询,则只能查到《JAVA程序设计简易教程》
任务要求:
(1)按照分析、设计、编码、调试和测试过程完成应用程序;
(2)学习并使用流程图等工具,并在撰写报告中使用;
(3)程序的各项功能在程序运行时,以菜单方式选择并执行;
(4)要求用户输入数据时,要给出清晰、明确的提示,包括:输入数据的内容、格式及结束方式等
(5)所有的信息存储在文件中,并实现文件读写操作。
输入格式
提示
(1)图书资料等信息可以设计一个结构体类型
(2)自己构思并增加的除规定功能之外的新功能,酌情加分。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <string>
//#include<ctime>
#include <conio.h>//定义了通过控制台进行 数据输入 和数据输出的函数,如getch函数。
#define LEN sizeof(struct book)
using namespace std;
void page3();
int successful=0;//判断是否登陆成功
int admin;//判断是否有管理员权限
char users[20];
struct book
{
int num,amount;
char name[30];
char author[20];
char type[20];
char time[20];//到期时间
int lend;//是否借出
char user[20];
struct book*next;
};
void returnBooks();//用户还书程序
void lendBooks();//用户借书程序
int statics();//统计图书馆有几本书(根据换行符来判断)
void dueBook();//查询书籍到期以及1周内即将到期的书籍
void perType();//统计各个分类的书籍
void inType();//输入分类,显示该分类的所有图书信息
void reviseBook();//修改图书(用链表找到后,再修改信息)
void findBooks();//查询书籍(通过输入关键字来进行查找)
void findBooksFull();//查询书籍(通过输入详细信息来进行查找)
void deleteBooks();//删除书籍
void addBooks();//添加书籍
void enroll();//创建账号
void logIn();//用户输入账号密码
int pick(char nameIn[20],char passwordIn[20]);//从id文件中提取账号和密码信息并进行匹配
void page1();
void page2();
void page3();
void page4();
void subtitle15();
//void time();
int statics();//查询有几本书
/**
* 退出程序
*/
void end()
{
system("cls");
cout<<" _____ _ __ __ \n"
" /__ \\/\\ /\\/_\\ /\\ \\ \\/\\ /\\/ _\\ \n"
" / /\\/ /_/ //_\\\\ / \\/ / //_/\\ \\ \n"
" / / / __ / _ \\/ /\\ / __ \\ _\\ \\ \n"
" \\/ \\/ /_/\\_/ \\_/\\_\\ \\/\\/ \\/ \\__/ "<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 感谢您的使用! ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
// time();
exit(0);
}
/**
* 打印出电子图书馆(LIBRARY)的标题
*/
void title()
{
subtitle15();
cout<<"--------------------------------------------------------------"<<endl;
cout<<"| ,--. ,--.,-----. ,------. ,---. ,------.,--. ,--. |\n"
"| | | | || |) /_ | .--. ' / O \\ | .--. '\\ `.' / |\n"
"| | | | || .-. \\| '--'.'| .-. || '--'.' '. / |\n"
"| | '--.| || '--' /| |\\ \\ | | | || |\\ \\ | | |\n"
"| `-----'`--'`------' `--' '--'`--' `--'`--' '--' `--' |"<<endl;
}
/**
* 打印出输入账号的副标题
*/
void subtitle1()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 进入系统需要先登陆账号,账号为个人学号 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 打印注册账号的副标题
*/
void subtitle2()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 输在下方输入要注册的学号以及密码(密码不得超过10位) ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 打印登陆成功后的副标题
*/
void subtitle3()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-进入图书管理系统 ★"<<endl;
cout<<" ★ 2-进入图书借阅系统 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 图书管理系统的副标题
*/
void subtitle4()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-添加书籍 ★"<<endl;
cout<<" ★ 2-删除书籍 ★"<<endl;
cout<<" ★ 3-查询书籍 ★"<<endl;
cout<<" ★ 4-修改图书 ★"<<endl;
cout<<" ★ 5-到期书籍 ★"<<endl;
cout<<" ★ 6-浏览图书 ★"<<endl;
cout<<" ★ 7-退出程序 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 添加书籍的副标题
*/
void subtitle5()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 请按要求输入图书相关信息 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 用户登陆信息
*/
void subtitle15()
{
if(successful&&admin==0){
cout<<"--------------------------------------------------------------"<<endl;
cout<<" 用户 "<<users<<" 欢迎使用!"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
else if(successful&&admin==1)
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" 管理员 "<<users<<" 欢迎使用!"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
}
/**
* 删除书籍的副标题
*/
void subtitle6()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-输入需删除书籍的书名 ★"<<endl;
cout<<" ★ 2-输入需删除书籍的编号 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 查找书籍的副标题
*/
void subtitle7()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-需要输入需查询书籍的书名 ★"<<endl;
cout<<" ★ 2-需要输入需查询书籍的编号 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 分类书籍的副标题
*/
void subtitle9()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 输入图书的类别,显示该类别的所有图书 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 分类书籍的副标题
*/
void subtitle11()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 下面为书籍的所有种类 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 图书借阅系统标题
*/
void subtitle10()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-输入类别查询书籍 ★"<<endl;
cout<<" ★ 2-统计目前书籍的种类 ★"<<endl;
cout<<" ★ 3-输入关键字查找书籍 ★"<<endl;
cout<<" ★ 4-借书系统 ★"<<endl;
cout<<" ★ 5-还书系统 ★"<<endl;
cout<<" ★ 6-浏览图书 ★"<<endl;
cout<<" ★ 7-退出程序 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 修改书籍的副标题
*/
void subtitle8()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-需要修改书籍的书名 ★"<<endl;
cout<<" ★ 2-需要修改书籍的编号 ★"<<endl;
cout<<" ★ 3-需要修改书籍的作者 ★"<<endl;
cout<<" ★ 4-需要修改书籍的数量 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 查询到期书籍副标题
*/
void subtitle12()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 输入时间,显示共享到期以及一周内即将到期的书籍 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 借书书籍副标题
*/
void subtitle13()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1.输入需要借的书籍书名 ★"<<endl;
cout<<" ★ 2.输入需要借的书籍编号 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 归还书籍副标题
*/
void subtitle14()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1.输入需要还的书籍书名 ★"<<endl;
cout<<" ★ 2.输入需要还的书籍编号 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 首页排版
*/
void homePage()
{
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1-登陆系统 ★"<<endl;
cout<<" ★ 2-注册账号 ★"<<endl;
cout<<" ★ 3-退出系统 ★"<<endl;
cout<<" ★ 请输入数字进行相应操作 按回车确定 ★ "<<endl;
cout<<"--------------------------------------------------------------"<<endl;
}
/**
* 浏览所有书籍
*/
void view(int t)
{
system("cls");
title();
FILE *fp;
fp = fopen("books.txt", "r");
struct book books{};
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
for(int i=0;i<statics();i++)
{
fscanf(fp, "%s%d%s%d%s%d%s%s", books.name, &books.num, books.author, &books.amount, books.type, &books.lend,
books.time, books.user);
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
}
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
if(t==4)page4();
if(t==3)page3();
}
/**
* 用户还书
*/
void returnBooks()
{
system("cls");
title();
subtitle14();
int judge=1;//是否成功归还
int w;
cin>>w;
if(w==1) {
char lend[40];
cout<<"请输入需还的书名(全名):";
cin>>lend;
int n=0;
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(strcmp(lend,books.name)!=0)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else
{
if(books.lend==0)
{
judge=0;
cout<<"很抱歉!该书未被借出!"<<endl;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount+1;
strcpy(p1->type,books.type);
strcpy(p1->time,"0");
strcpy(p1->user,"0");
p1->lend=0;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
fclose(fp);
}
else if(w==2) {
int lend;
cout<<"请输入需还的编号:";
cin>>lend;
int n=0;
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(books.num!=lend)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else
{
if(books.lend==0)
{
judge=0;
cout<<"很抱歉!该书未被借出"<<endl;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount+1;
strcpy(p1->type,books.type);
strcpy(p1->time,"0");
strcpy(p1->user,"0");
p1->lend=0;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!=NULL ;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
fclose(fp);
}
if(judge) {
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 成功归还! ★" << endl;
cout << "--------------------------------------------------------------" << endl;
}
if(!judge) {
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 归还失败! ★" << endl;
cout << "--------------------------------------------------------------" << endl;
}
printf("\n按任意键返回上一层!");
getch();
page4();
}
/**
* 用户借书
*/
void lendBooks()
{
system("cls");
title();
subtitle13();
int judge=1;//是否成功借出
int w;
cin>>w;
if(w==1) {
char lend[40];
cout<<"请输入需借的书名(全名):";
cin>>lend;
int n=0;
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(strcmp(lend,books.name)!=0)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else
{
if(books.lend==1)
{
judge=0;
cout<<"很抱歉!该书已被借出"<<" 并将在"<<books.time<<"后归还"<<endl;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else
{
cout<<"请输入需要借到何时(格式xxxx.xx.xx):";
char time[20];
cin>>time;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount-1;
strcpy(p1->type,books.type);
strcpy(p1->time,time);
strcpy(p1->user,users);
p1->lend=1;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
fclose(fp);
}
else if(w==2) {
int lend;
cout<<"请输入需借的编号:";
cin>>lend;
int n=0;
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(books.num!=lend)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else
{
if(books.lend==1)
{
judge=0;
cout<<"很抱歉!该书已被借出"<<" 并将在"<<books.time<<"后归还"<<endl;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else
{
cout<<"请输入需要借到何时(格式xxxx.xx.xx):";
char time[20];
cin>>time;
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount-1;
strcpy(p1->type,books.type);
strcpy(p1->time,time);
strcpy(p1->user,users);
p1->lend=1;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next= NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
fclose(fp);
}
if(judge) {
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 成功借出! ★" << endl;
cout << "--------------------------------------------------------------" << endl;
}
if(!judge) {
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 借出失败! ★" << endl;
cout << "--------------------------------------------------------------" << endl;
}
printf("\n按任意键返回上一层!");
getch();
page4();
}
/**
* 统计图书馆有几本书(根据换行符来判断)
*/
int statics()
{
FILE *fp;
fp=fopen("books.txt","r");
int amount=0;
char c;
while((c=fgetc(fp))!=EOF)
{
if(c=='\n')
amount++;
}
fclose(fp);
return amount;
}
/**
* 查询书籍到期以及1周内即将到期的书籍
*/
void dueBook()
{
system("cls");
title();
int i;
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 1.查询已到期的书籍 ★"<<endl;
cout<<" ★ 2.查询一周内到期的书 ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cin>>i;
subtitle12();
int count=0;
cout<<"请输入时间(按照xxxx.xx.xx的格式输入):";
char time[20];
cin>>time;
FILE *fp;
fp = fopen("books.txt", "r");
struct book books{};
if(i==1){
//cout<<"下面为到期的书籍:"<<endl;
cout<<"\n"<<endl;
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 下面为到期的书籍: ★" << endl;
cout << "--------------------------------------------------------------" << endl;
for(int i=0;i<statics();i++)
{
fscanf(fp, "%s%d%s%d%s%d%s%s", books.name, &books.num, books.author, &books.amount, books.type, &books.lend,
books.time, books.user);
if(books.time[0]=='0')continue;
for(int j=0;j<10;j++)
{
if(strcmp(time,books.time)==0)
{
count++;
cout <<"书名:"<< books.name << "\n编号:" << books.num << "\n作者:" << books.author << "\n数目:" << books.amount <<
"\n类型:"<<books.type;
if(books.lend==1)
cout<<"\n是否被借出:被借出"<<"\n归还时间:"<<books.time<<"\n借书者学号:"<<books.user<<"\n"<<endl;
else if(books.lend==0)
cout<<"\n是否被借出:未被借出\n"<<endl;
break;
}
// cout<<time[j]<<" "<<books.time[j]<<endl;
if(time[j]!=books.time[j])
{
if(time[j]>books.time[j]) {
count++;
cout << "书名:" << books.name << "\n编号:" << books.num << "\n作者:" << books.author << "\n数目:"
<< books.amount <<
"\n类型:" << books.type;
if (books.lend == 1)
cout << "\n是否被借出:被借出" << "\n归还时间:" << books.time << "\n借书者学号:" << books.user << "\n" << endl;
else if (books.lend == 0)
cout << "\n是否被借出:未被借出\n" << endl;
break;
}
else{
break;
}
}
}
}
if(count==0)
cout << "书籍均未到期\n " << endl;
fclose(fp);
}
else if (i==2){
//cout<<"下面为一周内到期的书籍:"<<endl;
cout << "--------------------------------------------------------------" << endl;
cout << " ★ 下面为一周内到期的书籍: ★" << endl;
cout << "--------------------------------------------------------------" << endl;
int date;
int month;
int year;
year=(time[0]-'0')*1000+(time[1]-'0')*100+(time[2]-'0')*10+(time[3]-'0');
month=(time[5]-'0')*10+time[6]-'0';
date=(time[8]-'0')*10+time[9]-'0';
// cout<<month<<endl;
if(month==1||month==3||month==5||month==7||month==8||month==10){
if(date+7>31)
{
month+=1;
date=date+7-31;
time[8]='0';
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
} else
{
date =date+7;
time[8]=(char)(date/10+'0');
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
// cout<<time[5]<<time[6]<<time[8]<<time[9]<<endl;
}
}
else if(month==4||month==6||month==9||month==11){
if(date+7>30)
{
month+=1;
date=date+7-30;
time[8]='0';
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
} else
{
date =date+7;
time[8]=(char)(date/10+'0');
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
}
}
else if(month==2&&year%4==0){
if(date+7>29)
{
month+=1;
date=date+7-30;
time[8]='0';
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
} else
{
date =date+7;
time[8]=(char)(date/10+'0');
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
}
}
else if(month==2&&year%4!=0){
if(date+7>28)
{
month+=1;
date=date+7-30;
time[8]='0';
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
} else
{
date =date+7;
time[8]=(char)(date/10+'0');
time[9]=(char)(date%10+'0');
time[6]=(char)(month%10+'0');
time[5]=(char)(month/10+'0');
}
}
else if(month==12){
if(date+7>31)
{
// month=1;
date=date+7-31;
time[8]='0';
time[9]=(char)(date%10+'0');
time[6]=(char)'1';
time[5]=(char)'0';
}
else
{
date =date+7;
time[8]=(char)(date/10+'0');
time[9]=(char)(date%10+'0');
time[6]=(char)'1';
time[5]=(char)'0';
}
}
fp = fopen("books.txt", "r");
for(int i=0;i<statics();i++)
{
fscanf(fp, "%s%d%s%d%s%d%s%s", books.name, &books.num, books.author, &books.amount, books.type, &books.lend,
books.time, books.user);
if(books.time[0]=='0')continue;
for(int j=0;j<10;j++)
{
if(strcmp(time,books.time)==0)
{
count++;
cout <<"书名:"<< books.name << "\n编号:" << books.num << "\n作者:" << books.author << "\n数目:" << books.amount <<
"\n类型:"<<books.type;
if(books.lend==1)
cout<<"\n是否被借出:被借出"<<"\n归还时间:"<<books.time<<"\n借书者学号:"<<books.user<<"\n"<<endl;
else if(books.lend==0)
cout<<"\n是否被借出:未被借出\n"<<endl;
break;
}
// cout<<time[j]<<" "<<books.time[j]<<endl;
if(time[j]!=books.time[j])
{
if(time[j]>books.time[j]) {
count++;
cout << "书名:" << books.name << "\n编号:" << books.num << "\n作者:" << books.author << "\n数目:"
<< books.amount <<
"\n类型:" << books.type;
if (books.lend == 1)
cout << "\n是否被借出:被借出" << "\n归还时间:" << books.time << "\n借书者学号:" << books.user << "\n" << endl;
else if (books.lend == 0)
cout << "\n是否被借出:未被借出\n" << endl;
break;
}
else{
break;
}
}
}
}
if(count==0)
cout << "书籍均未到期" << endl;
fclose(fp);
}
printf("\n按任意键返回上一层!");
getch();
page3();
}
/**
* 统计各个分类的书籍
*/
void perType()
{
system("cls");
title();
subtitle11();
FILE *fp;
fp = fopen("books.txt", "r");
struct book books{};
char type[50][20];
int i=0;
for(;i<statics();i++)
{
fscanf(fp, "%s%d%s%d%s%d%s%s", books.name, &books.num, books.author, &books.amount, books.type, &books.lend,
books.time, books.user);
strcpy(type[i],books.type);
}
for(int j=0;j<=i;j++)
for(int t=j+1;t<=i;t++)
{
if(strcmp(type[j],type[t])==0)
break;
if(t==i)
cout<<type[j]<<endl;
}
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
page4();
}
/**
* 输入分类,显示该分类的所有图书信息
*/
void inType() {
system("cls");
title();
subtitle9();
cout << "请输入需要查找书的类别:";
char type[20];
cin >> type;
FILE *fp;
fp = fopen("books.txt", "r");
struct book books{};
cout<<"下面为查询书籍的结果:"<<endl;
for (int i = 0; i < statics(); i++) {
fscanf(fp, "%s%d%s%d%s%d%s%s", books.name, &books.num, books.author, &books.amount, books.type, &books.lend,
books.time, books.user);
if(strcmp(books.type,type)==0)
{
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
}
}
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
page4();
}
/**
* 修改图书(用链表找到后,再修改信息)
*/
void reviseBook()
{
system("cls");
title();
subtitle7();
FILE *fp;
int n=0;
fp=fopen("books.txt","r");
int k=0,num=0;
char find[50];
cin>>k;
if(k==1)
{
cout<<"请输入需要修改的图书名:";
scanf("%s",find);
struct book *head= NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
// cout<<books.name<<endl;
if(strcmp(books.name,find)!=0)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else if(strcmp(books.name,find)==0)
{
system("cls");
//这里需要清下屏幕
title();
subtitle8();
int w=0;
cin>>w;
if(w==1)
{
char reviseBook1[30];
cout<<"请输入修改后的书籍名:";
cin>>reviseBook1;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,reviseBook1);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else if(w==2)
{
int reviseNum;
cout<<"请输入修改后的编号:";
cin>>reviseNum;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=reviseNum;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
if(w==3)
{
char auth[30];
cout<<"请输入修改后的作者名:";
cin>>auth;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,auth);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else if(w==4)
{
int reviseAmount;
cout<<"请输入修改后数量的:";
cin>>reviseAmount;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=p1->num;
p1->amount=reviseAmount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
}
else if(k==2)
{
int num1;
cout<<"请输入需要修改的编号:";
cin>>num1;
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(num1!=books.num)
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
else if(num1==books.num)
{
system("cls");
//这里需要清下屏幕
title();
subtitle8();
int w=0;
cin>>w;
if(w==1)
{
char reviseBook1[30];
cout<<"请输入修改后的书籍名:";
cin>>reviseBook1;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,reviseBook1);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else if(w==2)
{
int reviseNum;
cout<<"请输入修改后的编号:";
cin>>reviseNum;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=reviseNum;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
if(w==3)
{
char auth[30];
cout<<"请输入修改后的作者名:";
cin>>auth;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,auth);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
else if(w==4)
{
int reviseAmount;
cout<<"请输入修改后数量的:";
cin>>reviseAmount;
//下面进行修改
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=p1->num;
p1->amount=reviseAmount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
}
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
}
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 修改完成! ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
page3();
}
/**
* 查询书籍(通过输入详细信息来进行查找)
*/
void findBooksFull()
{
system("cls");
title();
subtitle7();
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
int i=0,num=0;
char find[50];
cin>>i;
if(i==1)
{
cout<<"请输入需要查询图书名:";
scanf("%s",find);
int amount=statics();
while(amount--)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(strcmp(books.name,find)==0)
{
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
break;
}
else if(amount==0)
{
cout<<"并未查询到该书!"<<endl;
}
}
}
else if (i==2)
{
cout<<"请输入需要查询书籍的编号:";
cin>>num;
int amount=statics();
while(amount--)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(num==books.num)
{
cout<<"下面为查询书籍的结果:"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
break;
}
else if(amount==0)
{
cout<<"并未查询到该书!"<<endl;
}
}
}
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 查询完成! ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
page3();
}
/**
* 查询书籍(通过输入关键字来进行查找);
*/
void findBooks()
{
system("cls");
title();
subtitle7();
int i, num = 0;
char find[50];
cin>>i;
if(i==1)
{
cout<<"请输入需要查询书籍的关键字(用\"\"+\"\"号链接):";
scanf("%s",find);
}
else if (i==2)
{
cout<<"请输入需要查询图书编号:";
cin>>num;
}
char keys[15][100];
int k = 0, j1 = 0, j2 = 0;//key为关键字并且被记录了下来
if(i==1) {
while (true) {
if (find[j2] == '+') {
keys[k][j1] = '\0';
k++;
j1 = 0;
j2++;
} else if (find[j2] == '\0') {
keys[k][j1] = '\0';
break;
}
keys[k][j1] = find[j2];
j2++;
j1++;
}
}
int amount=statics();
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
while(amount--)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if(i==1) {
string bookName(books.name);
int exit = 1;//判断是否存在
for (int q = 0; q < k; q++) {
if (bookName.find(keys[q], 0) == -1) {
exit = 0;
break;
}
}
if (exit)
{
cout<<"下面为查询书籍的结果:\n"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
}
}
else if(i==2)
{
if(num==books.num) {
cout<<"下面为查询书籍的结果:"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
printf(" 书名 编号 作者 数量 类型 状态\n");
cout<<"--------------------------------------------------------------"<<endl;
cout<<right<<setw(20)<<books.name;
cout<<right<<setw(5)<<books.num;
cout<<right<<setw(15)<<books.author;
cout<<right<<setw(5)<<books.amount;
cout<<right<<setw(10)<<books.type;
if(books.lend==1)
cout<<" 已借"<<endl;
else
cout<<" 未借"<<endl;
break;
}
}
}
fclose(fp);
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 查询完成! ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
printf("\n按任意键返回上一层!");
getch();
page4();
}
/**
* 删除书籍
*/
void deleteBooks()
{
system("cls");
title();
subtitle6();
char deleteBook[30];
int deleteNum = 0;
int choose,n=0;
cin>>choose;
if(choose==1)
{
cout<<"输入需删除书籍的书名:";
cin>>deleteBook;
}
else if (choose ==2)
{
cout<<"输入需删除书籍的编号:";
cin>>deleteNum;
}
struct book *head=NULL;
struct book *p, *p1 = NULL, *p2 = NULL;
struct book books{};
FILE *fp;
fp=fopen("books.txt","r");
for(int i=0;i<statics();i++)
{
fscanf(fp,"%s%d%s%d%s%d%s%s",books.name,&books.num,books.author,&books.amount,books.type,&books.lend,books.time,books.user);
if((choose==1&&strcmp(books.name,deleteBook) != 0)||(choose==2&&books.num!=deleteNum))
{
n++;
if(n==1)
{
p1=p2= (struct book*)malloc(LEN);
head=p1;
}
else {
p2->next = p1;
p2 = p1;
p1 = (struct book *) malloc(LEN);
}
//cout<<listName<<" "<<listAuthor<<endl;
strcpy(p1->name,books.name);
strcpy(p1->author,books.author);
p1->num=books.num;
p1->amount=books.amount;
strcpy(p1->type,books.type);
strcpy(p1->time,books.time);
strcpy(p1->user,books.user);
p1->lend=books.lend;
// cout<<p1->name<<" "<<p1->num<<endl;
}
}
if(n==0)
{
head= NULL;
}
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
fp=fopen("books.txt","w");//清空文件
fclose(fp);
fp=fopen("books.txt","a");
p=head;
for(;p!= NULL;)
{
// cout<<p->name<<" "<<p->num<<endl;
fprintf(fp, "%s %d %s %d %s %d %s %s\n", p->name, p->num, p->author, p->amount,p->type,p->lend,p->time,p->user);
p=p->next;
}
fclose(fp);
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 删除图书信息成功! ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
printf("\n按任意键返回上一层!");
getch();
page3();
}
/**
* 添加书籍
*/
void addBooks()
{
system("cls");
title();
subtitle5();
int addAmount;
struct book books{};
cout<<"请输入需要添加几本书籍:";
cin>>addAmount;
while(addAmount--) {
cout << "请输入需添加书籍的书名:";
scanf("%s", books.name);
cout << "请输入需添加书籍的书籍编号:";
cin >> books.num;
cout << "请输入需添加的书籍的作者:";
scanf("%s", books.author);
cout << "请入书籍的数目:";
cin >> books.amount;
cout<<"请输入书的类型:";
cin>>books.type;
books.lend=0;
books.time[0]='0';books.time[1]='\0';
books.user[0]='0';books.user[1]='\0';
FILE *fp;
fp = fopen("books.txt", "a");
fprintf(fp, "%s %d %s %d %s %d %s %s\n", books.name, books.num, books.author, books.amount,books.type,books.lend,books.time,books.user);
cout<<"--------------------------------------------------------------"<<endl;
cout<<" ★ 书籍添加完成!√ ★"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
fclose(fp);
}
printf("\n按任意键返回上一层!");
getch();
page3();
}
/**
* 创建账号
*/
void enroll(){
system("cls");
title();
subtitle2();
FILE*fp;
char name[20],password[20],password2[20];
fp=fopen("id.txt","a");
cout<<"请输入学号:";cin>>name;
while(true) {
cout << "请输入密码:";
cin >> password;//需要判断第一次和第二次十分相同,不相同则要求重新输入
cout<<"请再次输入密码(密码需一致):";
cin>>password2;
if(strcmp(password,password2)==0)
{
fprintf(fp, "%s %s 0\n", name, password);
cout<<"注册成功!欢迎使用!"<<endl;
break;
}
else cout<<"两次输入密码不一致,请重新输入"<<endl;
}
fclose(fp);
printf("\n按任意键返回上一层!");
getch();
page1();
}
/**
* 从id文件中提取账号和密码信息并进行匹配
*/
int pick(char nameIn[20],char passwordIn[20])
{
FILE*fp;
char name[20],password[20];
fp=fopen("id.txt","r");
for(;!feof(fp);)//检查每一个账号与登陆的十分一致
{
fscanf(fp,"%s%s%d",name,password,&admin);
if(strcmp(nameIn,name)==0&&strcmp(password,passwordIn)==0)
{
fclose(fp);
strcpy(users,name);
return 1;
}
}
fclose(fp);
return 0;
}
/**
* 用户输入账号密码
*/
void logIn()
{
system("cls");
title();
subtitle1();
int correct=1,a;//判断登陆是否成功
int attempt=0;//尝试次数
while(correct){
char name[20],password[20];
cout<<"请输入用户名:";
scanf("%s",name);
cout<<"请输入密码:";
scanf("%s",password);
a=pick(name, password);
if(a==1){
printf("登陆成功! 欢迎使用!\n");
correct = 0;
successful=1;
}
else {cout << "用户名或者密码错误" << endl;attempt++;}//若密码或者用户名输入错误则要求重新输入
if(attempt==3)
{cout<<"输入用户名或者密码错误三次,自动退出程序"<<endl;end();}//输入错误三次以上直接退出程序
}
}
void page4()
{
system("cls");
title();
subtitle10();
int i;
cin>>i;
int t;
t=4;
if(i==1)
{
inType();
}
else if(i==2)
{
perType();
} else if (i==3)findBooks();
else if (i==4)lendBooks();
else if (i==5)returnBooks();
else if (i==6) view(t);
else end();
}
void page3()
{
system("cls");
title();
subtitle4();
int b;
cin>>b;
int t;
t=3;
if(b==1)
{
system("cls");
title();
subtitle5();
addBooks();
}
else if(b==2) deleteBooks();
else if(b==3) findBooksFull();
else if(b==4) reviseBook();
else if(b==5) dueBook();
else if(b==6) view(t);
else if(b==7) end();
}
void page2()
{
system("cls");
title();
subtitle3();
int a;
cin>>a;
if(a==1&&admin==1)
{
page3();
}
else if(a==1&&admin==0)
{
cout<<"很抱歉,没有管理者权限!"<<endl;
printf("\n按任意键返回上一层!");
getch();
page2();
system("cls");
}
else if(a==2)
{
page4();
}
}
void page1()
{
system("cls");
title();
homePage();
// time();
int i;
cin>>i;
if(i==1)
logIn();
else if(i==2)
enroll();
else if(i==3)
end();
if(successful)
{
page2();
}
}
int main() {
page1();
return 0;
}