IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 1.2.1使用结构体链表编写的图书管理系统(2021.8.11) -> 正文阅读

[数据结构与算法]1.2.1使用结构体链表编写的图书管理系统(2021.8.11)

/*
1.2.1
相较于上一版本,用户信息及借阅信息都使用了链表进行储存,最终都会保存在文档内;
同时加入了文件读写的功能,可以在程序开始运行时读取文档里的书籍信息,也可以由管理员操作进行书籍信息录入;
*/

#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;                         //用户id
	string password;                       //用户密码
	Book* head=NULL;                       //此链表用于保存用户借阅信息
	int identifier = 0;                    //用户借阅标识符(已借阅的人为1;未借阅的人为0)
	User* next = NULL;                     //指向下一位用户
	int borrow_num = 0;                    //借书的数量
};
struct Admin
{
	int id_num = 0;                //管理员id
	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 gain_users()                  //读取已保存的用户信息
{
	User* users_head ,* trv;
	fstream users_list;
	users_list.open("users_list.txt", ios::in);
	if (users_head = NULL)
	{

	}
	for (trv; trv; trv=trv->next)
	{
		for (int n = 0; n < 2; n++)
		{
			users_list >> trv->id_num;
		}
	}
}
*/

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;                          //create用于建立新节点,follow用于跟踪节点
	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)                                      //当借书链表头指针为空时,执行此if
		{
			create = new Book;                                    //此处新建一个节点
			cout << "请输入书序" << endl;
			while (cin >> book_num)
			{
				for (k = 0; k < All_books; k++)                   //用于遍历所有已储存的书籍
				{
					if (book_num == books[k][0])                  //当输入的序号和已储存的书籍序号相同时,执行此if              
					{
						create->borrow[0][0] = books[k][0];       //分别将待借书目的书序和名称都复制到用户结构体中
						create->borrow[0][1] = books[k][1];
						t->identifier = 1;                        //将借阅标识符赋值为1,代表用户已经借阅图书
						t->head = create;                         //将头节点移至create
						follow = create;                          //将follow移至create
						k == All_books;
						i++;
					}
				}
				if (k == All_books)                               //当遍历整个已存书籍目录后,跳出while循环
				{
					break;
				}
				if (t->identifier == 0)                           //书序不存在时,标识符仍为0
				{
					cout << "此书序不存在,请重新输入,或按0返回菜单" << endl;
					cin >> judge;
					if (judge == 0)
					{
						user_operate(t);
					}
				}
			}
		}
		if (t->head != NULL && follow != NULL)                    //当借书链表头指针不为空时,执行此if
		{
			create = new Book;                                    //此处新建一个节点
			cout << "请输入书序" << endl;
			while (cin >> book_num)
			{
				for (k = 0; k < All_books; k++)                   //用于遍历所有已储存的书籍
				{
					if (book_num == books[k][0])                  //当输入的序号和已储存的书籍序号相同时,执行此if              
					{
						create->borrow[0][0] = books[k][0];       //分别将待借书目的书序和名称都复制到用户结构体中
						create->borrow[0][1] = books[k][1];
						t->identifier = 1;                        //将借阅标识符赋值为1,代表用户已经借阅图书
						follow->next = create;                    //将两个节点链接
						follow = follow->next;                    //将follow移至下一节点
						k == All_books;
						i++;
					}
				}
				if (k == All_books)                               //当遍历整个已存书籍目录后,跳出while循环
				{
					break;
				}
				if (t->identifier == 0)                           //书序不存在时,标识符仍为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赋值为空,跳出此函数
	{
		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++;                   //注册人数+1
	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)    //仅输出标识符为1(已借书)的用户
		{
			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();
		}
	}
}

//main
int main()
{
	gain_books();                  //初始获取书籍信息
	loading();
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 16:53:24  更:2021-08-12 16:56:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 4:15:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码