校园图书馆模拟器极其简陋版

写程序的日记

花了十多个小时完成老师布置的任务(应该算吧=D)
还有些小问题仍待解决。
这里算是自己的一个小的努力的纪念碑吧
bug:

  1. 学生能还老师借的书(笑死)
  2. 还书还是有点小bug??没有检查出大问题吧?
    (笑)
    于2021、6、21重新更新了一点,可以输出日志。
    还是懒得修bug =D;
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<fstream>
#include<sstream>
#include<map>
#include<iostream>
#include<vector>
//#pragma warning(disable:4996);
using namespace std;
string ID, mima;
class BOOK {
private:
	string nam;
	int left = 0;
	int day = -99;
	string who="NONE";
	bool tech_only = 0;//0为所有人可 1仅老师可借阅
public:
	BOOK() {}
	BOOK(string name, int left, string who, int day, bool tech_only)
	{
		this->nam = name;
		this->left = left;
		this->day = day;
		this->who = who;
		this->tech_only = tech_only;
	}
	friend ostream& operator <<(ostream& input, BOOK& t);
	friend ifstream& operator >> (ifstream& output, BOOK& t);
	string get_nam()
	{
		return nam;
	}
	int get_day()
	{
		return day;
	}
	int get_left()
	{
		return left;
	}
	string get_who()
	{
		return who;
	}
	bool get_tech()
	{
		return tech_only;
	}
	void change(string a,int day)
	{
		this->left = 0;
		this->day = day;
		this->who = a;
	}
	void book_return()
	{
		this->left = 1;
		this->day = -99;
		this->who = "NONE";
	}
};
class EBOOK :public BOOK
{
private:

public:
	EBOOK(string name, int left, string who, int day, bool tech_only) :BOOK(name, left, who, day, tech_only)
	{

	}
	friend ostream& operator <<(ostream& input, EBOOK& t);
	friend ifstream& operator >> (ifstream& output, EBOOK& t);
};
class DVD :public BOOK
{
private:

public:
	DVD(string name, int left, string who, int day, bool tech_only) :BOOK(name, left, who, day, tech_only)
	{

	}
	friend ostream& operator <<(ostream& input, DVD& t);
	friend istream& operator >> (istream& output, DVD& t);
};

ostream& operator <<(ostream& input, BOOK& t)  //book 输出
{
	input << "名称:" << t.nam;// << "  还书天数" << t.day << endl;
	if (t.left)
	{
		input << "   可借阅\n" << endl;
	}
	else 
	{
		input << "  还书天数" << t.day << endl << endl;
	}
	return input;
}
ifstream& operator >> (ifstream& output, BOOK& t)
{
	output >> t.nam >> t.left >> t.who >> t.day >> t.tech_only;
	return output;
} 
ostream& operator <<(ostream& input, EBOOK& t)  //EBOOK输出
{
	input << "名称:" << t.get_nam();// << "  还书天数" << t.get_day() << endl;
	if (t.get_left())
	{
		input << "   可借阅\n" << endl;
	}
	else
	{
		input << "  还书天数" << t.get_day() << endl << endl;
	}
	return input;
}
ifstream& operator >> (ifstream& output, EBOOK& t)
{
	return output;
}
ostream& operator <<(ostream& input, DVD& t)   //DVD输出
{
	input << "名称:" << t.get_nam();// << "  还书天数" << t.get_day() << endl;
	if (t.get_left())
	{
		input << "   可借阅\n" << endl;
	}
	else
	{
		input << "  还书天数" << t.get_day() << endl << endl;
	}
	return input;
}
istream& operator >> (istream& output, DVD& t)
{
	//output >> t.nam;
	return output;
}
vector<BOOK>book;
vector<EBOOK>ebook;
vector<DVD>dvd;
string Who_did;

int length_book = 0;
int length_ebook = 0;
int length_dvd = 0;
int did_book[1001];
int did_ebook[1001];
int did_dvd[1001];

