前言:你说这是你的大作业?>>>虽然看起来很简单,很普通,但它确实是大作业
,你可以从中发现无限乐趣…
ヾ(◍°∇°◍)ノ゙——
目录:
【1】客户需求描述(题目描述):
【2】难点分析:
难点1️⃣(题目本身)
-
最开始看到这个作业时,我是很不以为然的,毕竟都是提前自学过的了( •̀ ω •́ )✧,可——
-
我还是太弱小了ಥ_ಥ
-
看看这变态需求
-
1:支持大数据,比如书籍记录突破百万,用户数量突破万级规模
-
2:搜索时性能考察,调查、思考、设计加强搜索性能的方式
-
3.样本数据
难点2️⃣(卷神同学)
- 同学A😎:我要用Qt,做一个。
很多同学不知道Qt是什么,这里简单地介绍一下。
> < 这是Qt的软件
这:你可以在Codeblock看到它
》这是简单的界面,他教我写的
还有…
- 同学B😼:我要创个桌面程序!!!
可能还有朋友不知道C++怎么弄桌面应用
看这里——
`
- 同学C😈:你们都在编程,我整个网页
他自学了前端,学了HTML,还参加过网页设计比赛
这是他发给我的软件
- 同学D👻:mySQL选手
这个是我猜的≡(▔﹏▔)≡
学院那么大,来个学完数据库的同学,不过分吧》》》》
我都来不及装软件ヽ(≧□≦)ノ
【3】开始——
世界纵已内卷,编程不容躺平
编程必须强!!!💥🐉
【4】代码实现:
注:使用CB编写,VS需要重写时间函数
Libray(图书馆),Labray属拼写错误
1️⃣Labray.h
#ifndef LABRAY_H
#define LABRAY_H
#include<iostream>
#include<string>
#include<cstring>
#include<windows.h>
#include<functional>
#include<algorithm>
using namespace std;
#include<vector>
#include<ctime>
#include<unordered_map>
//----------------------------------引用的库
//界面美化
void toxy(int x, int y); //将光标移动到X,Y坐标处
void SetColorAndBackground(int ForgC, int BackC); //设置颜色
//----------------------
//-------图书类,管理员类,读者类的设定
class Book {
public:
string Book_name;
string IBSN;
string writer;
string classify;
int cur_num;
Book() {
}
Book(string n,string i,string w,string c,int num):Book_name(n),IBSN(i),writer(w),classify(c),cur_num(num){
}
};
class Administrator
{
friend class Labray;
public:
string a_account;
string a_code;
Administrator() {
};
Administrator(string account, string code) :a_account(account), a_code(code) {
}
};
class User
{
friend class Labray;
public:
string u_account;
string u_code;
unordered_map<string,int>u_memo;
unordered_map<string,int>u_back_memo;
User(){
};
User(string account, string code) :u_account(account), u_code(code) {
}
};
//-------
//------------------------------
class Labray
{
private:
unordered_map<string, Administrator>Admins;//用哈希表查找,更加快
unordered_map<string, User>Users;
vector<Book>tmbook;
vector<string>Borrow_memo;
vector<string>Back_memo;
string Landed_accout="0";//C++11标准,可直接这样初始化用,这里使用是为了后面的部分操作
//---------------------------
void init_book();
void init_Admin();
void init_User();
void init_Borrow_memo();
void init_Back_memo();
void Save_Book();
void Save_Admins();
void Save_Users();
void Save_Borrow_memo();
void Save_Back_memo();
//----------------------
void Search_thebook();
void myover(); //强制退出
//---------------------
void AdminStart();
void UserStart();
string getCur_time();
public:
Labray();
void Landing();
};
#endif // LABRAY_H
2️⃣Labray.cpp
#include "Labray.h"
#include<windows.h>
#include<fstream>
constexpr auto Bookfile = "H_books.txt";
constexpr auto Adminfile = "Admins.txt";
constexpr auto Usersfile = "users.txt";
constexpr auto Borrowfile="Borrow_memo.txt";
constexpr auto Backfile="Back_memo.txt";
//----------------------------------辅助函数,帮助查找
class help_find{
//,辅助find_if查找,无需泛型编程,就不用写模板
private:
string name; //你可以用适配器,只要你能正确匹配函数即可
public:
help_find(string t_name):name(t_name){
};
bool operator()(const Book& book) {
if (book.Book_name == name)return true;
else return false;
}
};
int str_to_num(string str){
int jie=0;
for(int i=0;i<str.size();i++){
jie=jie*10+(str[i]-'0');
}
return jie;
}
//-----------------------------------------------------设置
void SetColorAndBackground(int ForgC, int BackC) {
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void toxy(int x, int y) //将光标移动到X,Y坐标处
{
COORD pos = {
x , y };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
return ;
}
//对界面设置函数的定义,这两个函数,记住就行了,现阶段不要求理解
//----------------------------------------------------
//--获取时间
string Labray::getCur_time(){
time_t t; //typedef long time_t;
time(&t); //获取系统时间
char* str = ctime(&t); //将时间t转换为字符串
return str;
}
//-------------------关键的构造函数,初始化
Labray::Labray(){
ifstream ifs1;
ifs1.open(Bookfile, ios::in);
if (!ifs1.is_open()) {
//情况一:文件未创建,即文件不存在
//初始化属性
cout << "-----------------------------------------------------" << endl;
cout << "提示:--!!!图书信息文件未创建,运行系统可自动生成文件!!! --" << endl;
cout << "-----------------------------------------------------" << endl;
ifs1.close();
return;
}
//2.文件存在,但数据为空
char ch;
ifs1 >> ch;
if (ifs1.eof()) {
cout << "--------------------------------------------------------" << endl;
cout << "提示:--!!!图书馆尚无存书,等待管理员添加图书!!!--" << endl;
cout << "--------------------------------------------------------" << endl;
ifs1.close();
return;
}
//图书馆有图书,我们需要把文件中的图书信息读到程序中
ifs1.close();
this->init_book();
this->init_Admin();
this->init_User();
this->init_Borrow_memo();
this->init_Back_memo();
SetColorAndBackground(1,0);
cout << "------------------------- " << endl;
cout << "[H]系统现有【用户人数】为:" << this->Users.size() << endl;
cout << "[H]系统现有【管理员人数】为:" << this->Admins.size() << endl;
cout << "[H]系统现有【图书种类】为:" << this->tmbook.size() << endl;
cout << "------------------------- " << endl;
SetColorAndBackground(7,0);
cout << endl;
}
//---------------------
void Labray:: Save_Book() {
//保存图书文件
ofstream ofs;
ofs.open(Bookfile, ios::out);//写文件
//将每个人的数据写入文件中
for (int i = 0; i < this->tmbook.size(); i++) {
ofs << this->tmbook[i].Book_name << ' '
<< this->tmbook[i].IBSN << ' '
<< this->tmbook[i].writer<<' '<<
this->tmbook[i].classify <<' '<<this->tmbook[i].cur_num<<endl;
}
ofs.close();
return;
}
void Labray::Save_Admins() {
ofstream ofs;
ofs.open(Adminfile, ios::out);//写文件
//将每个人的数据写入文件中
for (pair<string,Administrator> it:this->Admins) {
ofs << it.second.a_account << ' '
<< it.second.a_code<< endl;
}
ofs.close();
return;
}
void Labray::Save_Users() {
ofstream ofs;
ofs.open(Usersfile, ios::out);//写文件
//将每个人的数据写入文件中
for (pair<string, User> it : this->Users) {
ofs << it.second.u_account << ' '
<< it.second.u_code << endl;
}
ofs.close();
return;
}
void Labray::Save_Borrow_memo(){
ofstream ofs;
ofs.open(Borrowfile,ios::out);
//写入借阅记录
for(string it:this->Borrow_memo){
ofs<<it<<endl;
}
ofs.close();
return;
}
void Labray::Save_Back_memo(){
ofstream ofs;
ofs.open(Backfile,ios::out);
//写入借阅记录
for(string it:this->Back_memo){
ofs<<it<<endl;
}
ofs.close();
return;
}
void Labray::init_book() {
ifstream ifs;
ifs.open(Bookfile, ios::in);
string name;
string IBSN;
string writer;
string classify;
string tmp_cur_num;
while (ifs >>name && ifs >>IBSN && ifs >>writer && ifs >>classify && ifs >>tmp_cur_num) {
int cur_num=str_to_num(tmp_cur_num);
Book tmp(name,IBSN,writer,classify,cur_num);
this->tmbook.push_back(tmp);
}
ifs.close();
return;
}
void Labray::init_Admin() {
ifstream ifs;
ifs.open(Adminfile, ios::in);
string account, code;
while (ifs >> account && ifs >>code) {
Administrator tmp(account,code);
this->Admins[tmp.a_account]=tmp;
}
ifs.close();
if(this->Admins.empty()){
Administrator tmp("20220101","123456");
this->Admins["20220101"]=tmp;//添加默认管理员
this->Save_Admins();
}
return;
}
void Labray::init_User() {
ifstream ifs;
ifs.open(Usersfile, ios::in);
string account;
string code;
while (ifs >> account && ifs >> code) {
User tmp(account,code);
this->Users[tmp.u_account] = tmp;
}
ifs.close();
return;
}
void Labray::init_Borrow_memo() {
ifstream ifs;
ifs.open(Borrowfile, ios::in);
string informantion;
while (getline(ifs,informantion)) {
this