本文小编为大家详细介绍“C++实现图书管理系统简易版的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++实现图书管理系统简易版的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
包括管理员端和学生端,可以对图书进行借阅,归还,还可以修改账号登陆密码等
#include<iostream> #include<string> #include<string.h> #include<cstdio> #include<conio.h> #include<fstream> #include<cstring> #include<iomanip> #include<algorithm> #include<regex> #include<windows.h> #include<sstream>//stringstream要使用到的头文件 #define STUDENT_FILE "student_text.txt" #define BOOK_FILE "book_text.txt" #define CLASS_FILE "class.txt" #define MAX 1000 #define MIN 100 using namespace std; class Student; class Book; class Clazz; void initialize();//初始化 void Manager_Menu(); void Student_Menu(); void existSystem(); //退出图书管理系统 void show_book_base(); string input_password();//不可见输入密码 int student_id_exist(int id);//查找用户是否存在 int manager_id_exist(int id); int book_id_exist(int id);//查找书籍是否存在 int class_id_exist(int id); int random_id(); //产生随机图书编号 void book_find(); void load_information(); void update_information(); void regist_windows(); void regist_manager(); void regist_student(); bool student_cmp(Student a, Student b); bool class_cmp(Clazz a, Clazz b); bool book_cmp(Book a, Book b); class Book { int book_id; //书籍编号 string book_name; //书籍名称 int book_total_num; //该类书籍总数 string book_author; //书籍作者 string book_type; //书籍分类 string book_present; //书籍简介 int book_student_len; //每种书共有几人选 int student_id[MIN]; //借阅该书籍的学生学号 string student_name[MIN]; //借阅该书籍的学生姓名 public: Book() { book_id = -1; book_total_num = 0; book_student_len = 0; book_type = ""; book_present = ""; memset(student_id, 0, MIN); for (int i = 0; i < MIN; i++) { student_name[i] = ""; } } void set_book_name(string book_name) { this->book_name = book_name; } void set_book_author(string book_author) { this->book_author = book_author; } void set_book_type(string book_type) { this->book_type = book_type; } void set_book_total_num(int book_total_num) { this->book_total_num = book_total_num; } void set_book_id(int book_id) { this->book_id = book_id; } int get_book_id() { return book_id; } void set_book_information(int book_id, string book_name, int book_total_num, string book_author, string book_type) { set_book_id(book_id); set_book_name(book_name); set_book_author(book_author); set_book_type(book_type); set_book_total_num(book_total_num); //set_book_present(book_present); } void set_book_present(string book_present) { this->book_present = book_present; } string& get_book_present() { return book_present; } void show_book_information(); friend int book_id_exist(int id); friend void show_book_base(); friend class Student; friend class Manager; friend void initialize(); friend void load_information(); friend void update_information(); }; Book book[MAX]; int book_len = 0; class User { protected: int id; string name; string password; int mark; public: User() { id = -1; mark = 0; name = ""; password = ""; } void password_update(); virtual void book_insert()=0; virtual void book_delete()=0; friend void initialize(); void set_id(int id) { this->id = id; } void set_name(string name) { this->name = name; } void set_mark(int mark) { this->mark = mark; } void set_password(string password) { this->password = password; } int get_id() { return id; } int get_mark() { return this->mark; } string get_name() { return name; } string get_password() { return password; } }; class Student:public User { int student_grade; //入学年份--年级 int class_id; string student_gender; int student_book_len;//借阅书籍总数 public: Student() { id = -1; //id为-1时表示该账户无效 class_id = -1; name = ""; student_gender = ""; student_grade = -1; student_book_len = 0; } Student operator = (const Student& p); void set_class_id(int id) { class_id = id; } void set_student_grade(int student_grade) { this->student_grade = student_grade; } void set_student_gender(string student_gender) { this->student_gender = student_gender; } int get_class_id() { return class_id; } int get_student_grade() { return student_grade; } string get_student_gender() { return student_gender; } //学生 void book_insert(); void student_information_view(); void book_delete(); void student_information_update(); //学生修改账号密码 void show_self(); void student_windows(); friend void eroll_windows(); friend class Manager; friend void load_information(); friend void update_information(); friend bool student_cmp(Student a,Student b); }; Student student[MAX]; int student_len = 0; class Manager :public User { public: Manager() { id = -1; mark = 0; name = ""; password = ""; } Manager(int id,int mark,string name,string password) { this->id =id; this->mark = mark; this->name = name; this->password = password; } Manager operator = (const Manager& p); //管理员对学生账号的管理 void student_insert(); void student_delete(); void student_view(); //管理员对书籍的管理 void book_insert(); void book_delete(); void book_view(); void book_update(); //班级 void class_insert(); void class_update(); void class_view(); void class_delete(); void manager_windows(); void manager_insert(); friend void load_information(); friend void update_information(); }; Manager manager[MIN]; int manager_len = 0; class Clazz { int class_id; string class_name; string teacher_name; int student_len; public: Clazz() { class_id = -1; class_name = ""; teacher_name = ""; student_len = 0; } int get_class_id() { return class_id; } string get_class_name() { return class_name; } string get_teacher_name() { return teacher_name; } void set_class_id(int id) { this->class_id =id; } void set_class_name(string name) { this->class_name = name; } void set_teacher_name(string name) { this->teacher_name = name; } friend int class_id_exist(int id); friend void load_information(); friend void update_information(); }; Clazz clazz[MIN]; int class_len = 0; int main() { initialize(); //登录 regist_windows(); system("pause"); return 0; } void initialize() { system("title 图书信息管理系统"); system("color f0"); system("mode con cols=90 lines=38"); srand((int)time(0)); load_information(); Manager p1; p1.set_id(111111); p1.set_name("张晖"); p1.set_password("111111"); manager[0]=p1; manager_len++; Manager p2(222222,1, "李四", "222222"); manager[1]=p2; manager_len++; } void Manager_Menu() { cout << " ________________________________________________________________________________________" << endl; cout << " | 图书管理管理系统 |" << endl; cout << " |______________________________________________________________________________________|" << endl; cout << " | 1.增加学生信息 | 13.修改登录密码 |" << endl; cout << " | 2.查看学生信息 | 14.退出当前账号 |" << endl; cout << " | 3.删除学生信息 | 0.退出管理系统 |" << endl; cout << " |__________________________________________|___________________________________________|" << endl; cout << " | 4.增加书籍信息 | 8.增加班级信息 |" << endl; cout << " | 5.查看书籍信息 | 9.查看班级信息 |" << endl; cout << " | 6.修改书籍信息 | 10.修改班级信息 |" << endl; cout << " | 7.删除书籍信息 | 11.删除班级信息 |" << endl; cout << " | | 12.增加管理员 |" << endl; cout << " |__________________________________________|___________________________________________|" << endl; cout << endl; } void Student_Menu() { cout << " ______________________________________________" << endl; cout << " | 学生图书系统 |" << endl; cout << " |____________________________________________|" << endl; cout << " | 1.选择借阅书籍 |" << endl; cout << " | 2.查找书籍信息 |" << endl; cout << " | 3.归还借阅书籍 |" << endl; cout << " | 4.查询个人信息 |" << endl; cout << " |____________________________________________|" << endl; cout << " | 5.修改个人信息 |" << endl; cout << " | 6.修改登录密码 |" << endl; cout << " | 7.退出当前账号 |" << endl; cout << " | 0.退出管理系统 |" << endl; cout << " |____________________________________________|" << endl; cout << endl; } void Manager::manager_windows() { while (true) { int choice = 0; Manager_Menu(); cout << "\t\t请输入您的选择:" << endl; cout << " "; cin >> choice; switch (choice) { case 0://退出 { existSystem(); break; } case 1://添加 { student_insert(); break; } case 2://显示 { student_view(); break; } case 3://删除 { student_delete(); break; } case 4: { book_insert(); break; } case 5: { book_view();//book_find(); break; } case 6://查找 { book_update(); break; } case 7: { book_delete(); break; } case 8: { class_insert(); break; } case 9: { class_view(); break; } case 10://查找 { class_update(); break; } case 11: { class_delete(); break; } case 12: { manager_insert(); break; } case 13: { password_update(); break; } case 14://回到登录界面 { system("cls"); update_information(); regist_windows(); break; } default: { cout << "没有该选项!" << endl; system("cls"); break; } } } } void Student::student_windows() { while (true) { int choice = 0; Student_Menu(); cout << "\t\t请输入您的选择:" << endl; cout << " "; cin >> choice; switch (choice) { case 0://退出 { existSystem(); break; } case 1://添加 { book_insert(); break; } case 2: { book_find(); break; } case 3: { book_delete(); break; } case 4: { student_information_view(); break; } case 5: { student_information_update(); break; } case 6: { password_update(); break; } case 7://回到登录界面 { system("cls"); update_information(); regist_windows(); break; } default: { cout << "没有该选项" << endl; system("cls"); break; } } } } void regist_windows() { int select = 0; cout << "\t\t1.管理员登录" << endl; cout << "\t\t2.学生登录" << endl; cin >> select; system("cls"); if (select == 1) { regist_manager(); } else if (select == 2) { regist_student(); } else { cout << "该选项不存在!请选择1或2!" << endl; } } void regist_manager() { cout << "\t用户名:"; int temp_user_id = -1; cin >> temp_user_id; cout << endl; cout << "\t密码:"; string temp_password; temp_password = input_password(); int flag = 0; int position = manager_id_exist(temp_user_id); if (position == -1) { cout << "该用户不存在!" << endl; regist_manager(); } else if (manager[position].get_password() == temp_password) { system("cls"); while (true) { manager[position].manager_windows(); } } else { cout << "\t\t用户名或密码错误" << endl << endl; regist_manager(); } system("pause"); system("cls"); } void regist_student() { int temp_user_id = 0; string temp_password; cout << "\t用户名:"; cin >> temp_user_id; cout << endl; cout << "\t密码:"; temp_password = input_password(); int flag = 0; int position = student_id_exist(temp_user_id); if (position == -1) { cout << "\t该用户不存在!" << endl; regist_student(); } else if (student[position].get_password() == temp_password) { system("cls"); while (true) { student[position].student_windows(); } } else { cout << "\t\t用户名或密码错误" << endl << endl; regist_student(); } system("pause"); system("cls"); } void Book::show_book_information() { cout << "-------------------------------------------------------------------------------------\n"; cout << " 书名\t\t\t类型\t\t\t\t作者\n"; cout << book_name << "\t\t\t" << book_type << "\t\t\t" << book_author << endl; cout << "-------------------------------------------------------------------------------------\n"; cout << " 总数\t\t\t剩余量\n"; cout << book_total_num << "\t\t\t" << (book_total_num - book_student_len) << endl; cout << "-------------------------------------------------------------------------------------\n"; cout << "\t\t\t小说简介\n"; cout << book_present << endl; cout << "-------------------------------------------------------------------------------------\n"; } void User::password_update() { int flag = 0; while (true) { cout << "请输入旧密码:"; string last_password, new_password1, new_password2; last_password = input_password(); if (last_password == password) { cout << "请输入新密码(大于等于6位):"; new_password1 = input_password(); cout << "请再次输入新密码:"; new_password2 = input_password(); if (new_password1 == new_password2) { if ((new_password1.size() >= 6) && (new_password1.size() <= 13)) { this->password = new_password1; cout << "密码修改成功!" << endl; break; } else { cout << "密码应大于等于6位!修改失败!" << endl; } } else { cout << "两次密码不一致!请重新输入!" << endl; } } else { flag++; if (flag <= 3) { cout << "旧密码错误!" << endl; } else { cout << "密码多次输入错误!返回菜单界面!"; break; } } } system("pause"); system("cls"); } Manager Manager::operator = (const Manager& p) { this->id = p.id; this->mark = p.mark; this->name = p.name; this->password = p.password; return *this; } void Manager::student_insert() { if (student_len > MAX) { cout << "信息系统已满,无法添加!" << endl; return; } else { Student temp_student; cout << "请输入学号:" << endl; int temp_id; cin >> temp_id; cout << "请输入姓名:" << endl; string temp_name; cin >> temp_name; cout << "请输入权限:" << endl; cout << "1.普通用户权限" << endl; cout << "2.高级用户权限" << endl; int temp_mark; cin >> temp_mark; temp_student.set_mark(temp_mark); string temp_student_gender; while (true) { cout << "请选择性别:" << endl; cout << "1.女\n2.男\n"; int choice = 0; cin >> choice; if (choice == 1) { temp_student_gender = "女"; break; } else if (choice == 2) { temp_student_gender = "男"; break; } else { cout << "没有该选项!请重新选择!\n"; } } int temp_student_grade; while (true) { cout << "请输入年级(入学年份):" << endl; cout << "1.2018\n2.2019\n3.2020\n4.2021\n"; int choice = 0; cin >> choice; if (choice == 1) { temp_student_grade = 2018; break; } else if (choice == 2) { temp_student_grade = 2019; break; } else if (choice == 3) { temp_student_grade = 2020; break; } else if (choice == 2) { temp_student_grade = 2021; break; } else { cout << "没有该选项!请重新选择!\n"; } } cout << "\t\t班级信息如下\n"; for (int i = 0; i < class_len; i++) cout << clazz[i].get_class_id() << "\t\t" << clazz[i].get_class_name() << endl; while (1) { cout << "请输入班级编号\n"; int temp_id = 0; cin >> temp_id; if (class_id_exist(temp_id) != -1) { temp_student.set_class_id(temp_id); break; } else { cout << "该班级编号不存在!\n"; } } string temp_password; stringstream transform; transform << temp_id; transform >> temp_password; temp_student.set_password(temp_password); temp_student.set_id(temp_id); temp_student.set_student_grade(temp_student_grade); temp_student.set_name(temp_name); temp_student.set_student_gender(temp_student_gender); student[student_len] = temp_student; /* while (1) { cout << "请设置您的密码:" << endl; temp_password1 = input_password(); temp_password2 = input_password(); if (temp_password1 == temp_password2) { student[student_len].password = temp_password1; student_len++; break; } else { cout << "两次密码不一致!请重新设置密码!" << endl; } }*/ student_len++; cout << "学生信息添加成功!" << endl; sort(student, student + student_len, student_cmp); } system("pause"); system("cls"); } void Manager::student_view() { cout << "\t\t1.查询单个账号信息" << endl; cout << "\t\t2.查看所有账号信息" << endl; int choice; cin >> choice; if (choice == 1) { cout << "\t\t请输入查询账号:" << endl; int id; cin >> id; int pos = student_id_exist(id); if (pos == -1) { cout << "\t\t该账号不存在!" << endl; } else { student[pos].show_self(); } } else { if (student_len == 0) { cout << "当前记录为空" << endl; } else { cout << "序号:\t姓名:\t学号: \t年级: \t性别\n"; for (int i = 0; i < student_len; i++) { cout << i + 1 << "\t\t"; cout << student[i].name << "\t\t" << student[i].id << "\t\t"; if (student[i].student_book_len == 0) { cout << "未借阅书籍"; } else { for (int j = 0; j < book_len; j++) { for (int k = 0; k < book[j].book_total_num; k++) { if (book[j].student_name[k] == student[i].name) cout << book[j].book_id << "\t\t" << book[j].book_name << endl;; } } } cout << endl; } } } system("pause"); system("cls"); } void Manager::student_delete() { cout << "请输入您要删除的学生账号:" << endl; int temp_id; cin >> temp_id; int position = student_id_exist(temp_id); if (position != -1) { for (int i = position; i < student_len - 1; i++) { student[i] = student[i + 1]; } student_len--; cout << "删除成功" << endl; } else { cout << "该学生账号不存在!" << endl; } system("pause"); system("cls"); } void Manager::book_insert() { int temp_book_id; string temp_book_name; show_book_base(); //cout << "请输入想增加的书籍编号:" << endl; cout << "书籍编号由系统自动生成!" << endl; temp_book_id = random_id(); cout << "请输入想增加的书籍名称:" << endl; cin >> temp_book_name; cout << "请输入书籍数量:" << endl; int temp_book_len; cin >> temp_book_len; cout << "请输入书籍作者:" << endl; string temp_book_author; cin >> temp_book_author; cout << "请输入书籍分类:" << endl; string temp_book_type; cin >> temp_book_type; cout << "请输入书籍简介:" << endl; string temp_book_present; cin >> temp_book_present; book[book_len].set_book_information(temp_book_id, temp_book_name, temp_book_len, temp_book_author, temp_book_type); book[book_len].set_book_present(temp_book_present); book_len++; cout << "书籍信息添加成功!" << endl; sort(book, book + book_len, book_cmp); system("pause"); system("cls"); } void Manager::book_delete() { int temp_book_id; show_book_base(); cout << "请输入想删除的书籍id号:" << endl; cin >> temp_book_id; if (book_id_exist(temp_book_id) == -1) { cout << "该书籍不存在!" << endl; } else { for (int i = 0; i < book_len - 1; i++) { book[i] = book[i + 1]; } book_len--; cout << "书籍信息删除成功!" << endl; } system("pause"); system("cls"); } void Manager::book_update() { show_book_base(); cout << "请输入想修改的书籍编号:" << endl; int temp_book_id; cin >> temp_book_id; cout << "请选择你要修改的书籍信息" << endl; cout << "1--修改书籍名称" << endl; cout << "2--修改书籍总数" << endl; cout << "3--修改书籍作者" << endl; cout << "4--修改书籍分类" << endl; cout << "5--修改书籍简介" << endl; cout << "0--返回菜单" << endl; int choice = 0; cin >> choice; switch (choice) { case 0: break; case 1: { cout << "请输入书籍名称:" << endl; string temp_book_name; cin >> temp_book_name; book[book_len].set_book_name(temp_book_name); cout << "修改成功\n"; break; } case 2: { cout << "请输入书籍总数:" << endl; int temp_book_len; cin >> temp_book_len; book[book_len].set_book_total_num(temp_book_len); break; } case 3: { cout << "请输入书籍作者:" << endl; string temp_book_author; cin >> temp_book_author; book[book_len].set_book_author(temp_book_author); break; } case 4: { cout << "请输入书籍分类:" << endl; int temp_book_type; cin >> temp_book_type; book[book_len].book_type = temp_book_type; break; }case 5: { cout << "请输入书籍简介:" << endl; string temp_book_present; cin >> temp_book_present; book[book_len].book_present = temp_book_present; break; } default: { cout << "没有该选项!" << endl; break; } } system("pause"); system("cls"); } void Manager::book_view() { if (book_len == 0) { cout << "无借阅信息!" << endl; } for (int i = 0; i < book_len; i++) { cout << "书籍id号:" << book[i].book_id << "\t书籍名:" << book[i].book_name << "\t作者:" << book[i].book_author << "\t书籍剩余数:" << (book[i].book_total_num - book[i].book_student_len) << "\t书籍总数:" << book[i].book_total_num << endl; if (book[i].book_student_len == 0) { cout << "\t\t空!" << endl; } else { cout << "学号\t\t" << "姓名" << endl; for (int j = 0; j < book[i].book_student_len; j++) { cout << book[j].book_id << "\t" << book[j].book_name << endl; } } cout << endl; } cout << "\t如果您需要查询书籍详细信息,请输入1;不需要则输入0\n"; int ch = 0; cin >> ch; if (ch==1) book_find(); system("pause"); system("cls"); } void Manager::class_insert() { int temp_id; string temp_name; cout << "请输入想增加的班级编号:" << endl; cin >> temp_id; clazz[class_len].set_class_id(temp_id); cout << "请输入想增加的班级名称:" << endl; cin >> temp_name; clazz[class_len].set_class_name(temp_name); cout << "请输入想增加的班主任姓名:" << endl; string temp_teacher_name; cin >> temp_teacher_name; clazz[class_len].set_teacher_name(temp_teacher_name); class_len++; cout << "班级信息添加成功!" << endl; sort(clazz, clazz + class_len, class_cmp); system("pause"); system("cls"); } void Manager::class_delete() { int temp_book_id; cout << "班级编号\t\t\t班级名称\n"; for (int i = 0; i < book_len; i++) cout << clazz[i].get_class_id()<< "\t\t" << clazz[i].get_class_name () << endl; cout << "请输入想删除的班级编号:" << endl; cin >> temp_book_id; if (class_id_exist(temp_book_id) == -1) { cout << "该班级不存在!" << endl; } else { for (int i = 0; i < class_len - 1; i++) { clazz[i] = clazz[i + 1]; } class_len--; cout << "班级信息删除成功!" << endl; } system("pause"); system("cls"); } void Manager::class_update() { class_view(); cout << "请输入想修改的班级编号:" << endl; int temp_book_id; cin >> temp_book_id; cout << "请选择你要修改的班级信息" << endl; cout << "1--修改班级名称" << endl; cout << "2--修改班主任姓名" << endl; cout << "0--返回菜单" << endl; int choice = 0; cin >> choice; switch (choice) { case 0: break; case 1: { cout << "请输入班级名称:" << endl; string temp_name; cin >> temp_name; clazz[class_len].set_class_name(temp_name); cout << "修改成功\n"; break; } case 2: { cout << "请输入班主任名称:" << endl; string temp_teacher_name; cin >> temp_teacher_name; clazz[class_len].set_teacher_name(temp_teacher_name); cout << "修改成功\n"; break; } default: { cout << "没有该选项!" << endl; break; } } system("pause"); system("cls"); } void Manager::class_view() { if (class_len == 0) { cout << "无班级信息!" << endl; } else { cout << "\t\t班级信息如下" << endl; cout << "班级编号\t\t班级名称\t\t班主任姓名" << endl; for (int i = 0; i < class_len; i++) cout << clazz[i].get_class_id() << setw(32) << clazz[i].get_class_name() << setw(26) << clazz[i].get_teacher_name() << endl; } system("pause"); system("cls"); } void Manager::manager_insert() { if (this->mark == 0) { cout << "\t\t权限不足!" << endl; } else { cout << "请输入账号:" << endl; int temp_id; cin >> temp_id; cout << "请输入姓名:" << endl; string temp_name; cin >> temp_name; cout << "请输入权限:" << endl; cout << "1.普通管理员权限" << endl; cout << "2.高级管理员权限" << endl; int temp_mark; cin >> temp_mark; cout << "密码默认为账号!" << endl; string temp_password; stringstream transform; transform << temp_id; transform >> temp_password; Manager temp(temp_id, temp_mark, temp_name, temp_password); manager[manager_len] = temp; manager_len++; } system("pause"); system("cls"); } Student Student::operator = (const Student& p) { this->id = p.id; this->name = p.name; this->student_gender = p.student_gender; this->student_grade = p.student_grade; this->class_id = p.class_id; this->student_book_len = p.student_book_len; this->password = p.password; return *this; } void Student::book_insert() { cout << "可选书籍如下:" << endl; show_book_base(); int temp_book_id; cout << "请输入你要借阅的书籍编号:" << endl; cin >> temp_book_id; int position = book_id_exist(temp_book_id); int count = book[position].book_student_len; if (position == -1) { cout << "该书籍不存在!" << endl; } else { book[position].student_id[count] = this->id; book[position].student_name[count] = this->name; book[position].book_student_len++; student_book_len++; cout << "借阅成功!" << endl; } system("pause"); system("cls"); } void Student::student_information_view() { cout << "\t\t请输入查询账号:" << endl; int id; cin >> id; int pos =student_id_exist(id); if (pos != student_id_exist(id)) { cout << "\t\t您无查看他人账号权限!" << endl; } else { show_self(); } system("pause"); system("cls"); } void Student::student_information_update() { cout << "请选择你要更改的信息:\n"; cout << "1.修改性别\n"; cout << "2.修改年级\n"; cout << "3.修改班级\n"; int choice; cin >> choice; if (choice == 1) { cout << "1.男\n2.女\n"; int select; cin >> select; if (select == 1) { this->student_gender = "男"; cout << "修改成功!\n"; } else if (select == 1) { this->student_gender = "女"; cout << "修改成功!\n"; } else cout << "没有该选项!\n"; } else if (choice == 2) { cout << "1.2018\n2.2019\n3.2020\n4.2021\n"; int select; cin >> select; if (select == 1) { this->student_grade = 2018; cout << "修改成功!\n"; } else if (select == 2) { this->student_grade = 2019; cout << "修改成功!\n"; } else if (select == 3) { this->student_grade = 2020; cout << "修改成功!\n"; } else if (select == 4) { this->student_grade = 2021; cout << "修改成功!\n"; } else cout << "没有该选项!\n"; } else if(choice == 3) { cout << "\t\t班级信息如下" << endl; cout << "班级编号\t\t班级名称\t\t班主任姓名" << endl; for (int i = 0; i < class_len; i++) cout << clazz[i].get_class_id() << "\t\t" << clazz[i].get_class_name() << "\t\t" << clazz[i].get_teacher_name() << endl; while (1) { cout << "\t\t请输入你选择的班级编号\n" << endl; int select; cin >> select; if (class_id_exist(select) != -1) { int temp_id; cin >> temp_id; this->class_id = temp_id; break; } else { cout << "\t\t该班级编号不存在!\n"; } } } else { cout << "该选项不存在!\n"; } system("pause"); system("cls"); } void Student::book_delete() { cout << "您的已选书籍如下:" << endl; if (student_book_len == 0) { cout << "空!" << endl; } else { cout << "序号\t\t书籍名称\t\t书籍编号" << endl;; for (int i = 0; i < book_len; i++) { for (int j = 0; j < book[i].book_student_len; j++) { if (book[i].student_id[j] == this->id) { cout << i + 1 << "\t\t" << book[i].book_name << "\t\t\t" << book[i].book_id << endl; } } } } cout << endl << "请输入您想退还的书籍编号:" << endl; int temp_book_id; cin >> temp_book_id; int book_position = book_id_exist(temp_book_id); if (book_position == -1) { cout << "该书籍不存在!" << endl; } else { int student_position = -1; for (int i = 0; i < book[book_position].book_student_len; i++) { if (book[book_position].student_id[i] == id) { student_position = i; break; } } if (student_position != -1) { for (int i = student_position; i < book[book_position].book_student_len - 1; i++) { book[book_position].student_id[i] = book[book_position].student_id[i + 1]; book[book_position].student_name[i] = book[book_position].student_name[i + 1]; } book[book_position].book_student_len--; student_book_len--; cout << "书籍归还成功!" << endl; } else { cout << "你并未借阅过该书籍!" << endl; } } system("pause"); system("cls"); } void Student::show_self() { cout << "\t\t账号信息如下:" << endl; cout << "______________________________________________________________________________\n"; cout << " 姓名:" << this->name << "\t\t 学号:" << this->id << "\n"; cout << "______________________________________________________________________________\n"; cout << " 性别:" << this->student_gender << "\t\t 班级编号:" << this->class_id << "\n"; cout << "______________________________________________________________________________\n"; cout << " 书籍编号" << "\t\t书籍名称" << "\n"; cout << "______________________________________________________________________________\n"; if (this->student_book_len == 0) { cout << "\t\t暂未借阅书籍" << "\n"; } else { for (int i = 0; i < book_len; i++) { for (int j = 0; j < book[i].book_student_len; j++) { if (book[i].student_id[j] == this->id) { cout << "\t\t" << book[i].book_id << "\t\t" << book[i].book_name << "\n"; } } } } cout << "______________________________________________________________________________\n"; } void book_find() { cout << "请输入你要查找的书籍编号:" << endl; int temp_book_id; cin >> temp_book_id; int position = book_id_exist(temp_book_id); if (position == -1) { cout << "该书籍编号不存在!查找失败!" << endl; } else { book[position].show_book_information(); } system("pause"); system("cls"); } void show_book_base() { cout << "书籍id号:\t" << "书籍名称:" << endl; for (int i = 0; i < book_len; i++) { cout << book[i].book_id << "\t\t" << book[i].book_name << endl; } } int student_id_exist(int id)//二分查找 { int left = 0; int right = student_len; int mid = 0; while (left < right) { mid = (left + right) / 2; if (id == student[mid].get_id()) return mid; if (id > student[mid].get_id()) left = mid + 1; else if (id < student[mid].get_id()) right = mid; } return -1; } int manager_id_exist(int id) { for (int i = 0; i < manager_len; i++) { if (manager[i].get_id() == id) { return i; } } return -1; } int book_id_exist(int id) { int left = 0; int right = book_len; int mid = 0; while (left < right) { mid = (left + right) / 2; if (id == book[mid].get_book_id()) return mid; if (id > book[mid].get_book_id()) left = mid + 1; else if (id < book[mid].get_book_id()) right = mid; } return -1; } int class_id_exist(int id) { int left = 0; int right = class_len; int mid = 0; while (left < right) { mid = (left + right) / 2; if (id == clazz[mid].get_class_id()) return mid; if (id > clazz[mid].get_class_id()) left = mid + 1; else if (id < clazz[mid].get_class_id()) right = mid; } return -1; } bool student_cmp(Student a, Student b) { return a.id < b.id; } bool class_cmp(Clazz a, Clazz b) { return a.get_class_id() < b.get_class_id(); } bool book_cmp(Book a, Book b) { return a.get_book_id() < b.get_book_id(); } void existSystem() { cout << "欢迎下次使用" << endl; system("pause"); exit(0); } int random_id() //产生随机书籍编号 { int id; for (int i = 0; i < 8; i++) { id = rand(); } return id; } string input_password() { char temp_password[20]; int len = 0; char key; while ((key = _getch()) != '\r')//不可见输入 { if (len < 12) { temp_password[len++] = key; putchar('*'); } else { cout << endl << endl;; cout << "\t 密码过长" << endl; } } temp_password[len] = '\0';//字符串结束标记:\0; cout << endl; return temp_password; } void load_information() { ifstream fin; fin.open(BOOK_FILE, ios::in); if (fin.is_open() == false) { cout << "文件打开失败!" << endl; } fin >> book_len; for (int i = 0; i < book_len; i++) { fin >> book[i].book_id >> book[i].book_name >> book[i].book_total_num >> book[i].book_student_len >> book[i].book_author >> book[i].book_type >> book[i].book_present; } fin.close(); fin.open(CLASS_FILE, ios::in); if (fin.is_open() == false) { cout << "文件打开失败!" << endl; } fin >> class_len; for (int i = 0; i < class_len; i++) { fin >> clazz[i].class_id >> clazz[i].class_name >> clazz[i].teacher_name; } fin.close(); fin.open(STUDENT_FILE, ios::in); if (fin.is_open() == false) { cout << "文件打开失败!" << endl; } fin >> student_len; for (int i = 0; i < student_len; i++) { fin >> student[i].id >> student[i].name >> student[i].mark>>student[i].student_grade>> student[i].class_id >> student[i].student_gender >> student[i].password >> student[i].student_book_len; } fin.close(); sort(book, book + book_len, book_cmp); sort(clazz, clazz + class_len, class_cmp); sort(student, student + student_len, student_cmp); } void update_information() { ofstream fou; fou.open(BOOK_FILE, ios::out); if (fou.is_open() == false) { cout << "文件打开失败!" << endl; } fou << book_len << endl; for (int i = 0; i < book_len; i++) { fou << book[i].book_id << " " << book[i].book_name << " " << book[i].book_total_num << " " << book[i].book_student_len << " " << book[i].book_author << " " << book[i].book_type << " " << book[i].book_present << endl; } fou.close(); fou.open(CLASS_FILE, ios::out); if (fou.is_open() == false) { cout << "文件打开失败!" << endl; } fou << class_len << endl; for (int i = 0; i < class_len; i++) { fou << clazz[i].class_id <<" "<< clazz[i].class_name <<" "<< clazz[i].teacher_name << endl; } fou.close(); fou.open(STUDENT_FILE, ios::out); if (fou.is_open() == false) { cout << "文件打开失败!" << endl; } fou << student_len << endl; for (int i = 0; i < student_len; i++) { fou << student[i].id << " " << student[i].name << " " << student[i].mark<<" "<<student[i].student_grade<<" "<<student[i].class_id << " " << student[i].student_gender << " " << student[i].password << " " << student[i].student_book_len << endl; } fou.close(); }
读到这里,这篇“C++实现图书管理系统简易版的方法”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。