int length_book_return = 0;
int length_ebook_return = 0;
int length_dvd_return = 0;
int did_book_return[1001];
int did_ebook_return[1001];
int did_dvd_return[1001];
void solution() //录入书本
{
	ifstream fin("book.txt");
	if (!fin)
	{
		cout << "不能打开book输入文件" << endl;
		return;
	}
	string c; //name
	int i;//借出状态
	int day;//剩余天数
	string ch;//谁借的
	bool tech_only;//仅仅老师能借的
	BOOK t; //test
	while (fin >> t)
	{
		book.push_back(t);
	}
	fin.close();
	ifstream fin1("ebook.txt");
	if (!fin1)
	{
		cout << "不能打开ebook输入文件" << endl;
		return;
	}
	while (fin1 >> c >> i >> ch >> day>> tech_only)
	{
		EBOOK t(c, i, ch, day, tech_only);
		ebook.push_back(t);
	}
	fin1.close();
	ifstream fin2("dvd.txt");
	if (!fin2)
	{
		cout << "不能打开dvd输入文件" << endl;
		return;
	}
	while (fin2 >> c >> i >> ch >> day>> tech_only)
	{
		DVD t(c, i, ch, day, tech_only);
		dvd.push_back(t);
	}
	fin2.close();
}
void add()//添加书本
{
	cout << "添加总数 :";
	int N;
	cin >> N;//总数
	cout << endl;
	for (int i = 1; i <= N; i++)
	{
		cout << "输入书本类型: \n书本: B \n电子书: E\nDVD: D\n ";
		string aaa;
		cin >> aaa;
		if (aaa == "B")
		{
			string name;
			cout << "输入书名: ";
			cin >> name;
			BOOK t(name, 1, "NONE", -99, 0);
			book.push_back(t);
		}
		else if (aaa == "E")
		{
			string name;
			cout << "输入书名: ";
			cin >> name;
			EBOOK t(name, 1, "NONE", -99, 0);
			ebook.push_back(t);
		}
		else if (aaa == "D")
		{
			string name;
			cout << "输入书名: ";
			cin >> name;
			DVD t(name, 1, "NONE", -99, 0);
			dvd.push_back(t);
		}
	}

}
void Adm_display()
{
	cout << "BOOK : \n";
	for (int i = 0; i < book.size(); i++)
	{
		cout << book[i];
		cout << "借阅者" << book[i].get_who() << endl;
	}
	cout << "EBOOK  :  \n";
	for (int i = 0; i < ebook.size(); i++)
	{
		cout << ebook[i];
		cout << "借阅者" << ebook[i].get_who() << endl;
	}
	cout << "DVD :  \n";
	for (int i = 0; i < dvd.size(); i++)
	{
		cout << dvd[i];
		cout << "借阅者" << dvd[i].get_who() << endl;
	}
}
void Log()
{
	cout << "退出文件夹中可查看" << endl;
}
void Adm()
{
	while (1)
	{
		cout << "查询 请输入 L" << endl;
		cout << "如果添加书籍 按 A" << endl;
		cout << "导出日志 OUT" << endl;
		cout << "退出 输入END" << endl;
		string DID;
		try
		{
			cin >> DID;
			if (!(DID == "A" || DID == "END" || DID == "L" || DID == "OUT"))
				throw 1;
		}
		catch (int)
		{
			cout << "输入错误 退出程序" << endl;
			return;
		}
		if (DID == "A")
		{
			add();
		}
		else if (DID == "L")
		{
			Adm_display();
		}
		else if (DID == "END")
		{
			return;
		}
		else if (DID == "OUT")
		{
			Log();
		}
	}

}
void stu_brow()
{
	int Time = 0;
	while (1)
	{
		if (Time > 3)
		{
			cout << "学生超出今日能借书范围" << endl;
			return;
		}
		Time++;
		cout << "请输入借书类型 :BOOK ,EBOOK ,DVD :";
		string typ;
		cin >> typ;
		if (typ == "EBOOK")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i <= ebook.size(); i++)
			{
				if (name == ebook[i].get_nam())
				{
					if (!ebook[i].get_left())
					{
						cout << "可恶书被抢走了 (〃>目<) orz" << endl;
						return;
					}
					else
					{
						ebook[i].change("stu",40);
					}
					length_ebook++;
					did_book[length_ebook] = i;
					break;
				}
				else if (i == ebook.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else if (typ == "BOOK")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i < book.size(); i++)
			{
				if (name == book[i].get_nam())
				{
					if (!book[i].get_left())
					{
						cout << "可恶书被抢走了 (〃>目<) orz" << endl;
						return;
					}
					else
					{
						book[i].change("stu", 40);
					}
					length_book++;
					did_book[length_book] = i;
					break;
				}
				else if (i == book.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else if (typ == "DVD")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i < dvd.size(); i++)
			{
				if (name == dvd[i].get_nam())
				{
					if (!dvd[i].get_left())
					{
						cout << "可恶书被抢走了 (〃>目<) orz" << endl;
						return;
					}
					else
					{
						dvd[i].change("stu", 40);
					}
					length_dvd++;
					did_dvd[length_dvd] = i;
					break;
				}
				else if (i == dvd.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else
		{
			cout << "输入错误退出" << endl;
			return;
		}
		cout << "继续借阅请输入 C 其他返回";
		string a;
		cin >> a;
		if (a != "C")
		{
			return;
		}
	}
}
void all_return()
{
	while (1)
	{
		cout << "请输入还书类型 :BOOK ,EBOOK ,DVD :";
		string typ;
		cin >> typ;
		if (typ == "BOOK")
		{
			cout << "请输入所还书名 :";
			string name;
			cin >> name;
			for (int i = 0; i < book.size(); i++)
			{
				if (book[i].get_nam() == name)
				{
					if (book[i].get_day() == 0)
					{
						cout << "超时还书扣钱(●'◡'●) orz" << endl;
					}
					else
					{
						book[i].book_return();
					}
					length_book_return++;
					did_book_return[length_book_return] = i;
					break;
				}
				else if (i == book.size() - 1)
				{
					cout << "书名有误" << endl;
					return;
				}
			}
		}
		else if (typ == "EBOOK")
		{
			cout << "请输入所还书名 :";
			string name;
			cin >> name;
			for (int i = 0; i < ebook.size(); i++)
			{
				if (ebook[i].get_nam() == name)
				{
					if (ebook[i].get_day() == 0)
					{
						cout << "超时还书扣钱(●'◡'●) orz" << endl;
						break;
					}
					else
					{
						ebook[i].book_return();
					}
					length_ebook_return++;
					did_ebook_return[length_ebook_return] = i;
					break;
				}
				else if (i == ebook.size() - 1)
				{
					cout << "书名有误" << endl;
					return;
				}
			}
		}
		else if (typ == "DVD")
		{
			cout << "请输入所还书名 :";
			string name;
			cin >> name;
			for (int i = 0; i < dvd.size(); i++)
			{
				if (dvd[i].get_nam() == name)
				{
					if (dvd[i].get_day() == 0)
					{
						cout << "超时还书扣钱(●'◡'●) orz" << endl;
						break;
					}
					else
					{
						dvd[i].book_return();
					}
					length_dvd_return++;
					did_dvd_return[length_dvd_return] = i;
					break;
				}
				else if (i == dvd.size() - 1)
				{
					cout << "书名有误" << endl;
					return;
				}
			}
		}
		else
		{
			cout << "输入有误" << endl;
			return;
		}
		cout << "继续请输入 C 退出任意键盘" << endl;
		string a;
		cin >> a;
		if (a != "C")
		{
			cout << "退出" << endl;
			return;
		}
	}
}
void tec_brow()
{
	int Time = 0;
	while (1)
	{
		if (Time > 15)
		{
			cout << "老师超出今日能借书范围" << endl;
			return;
		}
		Time++;
		cout << "请输入借书类型 :BOOK ,EBOOK ,DVD :";
		string typ;
		cin >> typ;
		if (typ == "EBOOK")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i <= ebook.size(); i++)
			{
				if (name == ebook[i].get_nam())
				{
					if (ebook[i].get_left())
					{
						ebook[i].change("teacher", 360);
					}
					else
					{
						cout << "咩有了呢 /(ㄒoㄒ)/~~" << endl;
						return;
					}
					length_ebook++;
					did_ebook[length_ebook] = i;
					break;
				}
				else if (i == ebook.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else if (typ == "BOOK")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i <= book.size(); i++)
			{
				if (name == book[i].get_nam())
				{
					if (book[i].get_left())
					{
						book[i].change("teacher", 360);
					}
					else
					{
						cout << "咩有了呢 /(ㄒoㄒ)/~~" << endl;
						return;
					}
					length_book++;
					did_book[length_book] = i;
					break;
				}
				else if (i == book.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else if (typ == "DVD")
		{
			cout << "请输入借阅书目 : ";
			string name;
			cin >> name;
			for (int i = 0; i <= dvd.size(); i++)
			{
				if (name == dvd[i].get_nam())
				{
					if (dvd[i].get_left())
					{
						dvd[i].change("teacher", 360);
					}
					else
					{
						cout << "咩有了呢 /(ㄒoㄒ)/~~" << endl;
						return;
					}
					length_dvd++;
					did_dvd[length_dvd] = i;
					break;
				}
				else if (i == dvd.size() - 1)
				{
					cout << "输入书名有误" << endl;
				}
			}
		}
		else
		{
			cout << "输入错误退出" << endl;
			return;
		}
		cout << "继续借阅请输入 C 其他返回";
		string a;
		cin >> a;
		if (a != "C")
		{
			return;
		}
	}
}
void display(bool tec_only)
{
	if (tec_only)
	{
		cout << "BOOK : \n\n";
		for (int i = 0; i < book.size(); i++)
		{
			cout << book[i];
		}
		cout << "EBOOK  :  \n\n";
		for (int i = 0; i < ebook.size(); i++)
		{
			cout << ebook[i];
		}
		cout << "DVD :  \n\n";
		for (int i = 0; i < dvd.size(); i++)
		{
			cout << dvd[i];
		}
	}
	else
	{
		cout << "BOOK : \n\n";
		for (int i = 0; i < book.size(); i++)
		{
			if(!book[i].get_tech())
				cout << book[i];
		}
		cout << "EBOOK  :  \n\n";
		for (int i = 0; i < ebook.size(); i++)
		{
			if (!ebook[i].get_tech())
				cout << ebook[i];
		}
		cout << "DVD :  \n\n";
		for (int i = 0; i < dvd.size(); i++)
		{
			if (!dvd[i].get_tech())
				cout << dvd[i];
		}
	}
}
void did_stu()
{
	cout << "现有书目: " << endl;
	display(0);
	while (1)
	{
		cout << "借书 B\n还书 R\n";
		cout << "返回请输入 Q\n" << endl;
		string wh; //操作
		try
		{
			cin >> wh;
			if (!(wh == "B" || wh == "R" || wh == "Q"))
			{
				throw 1;
			}
		}
		catch (int)
		{
			cout << "错误";
			return;
		}
		if (wh == "B")
		{
			stu_brow();
		}
		else if (wh == "R")
		{
			all_return();
		}
		else if (wh == "Q")
		{
			cout << "退出系统" << endl;
			return;
		}
	}
}
void did_teacher()
{
	cout << "现有书目: " << endl;
	display(1);
	while (1)
	{
		cout << "借书 B\n还书 R\n";
		cout << "返回请输入 Q\n";
		string wh; //操作
		try
		{
			cin >> wh;
			if (!(wh == "B" || wh == "R" || wh == "Q"))
			{
				throw 1;
			}
		}
		catch (int)
		{
			cout << "错误";
			return;
		}
		if (wh == "B")
		{
			tec_brow();
		}
		else if (wh == "R")
		{
			all_return();
		}
		else if (wh == "Q")
		{
			return;
		}
	}
}
void login()
{
	cout << "借书 输入 Y 管理员 输入 N 其他退出程序" << endl;
	string s;
	cin >> s;
	if (s == "Y")
	{
		string Nm;
		cout << "学生请输入S 老师请输入T" << endl;
		cin >> Nm;
		//cout << (Nm != "T" && Nm != "S");
		Who_did = Nm;//学生S 老师T
		try
		{
			if (Nm != "T" && Nm != "S")
				throw 1;
		}
		catch (int)
		{
			cout << "错误";
			exit(0);
		}
		solution();
		if (Nm == "S")
		{
			did_stu();
		}
		else if (Nm == "T")
		{
			did_teacher();
		}
	}
	else if (s == "N")
	{
		cout << "ID:";
		cin >> ID;
		cout << endl;
		cout << "密码:";
		cin >> mima;
		if (ID == "123" && mima == "000")
		{
			cout << "登录成功" << endl;
			solution();
			Adm();
		}
		else
		{
			cout << "登录失败" << endl;
			exit(0);
		}
	}
}
void final()
{
	ofstream fout("book.txt");
	if (!fout)
	{
		cout << "不能打开book输入文件" << endl;
		return;
	}
	for (int i = 0; i < book.size(); i++)
	{
		string nam = book[i].get_nam();
		int left = book[i].get_left();
		int day = book[i].get_day();
		string who = book[i].get_who();
		int tech_only = book[i].get_tech();
		fout << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	fout.close();


	ofstream fout1("ebook.txt");
	if (!fout1)
	{
		cout << "不能打开ebook输入文件" << endl;
		return;
	}
	for (int i = 0; i < ebook.size(); i++)
	{
		string nam = ebook[i].get_nam();
		int left = ebook[i].get_left();
		int day = ebook[i].get_day();
		string who = ebook[i].get_who();
		int tech_only = ebook[i].get_tech();
		fout1 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	fout1.close();


	ofstream fout2("dvd.txt");
	if (!fout2)
	{
		cout << "不能打开dvd输入文件" << endl;
		return;
	}
	for (int i = 0; i < dvd.size(); i++)
	{
		string nam = dvd[i].get_nam();
		int left = dvd[i].get_left();
		int day = dvd[i].get_day();
		string who = dvd[i].get_who();
		int tech_only = dvd[i].get_tech();
		fout2 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	fout2.close();


	ofstream fout3("LOG.txt");
	if (!fout3)
	{
		cout << "不能打开LOG输入文件" << endl;
		return;
	}
	fout3 << "借出" << endl;

	for (int i = 1; i <= length_book; i++)
	{
		int ttt = did_book[i];
		string nam = book[ttt].get_nam();
		int left = book[ttt].get_left();
		int day = book[ttt].get_day();
		string who = book[ttt].get_who();
		int tech_only = book[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	for (int i = 1; i <= length_ebook; i++)
	{
		int ttt = did_ebook[i];
		string nam = ebook[ttt].get_nam();
		int left = ebook[ttt].get_left();
		int day = ebook[ttt].get_day();
		string who = ebook[ttt].get_who();
		int tech_only = ebook[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	for (int i = 1; i <= length_dvd; i++)
	{
		int ttt = did_dvd[i];
		string nam = dvd[ttt].get_nam();
		int left = dvd[ttt].get_left();
		int day = dvd[ttt].get_day();
		string who = dvd[ttt].get_who();
		int tech_only = dvd[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	fout3 << "归还" << endl;
	for (int i = 1; i <= length_book_return; i++)
	{
		int ttt = did_book_return[i];
		string nam = book[ttt].get_nam();
		int left = book[ttt].get_left();
		int day = book[ttt].get_day();
		string who = book[ttt].get_who();
		int tech_only = book[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	for (int i = 1; i <= length_ebook_return; i++)
	{
		int ttt = did_ebook_return[i];
		string nam = ebook[ttt].get_nam();
		int left = ebook[ttt].get_left();
		int day = ebook[ttt].get_day();
		string who = ebook[ttt].get_who();
		int tech_only = ebook[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	for (int i = 1; i <= length_dvd_return; i++)
	{
		int ttt = did_dvd_return[i];
		string nam = dvd[ttt].get_nam();
		int left = dvd[ttt].get_left();
		int day = dvd[ttt].get_day();
		string who = dvd[ttt].get_who();
		int tech_only = dvd[ttt].get_tech();
		fout3 << nam << " " << left << " " << who << " " << day << " " << tech_only << endl;
	}
	fout3.close();
}
int main()
{
	login();
	final();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值