#include<iostream>
#include<fstream>
#define All_books 20
#define All_borrow 3
#define All_admin 5
using namespace std;
struct Book
{
string borrow[1][2];
Book* next=NULL;
};
struct User
{
string id_num;
string password;
Book* head=NULL;
int identifier = 0;
User* next = NULL;
int borrow_num = 0;
};
struct Admin
{
int id_num = 0;
string password;
};
User* head = NULL;
User* trace = NULL;
Admin manag[All_admin];
string books[All_books][2];
int si__ze = 0;
void loading();
void add_books();
void gain_books();
void file(User* t);
void user_set();
void user_register();
void user_login();
void user_operate(User* t);
void borrow(User* t);
void cancle(User* t);
void search(User* t);
void correct(User* t);
void admin_set();
void admin_register();
void admin_login();
void admin_operate();
void admin_search();
void file(User* t)
{
Book* trv;
fstream users_list;
users_list.open("users_list.txt", ios::out|ios::app);
users_list << "用户id:" << t->id_num << " "
<< "借阅数量:" << t->borrow_num << " " << "借阅书目:";
for (trv = t->head; trv; trv = trv->next)
{
for (int n = 0; n < 2; n++)
{
users_list << trv->borrow[0][n];
}
users_list << " ";
}
users_list << endl;
users_list.close();
}
void gain_books()
{
fstream books_list;
books_list.open("books_list.txt", ios::in);
for (int i = 0; i < All_books; i++)
{
for (int n = 0; n < 2; n++)
{
books_list >> books[i][n];
}
}
}
void correct(User* t)
{
string password;
cout << "请输入新密码:" << endl;
cin >> password;
t->password = password;
cout << "修改成功" << endl;
user_operate(t);
}
void loading()
{
int judge;
cout << "请选择身份" << endl;
cout << "1--用户" << " " << "2--管理员" << endl;
while (cin >> judge)
{
if (judge == 1)
{
user_set();
}
if (judge == 2)
{
admin_set();
}
}
}
void user_set()
{
int judge;
cout << "1--用户注册" << endl;
cout << "2--用户登录" << endl;
cout << "0--返回主菜单" << endl;
while (cin >> judge)
{
if (judge == 1)
{
user_register();
}
if (judge == 2)
{
user_login();
}
if (judge == 0)
{
loading();
}
}
}
void user_register()
{
string id;
User* users;
string password;
cout << "请输入账号" << endl;
while (cin >> id)
{
users = new User;
if (head == NULL)
{
users->id_num = id;
trace = head;
cout << "请输入密码" << endl;
cin >> password;
users->password = password;
head = users;
trace = head;
break;
}
if (head != NULL)
{
trace->next = users;
users->id_num = id;
cout << "请输入密码" << endl;
cin >> password;
users->password = password;
trace = trace->next;
break;
}
}
cout << "注册成功" << endl;
user_operate(trace);
}
void user_login()
{
string id;
User* t;
string password;
if (head == NULL)
{
cout << "无用户注册" << endl;
user_set();
}
cout << "请输入用户id" << endl;
while (cin >> id)
{
for (t = head; t; t = t->next)
{
if (id == t->id_num)
{
cout << "请输入密码" << endl;
while (cin >> password)
{
if (password == t->password)
{
user_operate(t);
}
cout << "密码错误,请重新输入" << endl;
}
}
}
cout << "用户名错误,请重新输入" << endl;
}
}
void user_operate(User* t)
{
cout << "1--借阅图书" << endl;
cout << "2--取消借阅" << endl;
cout << "3--查询借阅" << endl;
cout << "4--修改密码" << endl;
cout << "0--返回菜单" << endl;
int judge;
while (cin >> judge)
{
if (judge == 1)
{
borrow(t);
}
if (judge == 2)
{
cancle(t);
}
if (judge == 3)
{
search(t);
}
if (judge == 4)
{
correct(t);
}
if (judge == 0)
{
user_set();
}
}
}
void borrow(User* t)
{
int k=0;
int judge;
int borrow_num;
Book* create=NULL,* follow=NULL;
string book_num;
cout << "请输入借阅数量" << endl;
while (cin >> borrow_num)
{
if (borrow_num > 0 && borrow_num <= All_borrow)
{
t->borrow_num = borrow_num;
break;
}
}
for (int i = 0; i < borrow_num; )
{
if (t->head == NULL)
{
create = new Book;
cout << "请输入书序" << endl;
while (cin >> book_num)
{
for (k = 0; k < All_books; k++)
{
if (book_num == books[k][0])
{
create->borrow[0][0] = books[k][0];
create->borrow[0][1] = books[k][1];
t->identifier = 1;
t->head = create;
follow = create;
k == All_books;
i++;
}
}
if (k == All_books)
{
break;
}
if (t->identifier == 0)
{
cout << "此书序不存在,请重新输入,或按0返回菜单" << endl;
cin >> judge;
if (judge == 0)
{
user_operate(t);
}
}
}
}
if (t->head != NULL && follow != NULL)
{
create = new Book;
cout << "请输入书序" << endl;
while (cin >> book_num)
{
for (k = 0; k < All_books; k++)
{
if (book_num == books[k][0])
{
create->borrow[0][0] = books[k][0];
create->borrow[0][1] = books[k][1];
t->identifier = 1;
follow->next = create;
follow = follow->next;
k == All_books;
i++;
}
}
if (k == All_books)
{
break;
}
if (t->identifier == 0)
{
cout << "此书序不存在,请按1重新输入,或按0返回菜单" << endl;
cin >> judge;
if (judge == 0)
{
user_operate(t);
}
}
}
}
}
if (t->identifier == 1)
{
file(t);
user_operate(t);
}
}
void cancle(User* t)
{
Book* trv=t->head;
string book_num;
if (trv->next == NULL)
{
head = NULL;
user_operate(t);
}
cout << "请输入要退订的书序" << endl;
cin >> book_num;
if (trv->next != NULL)
{
for (trv; trv; trv = trv->next)
{
if (book_num == trv->next->borrow[0][0])
{
trv->next = trv->next->next;
}
}
cout << "退订成功" << endl;
user_operate(t);
}
}
void search(User* t)
{
int judge;
Book* trv = t->head;
cout << "------------------------" << endl;
cout << "用户id:";
cout << t->id_num << endl;
cout << "已借阅书目:" << endl;
for (trv; trv; trv=trv->next)
{
cout << trv->borrow[0][0];
cout << " ";
cout << trv->borrow[0][1];
cout << endl;
}
cout << "------------------------" << endl;
cout << "0--返回菜单" << endl;
while (cin >> judge)
{
user_operate(t);
}
}
void admin_set()
{
int judge;
int permission;
cout << "1--管理员注册" << endl;
cout << "2--管理员登录" << endl;
cout << "0--返回主菜单" << endl;
while (cin >> judge)
{
if (judge == 1)
{
cout << "请输入注册码" << endl;
while (cin >> permission)
{
if (permission == 505)
{
admin_register();
}
}
}
if (judge == 2)
{
admin_login();
}
if (judge == 0)
{
loading();
}
}
}
void admin_register()
{
int id;
string password;
cout << "请输入账号" << endl;
while (cin >> id)
{
manag[si__ze].id_num = id;
break;
}
cout << "请输入密码" << endl;
while (cin >> password)
{
manag[si__ze].password = password;
break;
}
si__ze++;
cout << "注册成功" << endl;
admin_operate();
}
void admin_login()
{
int id;
string password;
if (manag[0].id_num == 0)
{
cout << "无管理员,请先注册" << endl;
admin_register();
}
cout << "请输入管理员id" << endl;
while (cin >> id)
{
for (int i = 0; i < si__ze; i++)
{
if (id == manag[i].id_num)
{
cout << "请输入密码" << endl;
while (cin >> password)
{
if (password == manag[i].password)
{
admin_operate();
}
cout << "密码错误,请重新输入" << endl;
}
}
}
cout << "此id未注册,请重新输入" << endl;
}
}
void admin_operate()
{
int judge;
cout << "1--借阅查询" << endl;
cout << "2--添加书目" << endl;
cout << "0--返回菜单" << endl;
while (cin >> judge)
{
if (judge == 1)
{
admin_search();
}
if (judge == 2)
{
add_books();
}
if (judge == 0)
{
admin_set();
}
}
}
void admin_search()
{
int judge;
User* t=head;
Book* trv = t->head;
if (head == NULL)
{
cout << "无用户注册" << endl;
admin_set();
}
cout << "图书借阅查询" << endl;
for (t = head; t; t = t->next)
{
if (t->identifier != 0)
{
cout << "------------------------" << endl;
cout << "用户id:";
cout << t->id_num << endl;
cout << "已借阅书目:" << endl;
for (trv; trv; trv=trv->next)
{
cout << trv->borrow[0][0];
cout << " ";
cout << trv->borrow[0][1];
cout << endl;
}
}
cout << "------------------------" << endl;
}
cout << "0--返回菜单" << endl;
while (cin >> judge)
{
if (judge == 0)
{
admin_set();
}
}
}
void add_books()
{
int judge;
fstream books_list;
books_list.open("books_list.txt", ios::app);
for (int i = 0; i < All_books; i++)
{
cout << "请写入书籍信息:书序+书名" << endl;
for (int j = 0; j < 2; j++)
{
cin >> books[i][j];
books_list << books[i][j] << " ";
}
books_list << endl;
}
cout << "0--返回主菜单" << endl;
while (cin >> judge)
{
if (judge == 0)
{
books_list.close();
loading();
}
}
}
int main()
{
gain_books();
loading();
}
|