C++程序 要求:1.书屋有40个有效用户,普通用户编号从1-39,1-9号用户能借阅的最大书本数量为5,10-39号用
1.书屋有40个有效用户,普通用户编号从1-39,1-9号用户能借阅的最大书本数量为5,10-39号用户能借阅的最大书本数量为32.用户身份认证,用户注册后获得编号,自行...
1.书屋有40个有效用户,普通用户编号从1-39,1-9号用户能借阅的最大书本数量为5,10-39号用户能借阅的最大书本数量为3
2.用户身份认证,用户注册后获得编号,自行设定密码,登录系统时对用户的身份进行认证。
3.用户可按书本编号查询书本库存和自己的借阅记录
4.为书屋设置一位系统管理员,用户编号0,只有管理员可以进行库存整理
只需要给我编第四个就行 全部编的话给双倍财富!!
选择财富的上限只有100 编好的话给500都好说~ 展开
2.用户身份认证,用户注册后获得编号,自行设定密码,登录系统时对用户的身份进行认证。
3.用户可按书本编号查询书本库存和自己的借阅记录
4.为书屋设置一位系统管理员,用户编号0,只有管理员可以进行库存整理
只需要给我编第四个就行 全部编的话给双倍财富!!
选择财富的上限只有100 编好的话给500都好说~ 展开
展开全部
//基本上80%符合了,最后一个管理库存函数没写,不知道是什么意思
//lib.h
#ifndef LIB_H
#define LIB_H
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
struct User;
class Library{
public:
Library(string lib_file);
void proceedLoadingData(const string &file);
void popMenu(string user_name = string());
void proceedOPtions(int option, string &user_name);
User *findUser(const string &user_name);
void userRegister();
void userLogin();
void lookUpBorrowedBook(const string &user_name);
void lookUpBorrowedHistory(const string &user_name);
void addBorrowingBook(const string &user_name);
void deleteBorrowedBook(const string &user_name);
void modifiedUserPassword(const string &user_name);
void manageLibrary();
void saveAndExit();
static int max_user;
private:
string data_file;
User *m_user;
int m_userSize;
User *m_loginUser;
};
struct User{
User();
string name;
int number;
string password;
int borrowed;
string history;
string book_isbn;
};
#endif
//lib.cpp
#include "lib.h"
#include <functional>
int Library::max_user = 40;
Library::Library(string lib_file)
:data_file(lib_file), m_loginUser(NULL){
proceedLoadingData(lib_file);
if(m_userSize == 0){
m_user = new User[40];
m_user[0].name = "Admin";
m_user[0].password = "Admin";
m_user[0].number = 0;
m_user[0].borrowed = 0;
m_user[0].book_isbn = "null";
m_user[0].history = "null";
m_userSize++;
}
}
void Library::proceedLoadingData(const string &file){
ifstream fin;
fin.open(file.c_str());
if(!fin)
return;
int num_user = 0;
while(fin.peek() != EOF){
string line;
getline(fin, line);
num_user++;
}
fin.clear();
fin.seekg(0);
m_user = new User[40];
num_user = 0;
while(fin.peek() != EOF){
string line;
getline(fin, line);
stringstream s_line(line);
s_line >> m_user[num_user].name >> m_user[num_user].password
>> m_user[num_user].number >> m_user[num_user].borrowed
>> m_user[num_user].book_isbn >> m_user[num_user].history;
num_user++;
if(num_user == Library::max_user)
break;
}
m_userSize = num_user;
fin.close();
}
void Library::popMenu(string user_name){
const string menu1 = "==========书屋管理系统============\n1。添加新用户\n2。登录用户\n3。退出\n";
const string menu2 = "==========书屋管理系统============\n1。查看借书记录\n2。查看已借书本\n3。添加借书\n4。注销借书\n5。修改密码\n6。退出\n";
const string menu3 = "==========书屋管理系统============\n1。管理书屋库存\n2。退出\n";
cin.sync();
cin.clear();
if(user_name.empty())
cout << menu1;
else if (user_name == "Admin")
cout << menu3;
else
cout << menu2;
cout << "请选择:";
int option;
cin >> option;
proceedOPtions(option, user_name);
}
void Library::proceedOPtions(int option, string &user_name){
switch(option){
case 1:
{
if(user_name.empty())
userRegister();
else if (user_name == "Admin")
manageLibrary();
else
lookUpBorrowedHistory(user_name);
break;
}
case 2:
{
if(user_name.empty())
userLogin();
else if(user_name == "Admin")
saveAndExit();
else
lookUpBorrowedBook(user_name);
break;
}
case 3:
{
if(user_name.empty())
saveAndExit();
else if(user_name == "Admin")
cout << "选择错误!" << endl;
else
addBorrowingBook(user_name);
break;
}
case 4:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
deleteBorrowedBook(user_name);
break;
}
case 5:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
modifiedUserPassword(user_name);
break;
}
case 6:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
saveAndExit();
break;
}
default:
cout << "选择错误!" << endl;
}
if(m_loginUser)
user_name = m_loginUser->name;
popMenu(user_name);
}
void Library::userRegister(){
if (m_userSize >= max_user)
{
cout << "用户数已经达到最大!" << endl;
return;
}
string user_name, user_password;
while(true){
cout << "请输入用户名:";
cin >> user_name;
for (int i = 0; i < m_userSize; i++)
{
if(m_user[i].name == user_name){
cout << "用户名已存在!" << endl;
continue;
}
}
break;
}
cout << "请输入密码:";
cin >> user_password;
m_user[m_userSize].name = user_name;
m_user[m_userSize].password = user_password;
m_user[m_userSize].number = m_userSize;
m_user[m_userSize].borrowed = 0;
m_user[m_userSize].book_isbn = "null";
m_user[m_userSize].history = "null";
m_userSize++;
cout << "注册成功!" << endl;
}
void Library::userLogin(){
string user_name;
cout << "请输入用户名:";
cin >> user_name;
for(int i = 0; i < m_userSize; i++){
if(m_user[i].name == user_name){
string password;
while(true){
cout << "请输入密码:";
cin >> password;
if(password != m_user[i].password)
cout << "密码错误!" << endl;
else
break;
}
m_loginUser = &m_user[i];
cout << "登陆成功!" << endl;
return;
}
}
cout << "没有此用户!" << endl;
return;
}
void Library::lookUpBorrowedBook(const string &user_name){
User *user = findUser(user_name);
cout << user->book_isbn << endl;
}
void Library::lookUpBorrowedHistory(const string &user_name){
User *user = findUser(user_name);
cout << user->history << endl;
}
void Library::deleteBorrowedBook(const string &user_name){
User *user = findUser(user_name);
string delete_isbn;
while(true){
cout << "请输入要删除的书本编号:";
cin >> delete_isbn;
if (user->book_isbn.find(delete_isbn) == string::npos)
cout << "没有此书本编号!" << endl;
else{
user->book_isbn.replace(user->book_isbn.find(delete_isbn), delete_isbn.size(), "");
user->borrowed--;
cout << "删除成功!" << endl;
break;
}
}
}
void Library::addBorrowingBook(const string &user_name){
User *user = findUser(user_name);
if(((user->number>=1 && user->number<=9) && user->borrowed>=5) ||
((user->number>=10 && user->number<=39) && user->borrowed>=3)){
cout << "已到达最大借书量!" << endl;
return;
}
string book_isbn;
cout << "请输入要借的书本编号:";
cin >> book_isbn;
user->book_isbn == "null" ? user->book_isbn = book_isbn : user->book_isbn.append("," + book_isbn);
user->history == "null" ? user->history = book_isbn : user->history.append("," + book_isbn);
user->borrowed++;
}
void Library::modifiedUserPassword(const string &user_name){
User *user = findUser(user_name);
string passowrd;
while(true){
cout << "请输入旧密码:";
cin >> passowrd;
if (passowrd != user->password)
cout << "密码不正确!" << endl;
else
break;
}
string check_password;
do{
cout << "请输入新密码:";
cin >> passowrd;
cout << "请再次输入新密码:";
cin >> check_password;
}while(passowrd != check_password);
user->password = passowrd;
cout << "修改成功!" << endl;
}
User *Library::findUser(const string &user_name){
for (int i = 0; i < m_userSize; i++)
{
if(m_user[i].name == user_name)
return &m_user[i];
}
cerr << "寻找用户出现异常!程序终止!" << endl;
return NULL;
}
void Library::manageLibrary(){
}
void Library::saveAndExit(){
ofstream fout;
fout.open(data_file.c_str(), ios::out|ios::trunc);
for(int i = 0; i < m_userSize; i++){
fout << m_user[i].name << " " << m_user[i].password << " "
<< m_user[i].number << " " << m_user[i].borrowed << " "
<< m_user[i].book_isbn << " " << m_user[i].history << endl;
}
fout.close();
exit(0);
}
User::User(){
number = 0;
password = "user";
}
//main.cpp
#include "lib.h"
#include <iostream>
int main(){
string file_path = "lib.txt";
fstream fin;
fin.open(file_path.c_str());
if(!fin){
cout << "默认路径不正确,请手动输入载入数据路径:";
cin >> file_path;
}
Library *library = new Library(file_path);
library->popMenu();
return 0;
}
//lib.h
#ifndef LIB_H
#define LIB_H
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
struct User;
class Library{
public:
Library(string lib_file);
void proceedLoadingData(const string &file);
void popMenu(string user_name = string());
void proceedOPtions(int option, string &user_name);
User *findUser(const string &user_name);
void userRegister();
void userLogin();
void lookUpBorrowedBook(const string &user_name);
void lookUpBorrowedHistory(const string &user_name);
void addBorrowingBook(const string &user_name);
void deleteBorrowedBook(const string &user_name);
void modifiedUserPassword(const string &user_name);
void manageLibrary();
void saveAndExit();
static int max_user;
private:
string data_file;
User *m_user;
int m_userSize;
User *m_loginUser;
};
struct User{
User();
string name;
int number;
string password;
int borrowed;
string history;
string book_isbn;
};
#endif
//lib.cpp
#include "lib.h"
#include <functional>
int Library::max_user = 40;
Library::Library(string lib_file)
:data_file(lib_file), m_loginUser(NULL){
proceedLoadingData(lib_file);
if(m_userSize == 0){
m_user = new User[40];
m_user[0].name = "Admin";
m_user[0].password = "Admin";
m_user[0].number = 0;
m_user[0].borrowed = 0;
m_user[0].book_isbn = "null";
m_user[0].history = "null";
m_userSize++;
}
}
void Library::proceedLoadingData(const string &file){
ifstream fin;
fin.open(file.c_str());
if(!fin)
return;
int num_user = 0;
while(fin.peek() != EOF){
string line;
getline(fin, line);
num_user++;
}
fin.clear();
fin.seekg(0);
m_user = new User[40];
num_user = 0;
while(fin.peek() != EOF){
string line;
getline(fin, line);
stringstream s_line(line);
s_line >> m_user[num_user].name >> m_user[num_user].password
>> m_user[num_user].number >> m_user[num_user].borrowed
>> m_user[num_user].book_isbn >> m_user[num_user].history;
num_user++;
if(num_user == Library::max_user)
break;
}
m_userSize = num_user;
fin.close();
}
void Library::popMenu(string user_name){
const string menu1 = "==========书屋管理系统============\n1。添加新用户\n2。登录用户\n3。退出\n";
const string menu2 = "==========书屋管理系统============\n1。查看借书记录\n2。查看已借书本\n3。添加借书\n4。注销借书\n5。修改密码\n6。退出\n";
const string menu3 = "==========书屋管理系统============\n1。管理书屋库存\n2。退出\n";
cin.sync();
cin.clear();
if(user_name.empty())
cout << menu1;
else if (user_name == "Admin")
cout << menu3;
else
cout << menu2;
cout << "请选择:";
int option;
cin >> option;
proceedOPtions(option, user_name);
}
void Library::proceedOPtions(int option, string &user_name){
switch(option){
case 1:
{
if(user_name.empty())
userRegister();
else if (user_name == "Admin")
manageLibrary();
else
lookUpBorrowedHistory(user_name);
break;
}
case 2:
{
if(user_name.empty())
userLogin();
else if(user_name == "Admin")
saveAndExit();
else
lookUpBorrowedBook(user_name);
break;
}
case 3:
{
if(user_name.empty())
saveAndExit();
else if(user_name == "Admin")
cout << "选择错误!" << endl;
else
addBorrowingBook(user_name);
break;
}
case 4:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
deleteBorrowedBook(user_name);
break;
}
case 5:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
modifiedUserPassword(user_name);
break;
}
case 6:
{
if(user_name.empty() || user_name == "Admin")
cout << "选择错误!" << endl;
else
saveAndExit();
break;
}
default:
cout << "选择错误!" << endl;
}
if(m_loginUser)
user_name = m_loginUser->name;
popMenu(user_name);
}
void Library::userRegister(){
if (m_userSize >= max_user)
{
cout << "用户数已经达到最大!" << endl;
return;
}
string user_name, user_password;
while(true){
cout << "请输入用户名:";
cin >> user_name;
for (int i = 0; i < m_userSize; i++)
{
if(m_user[i].name == user_name){
cout << "用户名已存在!" << endl;
continue;
}
}
break;
}
cout << "请输入密码:";
cin >> user_password;
m_user[m_userSize].name = user_name;
m_user[m_userSize].password = user_password;
m_user[m_userSize].number = m_userSize;
m_user[m_userSize].borrowed = 0;
m_user[m_userSize].book_isbn = "null";
m_user[m_userSize].history = "null";
m_userSize++;
cout << "注册成功!" << endl;
}
void Library::userLogin(){
string user_name;
cout << "请输入用户名:";
cin >> user_name;
for(int i = 0; i < m_userSize; i++){
if(m_user[i].name == user_name){
string password;
while(true){
cout << "请输入密码:";
cin >> password;
if(password != m_user[i].password)
cout << "密码错误!" << endl;
else
break;
}
m_loginUser = &m_user[i];
cout << "登陆成功!" << endl;
return;
}
}
cout << "没有此用户!" << endl;
return;
}
void Library::lookUpBorrowedBook(const string &user_name){
User *user = findUser(user_name);
cout << user->book_isbn << endl;
}
void Library::lookUpBorrowedHistory(const string &user_name){
User *user = findUser(user_name);
cout << user->history << endl;
}
void Library::deleteBorrowedBook(const string &user_name){
User *user = findUser(user_name);
string delete_isbn;
while(true){
cout << "请输入要删除的书本编号:";
cin >> delete_isbn;
if (user->book_isbn.find(delete_isbn) == string::npos)
cout << "没有此书本编号!" << endl;
else{
user->book_isbn.replace(user->book_isbn.find(delete_isbn), delete_isbn.size(), "");
user->borrowed--;
cout << "删除成功!" << endl;
break;
}
}
}
void Library::addBorrowingBook(const string &user_name){
User *user = findUser(user_name);
if(((user->number>=1 && user->number<=9) && user->borrowed>=5) ||
((user->number>=10 && user->number<=39) && user->borrowed>=3)){
cout << "已到达最大借书量!" << endl;
return;
}
string book_isbn;
cout << "请输入要借的书本编号:";
cin >> book_isbn;
user->book_isbn == "null" ? user->book_isbn = book_isbn : user->book_isbn.append("," + book_isbn);
user->history == "null" ? user->history = book_isbn : user->history.append("," + book_isbn);
user->borrowed++;
}
void Library::modifiedUserPassword(const string &user_name){
User *user = findUser(user_name);
string passowrd;
while(true){
cout << "请输入旧密码:";
cin >> passowrd;
if (passowrd != user->password)
cout << "密码不正确!" << endl;
else
break;
}
string check_password;
do{
cout << "请输入新密码:";
cin >> passowrd;
cout << "请再次输入新密码:";
cin >> check_password;
}while(passowrd != check_password);
user->password = passowrd;
cout << "修改成功!" << endl;
}
User *Library::findUser(const string &user_name){
for (int i = 0; i < m_userSize; i++)
{
if(m_user[i].name == user_name)
return &m_user[i];
}
cerr << "寻找用户出现异常!程序终止!" << endl;
return NULL;
}
void Library::manageLibrary(){
}
void Library::saveAndExit(){
ofstream fout;
fout.open(data_file.c_str(), ios::out|ios::trunc);
for(int i = 0; i < m_userSize; i++){
fout << m_user[i].name << " " << m_user[i].password << " "
<< m_user[i].number << " " << m_user[i].borrowed << " "
<< m_user[i].book_isbn << " " << m_user[i].history << endl;
}
fout.close();
exit(0);
}
User::User(){
number = 0;
password = "user";
}
//main.cpp
#include "lib.h"
#include <iostream>
int main(){
string file_path = "lib.txt";
fstream fin;
fin.open(file_path.c_str());
if(!fin){
cout << "默认路径不正确,请手动输入载入数据路径:";
cin >> file_path;
}
Library *library = new Library(file_path);
library->popMenu();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询