#数组#类#面向对象设计#控制台
可以借鉴,莫要抄袭。每个高校都会将大作业查重
由于本人在完成大作业期间经历了十分痛苦的查询资料,设计代码等过程,只是为了能符合给出的大作业规范,现在将完成的代码发出,希望能够拯救那些刚学完c/c++基础就被要求设计系统的学生(可能会有一些bug存在,有需求者自行更正)
#include<iostream>
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include<cstdlib>
#include<conio.h>
#include<cmath>
#include <string>
#include<cstring>
#include<fstream>
#include<algorithm>
#include <vector>
#define endl '\n'
int BookNumber = 0;//记录图书总量
int UserNumber=0;//记录用户数量
int byy=2005;
using namespace std;
void usermenu()//登陆页面
{
cout<<"---------------------"<<endl;
cout << "图书管理系统" << endl;
cout << endl;
cout<<"1.登录";
cout<<endl;
cout<<"2.注册";
cout<<endl;
}
void makeMenu()//用户界面
{
cout << "----------------------" << endl;
cout << endl;
cout << "1.【登记图书】" << endl;
cout << endl;
cout << "2.【浏览图书】" << endl;
cout << endl;
cout << "3.【借阅图书】" << endl;
cout << endl;
cout << "4.【归还图书】" << endl;
cout << endl;
cout << "5.【查找图书】" << endl;
cout << endl;
cout << "0.【退出】" << endl;
cout << endl;
cout << "----------------------" << endl;
cout << endl;
//用户菜单
}
class Book//图书类定义
{
public:
string name;
double price;
int num;
string writer;
string getname()
{
return name;
}
double getprice()
{
return price;
}
int getnum()
{
return num;
}
string getwriter()
{
return writer;
}
void setname(string ne)
{
name = ne;
}
void setprice(double prc)
{
price = prc;
}
void setnum(int nm)
{
num = nm;
}
void setwriter(string wr)
{
writer = wr;
}
};
class DifferentBook :public Book//不同书类继承图书类
{
public:
string type;
string bookstr()
{
string bs = name + "," + to_string(price) + "," + to_string(num) + "," + type + "," + writer+",";
return bs;
}
void settype(string newtype)//运行多态,应对不同图书类型
{
type = newtype;
}
void settype(int n)//运行多态,应对不同图书类型
{
type = n;
}
void settype(char a)//运行多态,应对不同图书类型
{
type = a;
}
string gettype()
{
return type;
}
};
class User
{
public:
string username;
string password;
string getusername()
{
return username;
}
string getpassword()
{
return password;
}
void setusername(string un)
{
username=un;
}
void setpassword(string pd)
{
password=pd;
}
string userstr()
{
string us=username+","+password+",";
return us;
}
};
User user[1000000];
DifferentBook book[1000];
void loaduser()
{
ifstream file("users.txt");
if (file.is_open())
{
string line;
int i = 0;
getline(file, line);
for (i = 0; i < UserNumber; i++)//循环加载信息
{
user[i].username = line.substr(0, line.find(","));
line = line.substr(line.find(",") + 1);
user[i].password = line.substr(0, line.find(","));
line = line.substr(line.find(",") + 1);
}
file.close();
}
}
void savesuer()
{
ofstream file("users.txt");
if (file.is_open())
{
for (int i = 0; i < UserNumber; i++)
{
file << user[i].userstr();
}
file.close();
}
}
void loadusernumber()
{
ifstream file("UserNumber.txt");
if (file.is_open())
{
string userflag;
getline(file, userflag);
UserNumber = stoi(userflag);
}
file.close();
}
void saveusernumber()
{
ofstream file("UserNumber.txt");
if (file.is_open())
{
file << UserNumber;
cout << "\n";
}
file.close();
}
void denglu()
{
string username;
string userpassword;
cout<<"请输入账号"<<endl;
cin>>username;
cout<<"请输入密码"<<endl;
cin>>userpassword;
int i=0;
int b=0;
int flag=0;
for(i=0;i<=UserNumber;i++)
{
string namedemo=user[i].getusername();
string passworddemo=user[i].getpassword();
if(username==namedemo)
{
if(userpassword==passworddemo)
{
flag=1;
byy=0321;
}
else
{
flag=2;
}
b=i;
}
}
if(flag==1)
{
cout<<"用户:";
cout<<user[b].getusername();
cout<<"登录成功";
cout<<endl;
}
else if(flag==0)
{
cout<<"账户错误";
cout<<endl;
}
else if(flag==2)
{
cout<<"用户:";
cout<<user[b].getusername();
cout<<"密码错误";
cout<<endl;
}
}
void zhuce()
{
string username;
string userpassword;
cout<<"请输入账号"<<endl;
cin>>username;
cout<<"请输入密码"<<endl;
cin>>userpassword;
user[UserNumber].setusername(username);
user[UserNumber].setpassword(userpassword);
UserNumber++;
savesuer();
saveusernumber();
}
void base()//登陆界面开关
{
int n;
cin>>n;
switch(n)
{
case 1:
denglu();
break;
case 2:
zhuce();
break;
default:
cout << "【 未输入有效指令 】" ;
cout << "\n" ;
cout << "【按任意键退出】" ;
cout << "\n" ;
exit(0);//在输入无关数据后退出防止卡死
break;
}
}
void saveBookNumber()//将图书数量信息保存到文件
{
ofstream file("BookNumber.txt");
if (file.is_open())
{
file << BookNumber ;
cout << "\n" ;
}
file.close();
}
void saveBooks()// 将图书信息保存到文件
{
ofstream file("books.txt");
if (file.is_open())
{
for (int i = 0; i < BookNumber; i++)
{
file << book[i].bookstr();
}
file.close();
}
}
void loadBookNumber()//从文件中加载图书数量信息
{
ifstream file("BookNumber.txt");
if (file.is_open())
{
string bookflag;
getline(file, bookflag);
BookNumber = stoi(bookflag);
}
file.close();
}
void add()//添加书的函数(相同书籍不合并)
{
cout << "【 登记 】" << "\n";
cout << "输入书籍的信息\n";
cout << "name:";
cout << "\n" ;
string newname;
cin >> newname;
book[BookNumber].setname(newname);
cout << "price:" ;
cout << "\n" ;
double newprice;
cin >> newprice;
book[BookNumber].setprice(newprice);
cout << "number:" ;
cout << "\n" ;
int newnum;
cin >> newnum;
book[BookNumber].setnum(newnum);
cout << "type:" ;
cout << "\n" ;
string newtype;
cin >> newtype;
book[BookNumber].settype(newtype);
cout << "writer:" ;
cout << "\n" ;
string newwriter;
cin >> newwriter;
book[BookNumber].setwriter(newwriter);
BookNumber++;
saveBookNumber();//记录图书数量
ofstream file("books.txt");//向文件输入文件数据
if (file.is_open())
{
for (int i = 0; i < BookNumber; i++)
{
file << book[i].bookstr();
}
file.close();
//数据输入完毕
}
}
//byy专属水印
void Borrow()//banyuyou建立的借阅函数
{
int count1 = 0;
int a = 0;
cout << "【 借阅 】" << "\n";
cout << "【请输入借阅的书名】" ;
cout << "\n" ;
cout << "name:" ;
cout << "\n" ;
string bookname1;
cin >> bookname1;
cout << endl;
loadBookNumber();//获取图书数量
ifstream file("books.txt");//从文件book.txt提取图书信息
if (file.is_open())
{
string line;
int i = 0;
getline(file, line);
for (i = 0; i < BookNumber; i++)//循环加载信息
{
book[i].name = line.substr(0, line.find(","));//名字加载
line = line.substr(line.find(",") + 1);//在逗号后寻找
book[i].price = stod(line.substr(0, line.find(",")));//价格加载
line = line.substr(line.find(",") + 1);
book[i].num = stoi(line.substr(0, line.find(",")));//数量加载
line = line.substr(line.find(",") + 1);
book[i].type = line.substr(0, line.find(","));//类型加载
line = line.substr(line.find(",") + 1);
book[i].writer = line.substr(0,line.find(","));//作者加载
line=line.substr(line.find(",")+1);
}
file.close();
//数据获取完毕
}
for (int i = 0; i <= BookNumber; i++)//查找是否有本书
{
string ownname1 = book[i].getname();
if (ownname1 == bookname1)//书名对比
{
count1 = 1;//标记是否查找到
a = i;//标记位置
}
}
if (count1 == 1)
{
if (book[a].num >= 1)
{
cout << "【借阅成功】" ;
cout << "\n" ;
book[a].num--;//数量减1
}
else
{
cout << "【当前书本暂无库存,借阅失败!】" ;
cout << "\n" ;
}
}
else
{
cout << "【没有找到相关书籍,无法借阅。】" ;
cout << "\n" ;
}
saveBookNumber();//存储图书数量
saveBooks();//存储图书数据
}
//byy专属水印
void Back()//banyuyou建立的归还函数
{
int count2 = 0;
int b = 0;
cout << "【 归还 】" ;
cout << "\n" ;
cout << "【请输入归还的书名】" ;
cout << "\n" ;
cout << "name:" ;
cout << "\n" ;
string bookname2;
cin >> bookname2;
loadBookNumber();//获取图书数量
ifstream file("books.txt");//从文件book.txt提取图书信息
if (file.is_open())
{
string line;
int i = 0;
getline(file, line);
for (i = 0; i < BookNumber; i++)//循环加载信息
{
book[i].name = line.substr(0, line.find(","));//名字加载
line = line.substr(line.find(",") + 1);//在逗号后寻找
book[i].price = stod(line.substr(0, line.find(",")));//价格加载
line = line.substr(line.find(",") + 1);
book[i].num = stoi(line.substr(0, line.find(",")));//数量加载
line = line.substr(line.find(",") + 1);
book[i].type = line.substr(0, line.find(","));//类型加载
line = line.substr(line.find(",") + 1);
book[i].writer = line.substr(0,line.find(","));//作者加载
line=line.substr(line.find(",")+1);
}
file.close();
//数据获取完毕
}
for (int i = 0; i <= BookNumber; i++)//查找是否有本书
{
string ownname2 = book[i].getname();
if (ownname2 == bookname2)//书名对比
{
count2 = 1;
b = i;
}
}
if (count2 == 1)
{
book[b].num++;//图书数量加一
cout << "【归还成功,感谢您的使用。】" ;
cout << "\n" ;
}
else
{
cout << "【非本馆书籍,无法归还!】" ;
cout << "\n" ;
}
saveBookNumber();//存储图书数量
saveBooks();//存储图书数据
}
//byy专属水印
void Find()//banyuyou建立的查找函数
{
cout << "【 查找 】" ;
cout << "\n" ;
cout << "【请输入要查找的书籍】" ;
cout << "\n" ;
cout << "name:" ;
cout << "\n" ;
string bookname3;
cin >> bookname3;
cout << "书名";
cout << "\t";
cout << "价格";
cout << "\t";
cout << "数量";
cout << "\t";
cout << "类型";
cout << "\t";
cout << "作者";
cout << "\n" ;
loadBookNumber();//获取图书数量
ifstream file("books.txt");//从文件book.txt提取图书信息
if (file.is_open())
{
string line;
int i = 0;
getline(file, line);
for (i = 0; i < BookNumber; i++)//循环加载信息
{
book[i].name = line.substr(0, line.find(","));//名字加载
line = line.substr(line.find(",") + 1);//在逗号后寻找
book[i].price = stod(line.substr(0, line.find(",")));//价格加载
line = line.substr(line.find(",") + 1);
book[i].num = stoi(line.substr(0, line.find(",")));//数量加载
line = line.substr(line.find(",") + 1);
book[i].type = line.substr(0, line.find(","));//类型加载
line = line.substr(line.find(",") + 1);
book[i].writer = line.substr(0,line.find(","));//作者加载
line=line.substr(line.find(",")+1);
}
file.close();
//数据获取完毕
}
for (int i = 0; i <= BookNumber; i++)//查找是否有本书
{
string ownname3 = book[i].getname();
if (ownname3 == bookname3)//书名对比
{
cout << book[i].getname();
cout << "\t";
cout << book[i].getprice();
cout << "\t";
cout << book[i].num;
cout << "\t";
cout << book[i].gettype();
cout << "\t";
cout << book[i].getwriter();
cout << "\n" ;
}
}
cout << "【搜索完毕】" ;
cout << "\n" ;
}
//byy专属水印
void List()//banyuyou建立的浏览函数
{
cout << "【 浏览 】" << "\n";
cout << "书名";
cout << "\t";
cout << "价格";
cout << "\t";
cout << "数量";
cout << "\t";
cout << "类型";
cout << "\t";
cout << "作者";
cout << endl;
//此处为输出建立的列表所含项目
loadBookNumber();//获取图书数量
ifstream file("books.txt");//从文件book.txt提取图书信息
if (file.is_open())
{
string line;
int i = 0;
getline(file, line);
for (i = 0; i < BookNumber; i++)//循环加载信息
{
book[i].name = line.substr(0, line.find(","));//名字加载
line = line.substr(line.find(",") + 1);//在逗号后寻找
book[i].price = stod(line.substr(0, line.find(",")));//价格加载
line = line.substr(line.find(",") + 1);
book[i].num = stoi(line.substr(0, line.find(",")));//数量加载
line = line.substr(line.find(",") + 1);
book[i].type = line.substr(0, line.find(","));//类型加载
line = line.substr(line.find(",") + 1);
book[i].writer = line.substr(0,line.find(","));//作者加载
line=line.substr(line.find(",")+1);
}
file.close();//关闭文件
//数据获取完毕
}
if(BookNumber!=0)
{
for (int i = 0; i < BookNumber; i++)
{
cout << book[i].name;
cout << "\t";
cout << book[i].price;
cout << "\t";
cout << book[i].num;
cout << "\t";
cout << book[i].type;
cout << "\t";
cout << book[i].writer;
cout << endl;
}
}
else
{
cout << "【暂无图书入库】" ;
cout << endl ;
}
}
void key()//交互switch函数选择界面
{
int t;
cin >> t;
switch (t)//用户界面交互开关
{
case 0://退出操作
cout << "【 退出成功 】" << "\n";
exit(0);
break;
case 1://录入书籍操作
add();
break;
case 2://按顺序输出图书
List();
break;
case 3://借阅功能(由于相同书籍数量未合并,所以查找时会有多条提示,不影响使用)
Borrow();
break;
case 4:
Back();
break;
case 5:
Find();
break;
default:
cout << "【 未输入有效指令 】" ;
cout << "\n" ;
cout << "【按任意键退出】" ;
cout << "\n" ;
exit(0);//在输入无关数据后退出防止卡死
break;
}
}
int main()
{
while(byy==2005)
{
loadusernumber();
loaduser();
usermenu();
base();
system("pause");
system("cls");
}
while (byy==0321)//建立循环在交互后可以返回用户菜单
{
makeMenu();
key();
system("pause");
system("cls");
}
system("pause");//清屏
return 0;//好习惯
//2024/1/11/12:00;(完成)
}