C++实现图书管理系统课程设计
本文实例为大家分享了C++实现图书管理系统的具体代码,供大家参考,具体内容如下
大一 C++课设,没有用分文件的形式,只是把菜单页面单独分开了。用的是链表,都是一些基础的东西。另外采用了二维数组来保存读者借书信息,并将二维数组读入文件中。
功能:
1.首先是注册,登录,找回密码和修改密码功能,登录和注册使用了多态,并且登录页面采用了*符号加密。
2.管理员在删除图书时,若用户此时已将此书借走,则会将此书从用户的借书书单中删去,实现了动态管理。
3.容错率极高,每个页面都有返回上一页面的功能。
4.每个页面都有设置有滚键盘机制,提高了容错率。
菜单:
#pragma once void _mainmenu(); void Loginmenu(); void Registmenu(); void readermenu(); void mangermenu(); void changebookmenu(); void browsemenu();
菜单文件
#include "menu.h" #include <cstdio> void _mainmenu() { printf("\n\n"); printf(" 欢 迎 使 用 图 书 管 理 系 统 !\n"); printf(" **************************************************\n"); printf(" ** 1.登录 **\n"); printf(" ** **\n"); printf(" ** 2.注册 **\n"); printf(" ** **\n"); printf(" ** 3.找回密码 **\n"); printf(" ** **\n"); printf(" ** 4.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:"); } void Loginmenu() { printf(" 登录页面\n"); printf(" **************************************************\n"); printf(" ** 1.管理员登录 **\n"); printf(" ** **\n"); printf(" ** 2.用户登录 **\n"); printf(" ** **\n"); printf(" ** 3.返回 **\n"); printf(" ** **\n"); printf(" ** 4.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:"); } void Registmenu() { printf(" 注册页面\n"); printf(" **************************************************\n"); printf(" ** 1.管理员注册 **\n"); printf(" ** **\n"); printf(" ** 2.用户注册 **\n"); printf(" ** **\n"); printf(" ** 3.返回 **\n"); printf(" ** **\n"); printf(" ** 4.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:"); } void readermenu() { printf(" 用户页面\n"); printf(" **************************************************\n"); printf(" ** 1.借书 **\n"); printf(" ** **\n"); printf(" ** 2.还书 **\n"); printf(" ** **\n"); printf(" ** 3.浏览图书信息 **\n"); printf(" ** **\n"); printf(" ** 4.查看已借阅书籍 **\n"); printf(" ** **\n"); printf(" ** 5.修改密码 **\n"); printf(" ** **\n"); printf(" ** 6.返回 **\n"); printf(" ** **\n"); printf(" ** 7.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:"); } void mangermenu() { printf(" 管理员页面\n"); printf(" **************************************************\n"); printf(" ** 1.增加图书 **\n"); printf(" ** **\n"); printf(" ** 2.删除图书 **\n"); printf(" ** **\n"); printf(" ** 3.修改图书信息 **\n"); printf(" ** **\n"); printf(" ** 4.查看读者信息 **\n"); printf(" ** **\n"); printf(" ** 5.浏览图书信息 **\n"); printf(" ** **\n"); printf(" ** 6.修改密码 **\n"); printf(" ** **\n"); printf(" ** 7.返回 **\n"); printf(" ** **\n"); printf(" ** 8.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:"); } void changebookmenu() { printf("\n\n"); printf("\t\t\t\t*******************************\n"); printf("\t\t\t\t** 1.修改id号 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 2.修改图书名称 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 3.修改图书作者 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 4.修改出版社 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 5.修改图书数量 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 6.修改是否可借 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 7.返回 **\n"); printf("\t\t\t\t*******************************"); printf("\n\n"); } void browsemenu() { printf("\n\n\n\n"); printf("\t\t\t\t***********************************\n"); printf("\t\t\t\t** 1.按id号浏览 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 2.按图书数量浏览 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 3.返回 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 4.退出 **\n"); printf("\t\t\t\t***********************************\n"); printf("请按键选择:"); }
主程序
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <Windows.h> #include <string> #include <fstream> #include <conio.h> #include "menu.h" using namespace std; class User { protected : char account[20]; char password[20]; char spassword[20]; public : virtual void Login() = 0; virtual void Regist() = 0; virtual void save() = 0; virtual void BrowseBook() = 0; virtual void ChangePassword() = 0; virtual void FindPassword() = 0; }; class Reader : public User { private: int number; string name; string sex; int books; // 借书数量 int bookcnt;// 借阅次数 char booklist[20][20];// 储存借阅书籍 class Reader* next; public : virtual void Login(); virtual void Regist(); virtual void save(); virtual void BrowseBook(); virtual void ChangePassword(); virtual void FindPassword(); void creat(); void BrowseBorrowBook(Reader* pp); void BorrowBook(Reader* pp); void ReturnBook(Reader* pp); public : void set_number(int _number) { number = _number; }; void set_name(string _name) { name = _name; }; void set_sex(string _sex) { sex = _sex; }; void set_next(Reader* p) { next = p; }; void set_books(int _books) { books = _books; }; void set_bookcnt(int _bookcnt) { bookcnt = _bookcnt; }; int get_bookcnt() { return bookcnt; }; int get_number() { return number; }; int get_books() { return books; }; Reader* get_next() { return next; } string get_name() { return name; }; string get_sex() { return sex; }; char* get_account(Reader* p) { return p->account; }; char* get_password(Reader* p) { return p->password; }; char* get_spassword(Reader* p) { return p->spassword; }; char* get_booklist(Reader* p, int i) { return p->booklist[i]; }; }; class Manger : public User { private: class Manger* next; public : virtual void Login(); virtual void Regist(); virtual void BrowseBook(); virtual void save(); virtual void ChangePassword(); virtual void FindPassword(); void creat(); void AddBook(); void ChangeBook(); void DeleteBook(); void DeleteReaderBook(char* bookname); void BrowseReader(); public : void set_next(Manger* p) { next = p; }; Manger* get_next() { return next; }; char* get_account(Manger* p) { return p->account; }; char* get_password(Manger* p) { return p->password; }; char* get_spassword(Manger* p) { return p->spassword; }; }; class Book { private: int id; int amount; int flag; string bookname; string author; string press; class Book* next; public : void creat(); void save(); void show_book(); void set_id(int _id) { id = _id; }; void set_amount(int _amount) { amount = _amount; }; void set_flag(int _flag) { flag = _flag; }; void set_bookname(string _bookname) { bookname = _bookname; }; void set_author(string _author) { author = _author; }; void set_press(string _press) { press = _press; }; void set_next(Book* p) { next = p; }; int get_id() { return id; }; int get_amount() { return amount; }; int get_flag() { return flag; }; string get_bookname() { return bookname; }; string get_author() { return author; }; string get_press() { return press; }; Book* get_next() { return next; }; }; class DBFile { public : void cls(); void over(); void login(); void useLogin(User* base); void regist(); void useRegist(User* base); void usebrowsebook(User* base); void usesave(User* base); void usechangepassword(User* base); void usefindpassword(User* base); void findpassword(); int exist(char* acconut, int flag); void _main(); void idsort(); void bookamountsort(); void showidsort(); void showbookamountsort(); void mangerfunction(); void readerfunction(Reader* rp); }; Reader* rhead = NULL; Reader* _rend = NULL; Manger* mhead = NULL, * mend = NULL; Book* bhead = NULL; Book* bend = NULL; char code[20] = "glysw"; // 读者类 void Reader::Login() { int cnt = 4; int flag = 0; int k = 0; int i; char c; DBFile* fp = new DBFile; Reader* p = rhead->get_next(); char rpassword[2000]; char raccount[2000]; cout << "\n\n\n\n\n\n\n"; printf("请输入您的账户(按0退出):"); while (cin >> raccount) { if (strcmp(raccount, "0") == 0) { return; } if (strlen(raccount) <= 16) { break; } else { cout << "请输入长度为1到16位的账户:"; } } cout << "请输入您的密码(按0退出):"; for (i = 0; (c = _getch()) != '\r';) { if (c == '0') { return; } if (i <= 15 && isprint(c)) { rpassword[i] = c; i++; k++; cout << "*"; } if (k > 0 && c > 0 && c == '\b') { i--; k--; cout << "\b"; cout << " "; cout << "\b"; } } rpassword[i] = '\0'; cout << "\n"; while (p) { if (strcmp(p->account, raccount) == 0) { flag = 1; while (strcmp(p->password, rpassword) != 0) { cnt--; if (cnt == 1) { fp->cls(); cout << "\n\n\n\n\n\n\n"; cout << "密码输入次数已用完! " << "\n" << "即将退出登录页面..."; Sleep(3000); fp->cls(); return; } cout << "密码错误!您还有" << cnt - 1 << "次机会\n" << endl; cout << "请重新输入(按0退出):"; k = 0; for (i = 0; (c = _getch()) != '\r';) { if (c == '0') { return; } if (i <= 15 && isprint(c)) { rpassword[i] = c; i++; k++; cout << "*"; } if (k > 0 && c > 0 && c == '\b') { i--; k--; cout << "\b"; cout << " "; cout << "\b"; } } rpassword[i] = '\0'; cout << "\n"; } cout << "读者" << raccount << "登陆成功!\n" << "即将跳转至读者页面..." << endl; Sleep(2000); fp->cls(); fp->readerfunction(p); return; } p = p->get_next(); } if (!flag) { cout << "不存在此账号!\n请先前往注册!\n" << endl; cout << "即将跳转至主页面..."; Sleep(2000); } } void Reader::Regist() { DBFile* fp = new DBFile; Reader* rp = new Reader; char raccount[2000]; char rpassword[2000]; char rspassword[2000]; char name[200]; char sex[10]; int number; Reader* p = new Reader; cout << "\n\n\n\n\n\n\n"; cout << "请输入账户(按0退出):"; while (cin >> raccount) { if (strcmp(raccount, "0") == 0) { return; } if (strlen(raccount) <= 16 && !fp->exist(raccount, 0)) { break; } else if (fp->exist(raccount, 1)) { cout << "该账户已存在!请重新输入(按0退出):"; } else { cout << "请输入长度为1到16位的账户:"; } } strcpy_s(p->account, raccount); cout << "请输入密码(按0退出):"; while (cin >> rpassword) { if (strcmp(rpassword, "0") == 0) { return; } if (strlen(rpassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密码(按0退出):"; } } strcpy_s(p->password, rpassword); cout << "请输入您的密保(按0退出):"; while (cin >> rspassword) { if (strcmp(rspassword, "0") == 0) { return; } if (strlen(rspassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密保(按0退出):"; } } strcpy_s(p->spassword, rspassword); cout << "请输入您的姓名(按0退出):"; cin >> name; if (strcmp(name, "0") == 0) { return; } p->name = name; cout << "请输入您的性别(按0退出):"; while (cin >> sex) { if (strcmp(sex, "0") == 0) { return; } if (strcmp(sex, "男") == 0 || strcmp(sex, "女") == 0) { p->sex = sex; break; } else { cout << "请输入男或女(按0退出):"; } } cout << "请输入您的学号(按0退出):"; while (cin >> number) { if (number == 0) { return; } if (number >= 0) { p->number = number; break; } else { cout << "请输入正确格式的学号(按0退出):"; } } _rend->set_next(p); _rend = p; _rend->set_next(NULL); rp->save(); fp->cls(); printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t*******************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 读者%s注册成功!! \n", raccount); printf("\t\t\t\t\t** 即将跳转至主页面... **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t*******************************\n"); Sleep(1500); } void Reader::save() { Reader* rp = rhead->get_next(); ofstream f_reader("reader.txt", ios::out); while (rp) { f_reader << rp->account << " " << rp->password << " " << rp->spassword << " " << rp->number << " " << rp->name << " " << rp->sex << " " << rp->bookcnt << " " << rp->books; for (int i = 0; i < rp->bookcnt; i++) { f_reader << " " << booklist[i]; } f_reader << endl; rp = rp->get_next(); } f_reader.close(); } void Reader::BrowseBook() { DBFile* fp = new DBFile; int sign = 0; while (1) { fp->cls(); browsemenu(); char choose = _getch(); switch (choose) { case '1': fp->cls(); fp->showidsort(); break; case '2': fp->cls(); fp->showbookamountsort(); break; case '3': fp->cls(); sign = 1; break; case '4': fp->cls(); fp->over(); break; default: break; } if (sign) { break; } } } void Reader::creat() { ifstream f_reader("reader.txt", ios::in); char _account[20]; char _password[20]; char _spassword[20]; int number; int bookcnt; int books; char name[20]; char sex[20]; char booklist[20][20]; while (f_reader >> _account && f_reader >> _password && f_reader >> _spassword) { Reader* p = new Reader; strcpy_s(p->account, _account); strcpy_s(p->password, _password); strcpy_s(p->spassword, _password); _rend->set_next(p); p->set_next(NULL); _rend = _rend->get_next(); } while (f_reader >> number && f_reader >> name && f_reader >> sex && f_reader >> bookcnt && f_reader >> books) { Reader* p = new Reader; p->number = number; p->sex = sex; p->name = name; p->bookcnt = bookcnt; p->books = books; _rend->set_next(p); p->set_next(NULL); _rend = _rend->get_next(); } while (f_reader >> bookcnt) { Reader* p = new Reader; for (int i = 0; i < bookcnt; i++) { f_reader >> booklist[i]; strcpy_s(p->booklist[i], booklist[i]); } _rend->set_next(p); p->set_next(NULL); _rend = _rend->get_next(); } f_reader.close(); } void Reader::BrowseBorrowBook(Reader* pp) { int cnt = pp->bookcnt; long long sign; if (cnt == 0) { printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t您还未借阅任何书籍!\n"); } printf("\n\n\n\n\n\n"); for (int i = 0; i < cnt; i++) { printf("\t第%d次借阅的书籍:", i + 1); printf("%s\t", pp->get_booklist(pp, i)); cout << "借阅数量:" << pp->get_books() << endl; } printf("\n\n请输入0退出:"); while (scanf("%lld", &sign)) { if (sign == 0) { break; } else { printf("-------------无效的输入!请重新输入!"); } } } void Reader::BorrowBook(Reader* pp) { Book* bp = bhead->get_next(); Book* _bp = new Book; _bp->show_book(); char bookname[200]; int amount; int flag = 1; int sign = 1; int exist = 0; cout << "\n\n\n\n"; cout << "请输入要借书的名称(按0退出):"; cin >> bookname; if (strcmp(bookname, "0") == 0) { return; } while (bp) { if (bp->get_bookname() == bookname) { if (bp->get_amount() == 0) { cout << "此书无余量!\n"; sign = 0; } if (bp->get_flag() == 0) { cout << "此书不可借!\n"; flag = 0; } if (sign == 1 && flag == 1) { cout << "请输入要借阅书籍的数量:"; while (cin >> amount) { if (bp->get_amount() < amount) { cout << "借阅数量超过此书已有库存!请重新输入:"; } else { int a = pp->get_books() + amount; pp->set_books(a); int b = bp->get_amount() - amount; bp->set_amount(b); break; } } pp->bookcnt++; strcpy_s(pp->booklist[pp->bookcnt - 1], bookname); pp->save(); bp->save(); exist = 1; break; } } bp = bp->get_next(); } if (exist == 0 && sign == 1 && flag == 1) { cout << "不存在此书!!\n"; } else if (exist == 1) { cout << "图书:" << bookname << "借阅成功!!\n"; } cout << "页面即将跳转..."; Sleep(1200); } void Reader::ReturnBook(Reader* pp) { Book* bp = bhead->get_next(); char bookname[200]; int amount; int exist = 0; int flag = 0; int cnt = pp->bookcnt; if (cnt == 0) { printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t您还未借阅任何书籍!\n"); system("pause"); return; } printf("\n\n\n\n\n\n"); for (int i = 0; i < cnt; i++) { cout << "-------------第" << i + 1 << "次借阅的书籍:" << pp->booklist[i] << "\t\t"; cout << "借阅数量:" << pp->books << endl; } cout << "请输入您要退还的书籍(按0退出):"; while (cin >> bookname) { if (strcmp(bookname, "0") == 0) { return ; } for (int i = 0; i < pp->bookcnt; i++) { if (strcmp(pp->booklist[i], bookname) == 0) { flag = i; exist = 1; break; } } if (!exist) { cout << "未查询到该书!\n"; cout << "请重新输入(按0退出):"; } else { break; } } while (bp) { if (bp->get_bookname() == bookname) { cout << "请输入您要退还书籍的数量:"; while (cin >> amount) { if (pp->books < amount) { cout << "您未持有此数量的书籍!\n"; cout << "请重新输入:"; } else { int a = pp->books - amount; int b = bp->get_amount() + amount; pp->set_books(a); bp->set_amount(b); break; } } } bp = bp->get_next(); } if (pp->books == 0) { for (int i = flag; i < pp->books - 1; i++) { strcpy_s(pp->booklist[i], pp->booklist[i + 1]); } pp->bookcnt--; } pp->save(); bp->save(); cout << bookname << "退还成功!!\n"; cout << "即将返回上一页面..."; Sleep(1100); } void Reader::FindPassword() { Reader* rp = rhead->get_next(); char password[200]; char account[200]; char spassword[200]; int flag = 0; int sign = 0; cout << "请输入您的账户(按0退出):"; while (cin >> account) { if (strcmp(account, "0") == 0) { return; } while (rp) { if (strcmp(rp->get_account(rp), account) == 0) { flag = 1; break; } rp = rp->get_next(); } if (flag) { break; } else { printf("此账号不存在!请重新输入(按0退出):"); rp = rhead->get_next(); } } if (flag) { rp = rhead->next; printf("请输入您设置的密保(按0退出):"); while (cin >> spassword) { if (strcmp(spassword, "0") == 0) { return; } while (rp) { if (strcmp(rp->get_spassword(rp), spassword) == 0) { sign = 1; break; } rp = rp->get_next(); } if (sign) { printf("密保输入正确!\n"); printf("您的密码为%s\n", rp->get_password(rp)); system("pause"); return; } else { printf("-------------密保输入错误!请重新输入(按0退出):"); rp = rhead->get_next(); } } } } void Reader::ChangePassword() { DBFile* fp = new DBFile; Reader* p = rhead->get_next(), * rp = new Reader; int flag = 0; int sign = 0; char raccount[200]; char rpassword[200]; char rspassword[200]; cout << "请输入您的账户(按0退出):"; while (cin >> raccount) { if (strcmp(raccount, "0") == 0) { return; } while (p) { if (strcmp(p->account, raccount) == 0) { flag = 1; break; } p = p->get_next(); } if (flag) { break; } else { cout << "此账号不存在!请重新输入(按0退出):"; p = rhead->get_next(); } } if (flag) { p = rhead->get_next(); cout << "请输入您设置的密保(按0退出):"; while (cin >> rspassword) { if (strcmp(rspassword, "0") == 0) { return; } while (p) { if (strcmp(p->spassword, rspassword) == 0) { sign = 1; break; } p = p->get_next(); } if (sign) { cout << "请输入修改后的密码(按0退出):"; while (cin >> rpassword) { if (strcmp(rpassword, "0") == 0) { return; } if (strlen(rpassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密码(按0退出):"; } } strcpy_s(p->password, rpassword); cout << "密码修改成功!"; fp->usesave(p); system("pause"); return; } else { cout << "密保输入错误!!请重新输入(按0退出):"; p = rhead->get_next(); } } } } // 数据操作类 void DBFile::cls() { system("cls"); } void DBFile::over() { DBFile* dbp = new DBFile; dbp->cls(); system("color 6"); int time = 6; printf("\n\n\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t感谢您的使用!!\n\n\n\n\n"); printf(" 程序即将关闭..."); while (--time >= 1) { printf("\r%d", time); printf("\b"); Sleep(1000); } dbp->cls(); exit(0); } void DBFile::login() { Reader* rp = new Reader; Manger* mp = new Manger; int sign = 0; while (1) { cls(); Loginmenu(); char choose = _getch(); switch (choose) { case '1': cls(); useLogin(mp); break; case '2': cls(); useLogin(rp); break; case '3': cls(); sign = 1; break; case '4': cls(); over(); break; default: cout << "输入无效!!"; break; } if (sign) { break; } } } void DBFile::useLogin(User* base) { base->Login(); } void DBFile::regist() { Reader* rp = new Reader; Manger* mp = new Manger; int sign = 0; while (1) { cls(); Registmenu(); char choose = _getch(); switch (choose) { case '1': cls(); useRegist(mp); break; case '2': cls(); useRegist(rp); break; case '3': cls(); sign = 1; break; case '4': cls(); over(); break; default: cout << "无效输入!!"; break; } if (sign) { break; } } } void DBFile::usesave(User* base) { base->save(); } void DBFile::usechangepassword(User* base) { base->ChangePassword(); } void DBFile::useRegist(User* base) { base->Regist(); } void DBFile::usebrowsebook(User* base) { base->BrowseBook(); } void DBFile::usefindpassword(User* base) { base->FindPassword(); } void DBFile::findpassword() { printf("\n\n\n\n\n\n"); printf("\t\t\t\t\t********************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 1.管理员找回密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 2.用户找回密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 3.返回 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t********************************\n"); printf("请按键选择:"); int choose; int sign = 0; Reader* rp = new Reader; Manger* mp = new Manger; scanf("%d", &choose); while (1) { cls(); switch (choose) { case 1: cls(); usefindpassword(mp); sign = 1; break; case 2: cls(); usefindpassword(mp); sign = 1; break; case 3: sign = 1; break; default: printf("输入无效!"); break; } if (sign) { break; } } } int DBFile::exist(char* account, int flag) { if (flag == 0) { Reader* p = rhead->get_next(); while (p != NULL) { if (strcmp(account, p->get_account(p)) == 0) { return 1; } p = p->get_next(); } } else if (flag == 1) { Manger* p = mhead->get_next(); while (p != NULL) { if (strcmp(account, p->get_account(p) ) == 0) { return 1; } p = p->get_next(); } } return 0; } void DBFile::_main() { while (1) { cls(); _mainmenu(); char choose = _getch(); switch (choose) { case '1': cls(); login(); break; case '2': cls(); regist(); break; case '3': cls(); findpassword(); break; case '4': cls(); over(); break; default: cout << "无效输入!!"; break; } } } void DBFile::idsort() { Book* f, *x, *y, *p; f = NULL; if (bhead->get_next() == NULL || bhead->get_next()->get_next() == NULL) { return; } while (f != bhead->get_next()->get_next()) { for (p = bhead; p->get_next()->get_next() != f; p = p->get_next()) { if (p->get_next()->get_id() > p->get_next()->get_next()->get_id()) { x = p->get_next(); y = p->get_next()->get_next(); p->set_next(y); x->set_next(y->get_next()); y->set_next(x); } } f = p->get_next(); } } void DBFile::bookamountsort() { Book* f, * x, * y, * p; f = NULL; if (bhead->get_next() == NULL || bhead->get_next()->get_next() == NULL) { return; } while (f != bhead->get_next()->get_next()) { for (p = bhead; p->get_next()->get_next() != f; p = p->get_next()) { if (p->get_next()->get_amount() > p->get_next()->get_next()->get_amount()) { x = p->get_next(); y = p->get_next()->get_next(); p->set_next(y); x->set_next(y->get_next()); y->set_next(x); } } f = p->get_next(); } } void DBFile::showidsort() { DBFile* fp = new DBFile; Book* bp = new Book; fp->idsort(); bp->show_book(); system("pause"); } void DBFile::showbookamountsort() { DBFile* fp = new DBFile; Book* bp = new Book; fp->bookamountsort(); bp->show_book(); system("pause"); } void DBFile::mangerfunction() { DBFile* fp = new DBFile; Manger* mp = new Manger; int sign = 0; int choose; while (1) { fp->cls(); mangermenu(); cin >> choose; switch (choose) { case 1: fp->cls(); mp->AddBook(); break; case 2: fp->cls(); mp->DeleteBook(); break; case 3: fp->cls(); mp->ChangeBook(); break; case 4: fp->cls(); mp->BrowseReader(); break; case 5: fp->cls(); mp->BrowseBook(); break; case 6: fp->cls(); fp->usechangepassword(mp); break; case 7: fp->cls(); sign = 1; break; case 8: fp->cls(); fp->over(); break; default: cout << "输入无效!!"; break; } if (sign) { break; } } } void DBFile::readerfunction(Reader* pp) { DBFile* fp = new DBFile; Reader* rp = new Reader; int sign = 0; while (1) { fp->cls(); readermenu(); char choose = _getch(); switch (choose) { case '1': fp->cls(); rp->BorrowBook(pp); break; case '2': fp->cls(); rp->ReturnBook(pp); break; case '3': fp->cls(); fp->usebrowsebook(rp); break; case '4': fp->cls(); rp->BrowseBorrowBook(pp); break; case '5': fp->cls(); fp->usechangepassword(rp); break; case '6': fp->cls(); sign = 1; break; case '7': fp->over(); break; default: cout << "输入无效!!"; break; } if (sign) { break; } } } // 管理员类 void Manger::Login() { int cnt = 4; int flag = 0; int k = 0; int i; char c; DBFile* fp = new DBFile; Manger* p = mhead->get_next(); char mpassword[2000]; char maccount[2000]; cout << "\n\n\n\n\n\n\n"; printf("请输入您的账户(按0退出):"); while (cin >> maccount) { if (strcmp(maccount, "0") == 0) { return; } if (strlen(maccount) <= 16) { break; } else { cout << "请输入长度为1到16位的账户:"; } } cout << "请输入您的密码(按0退出):"; for (i = 0; (c = _getch()) != '\r';) { if (c == '0') { return; } if (i <= 15 && isprint(c)) { mpassword[i] = c; i++; k++; printf("*"); } if (k > 0 && c > 0 && c == '\b') { i--; k--; printf("\b"); printf(" "); printf("\b"); } } mpassword[i] = '\0'; printf("\n"); while (p) { if (strcmp(p->account, maccount) == 0) { flag = 1; while (strcmp(p->password, mpassword) != 0) { cnt--; if (cnt == 1) { fp->cls(); cout << "\n\n\n\n\n\n\n"; cout << "密码输入次数已用完! " << "\n" << "即将退出登录页面..."; Sleep(3000); fp->cls(); return; } cout << "密码错误!您还有"<< cnt - 1 << "次机会\n"<< endl; cout << "请重新输入(按0退出):"; k = 0; for (i = 0; (c = _getch()) != '\r';) { if (c == '0') { return; } if (i <= 15 && isprint(c)) { mpassword[i] = c; i++; k++; printf("*"); } if (k > 0 && c > 0 && c == '\b') { i--; k--; printf("\b"); printf(" "); printf("\b"); } } mpassword[i] = '\0'; printf("\n"); } cout << "管理员"<< maccount << "登陆成功!\n" << "即将跳转至管理员页面..." << endl; Sleep(2000); fp->cls(); fp->mangerfunction(); return; } p = p->get_next(); } if (!flag) { cout << "不存在此账号!\n请先前往注册!\n" << endl; cout << "即将跳转至主页面..."; Sleep(2000); } } void Manger::Regist() { DBFile* fp = new DBFile; Manger* mp = new Manger; char maccount[2000]; char mpassword[2000]; char mspassword[2000]; char mangercode[2000]; cout << "\n\n\n\n\n\n\n"; cout << "请输入管理员验证码:"; cin >> mangercode; int cnt = 2; while (--cnt >= 0) { if (strcmp(mangercode, code) == 0) { cout << "管理员验证成功!!\n" << "即将跳转至注册页面..."; Sleep(1000); fp->cls(); break; } else { cout << "管理员验证失败!!\n" << "您还有" << cnt + 1 << "次机会\n"; cout << "请重新输入:"; cin >> mangercode; } if (cnt == 0) { cout << "输入次数已用完\n" << "即将退出管理员注册系统..."; Sleep(1000); fp->cls(); return; } } Manger* p = new Manger; cout << "\n\n\n\n\n\n\n"; cout << "请输入账户(按0退出):"; while (cin >> maccount) { if (strcmp(maccount, "0") == 0) { return; } if (strlen(maccount) <= 16 && !fp->exist(maccount, 1)) { break; } else if (fp->exist(maccount, 1)) { cout << "该账户已存在!请重新输入(按0退出):"; } else { cout << "请输入长度为1到16位的账户:"; } } strcpy_s(p->account, maccount); cout << "请输入密码(按0退出):"; while (cin >> mpassword) { if (strcmp(mpassword, "0") == 0) { return; } if (strlen(mpassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密码(按0退出):"; } } strcpy_s(p->password, mpassword); cout << "请输入您的密保(按0退出):"; while (cin >> mspassword) { if (strcmp(mspassword, "0") == 0) { return; } if (strlen(mspassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密保(按0退出):"; } } strcpy_s(p->spassword, mspassword); mend->set_next(p); mend = p; mend->set_next(NULL); mp->save(); fp->cls(); printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t*******************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 管理员%s注册成功!! \n", maccount); printf("\t\t\t\t\t** 即将跳转至主页面... **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t*******************************\n"); Sleep(1500); } void Manger::BrowseBook() { DBFile* fp = new DBFile; int sign = 0; while (1) { fp->cls(); browsemenu(); char choose = _getch(); switch (choose) { case '1': fp->cls(); fp->showidsort(); break; case '2': fp->cls(); fp->showbookamountsort(); break; case '3': fp->cls(); sign = 1; break; case '4': fp->cls(); fp->over(); break; default: break; } if (sign) { break; } } } void Manger::save() { Manger* mp = mhead->get_next(); ofstream f_manger("manger.txt", ios::out); while (mp) { f_manger << mp->account << " " << mp->password << " " << mp->spassword << endl; mp = mp->get_next(); } f_manger.close(); } void Manger::creat() { char account[20]; char password[20]; char spassword[20]; ifstream f_manger("manger.txt", ios::in); while (f_manger >> account && f_manger >> password && f_manger >> spassword) { Manger* p = new Manger; strcpy_s(p->account, account); strcpy_s(p->password, password); strcpy_s(p->spassword, password); mend->set_next(p); p->set_next(NULL); mend = mend->get_next(); } f_manger.close(); } void Manger::AddBook() { DBFile* fp = new DBFile; Book* p = bhead->get_next(); int a; int id = 0; int amount; char bookname[2000]; do { fp->cls(); cout << "\n\n\n\n\n"; cout << "请输入要添加书名id(按0退出):"; while (cin >> id) { if (id == 0) { return; } if (id < 0) { cout << "请输入正确格式的id号(按0退出):"; } else { break; } } int flag = 0; while (p) { if (p->get_id() == id) { flag = 1; break; } p = p->get_next(); } if (flag) { cout << "已查出此书,请输入再次增加图书的数量:"; while (cin >> amount) { if (amount <= 199 && amount > 0) { int temp = p->get_amount() + amount; p->set_amount(temp); cout << "图书" << p->get_bookname() << "添加成功!"; break; } else { cout << "请输入数量为0-199以内的图书:"; } } } else { p = new Book; p->set_id(id); cout << "请输入增加图书的数量:"; while (cin >> amount) { if (amount <= 199 && amount > 0) { p->set_amount(amount); break; } else { cout << "请输入数量为0-199范围以内的图书:"; } } cout << "请输入是否可借:"; int sign = 0; while (cin >> sign) { if (sign == 1 || sign == 0) { p->set_flag(sign); break; } else if (sign < 0 || sign > 1) { cout << "请输入正确格式的可借标志(1为可借,0为不可借):"; } } char bookname[200]; cout << "请输入书名:"; cin >> bookname; p->set_bookname(bookname); char author[200]; cout << "请输作者:"; cin >> author; p->set_author(author); char press[200]; cout << "请输入出版社:"; cin >> press; p->set_press(press); bend->set_next(p); bend = p; bend->set_next(NULL); } p->save(); fp->cls(); printf("\n\n\n\n\n\n"); printf("\t\t\t\t\t********************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 图书%d添加成功! \n", bend->get_id()); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t********************************\n"); Sleep(1000); fp->cls(); cout << "\n\n\n\n\n"; cout << "是否继续添加?\n" << "继续请按1,返回请按0:"; cin >> a; } while (a == 1); printf("页面即将跳转..."); Sleep(1000); } void Manger::ChangeBook() { DBFile* fp = new DBFile; Book* p = bhead->get_next(); int id; int amount; int flag = 0; char bookname[200]; char author[200]; char press[200]; int exist = 0; int sign = 0; Book* bp = bhead->get_next(); bp->show_book(); cout << "请输入要修改书名的id号(按0退出):"; while (cin >> id) { if (id == 0) { return; } while (p) { if (p->get_id() == id) { exist = 1; break; } p = p->get_next(); } if (!exist) { p = bhead->get_next(); cout << "为查询到该id号!请重新输入(按0退出):"; } else { break; } } if (!exist) { return; } else { int sign = 0; int choose; fp->cls(); changebookmenu(); while (1) { cout << "请输入您要进行的操作:"; cin >> choose; switch (choose) { case 1: cout << "请输入修改后的id号:"; cin >> id; p->set_id(id); cout << "修改成功!!\n"; break; case 2: cout << "请输入要修改后的图书名称:"; cin >> bookname; p->set_bookname(bookname); cout << "修改成功!!\n"; break; case 3: cout << "请输入要修改后的作者:"; cin >> author; p->set_author(author); cout << "修改成功!!\n"; break; case 4: cout << "请输入修改后的出版社:"; cin >> press; p->set_press(press); cout << "修改成功!!\n"; break; case 5 : cout << "请输入修改后的图书数量:"; while (cin >> amount) { if (amount <= 199 && amount > 0) { p->set_amount(amount); break; } else { cout << "请输入0-199以内的图书数量:"; } } cout << "修改成功!!\n"; break; case 6 : if (p->get_flag() == 1) { cout << "是否将此书修改为不可借?(1确认,2返回):"; while (cin >> flag) { if (flag == 2) { break; } if (flag == 1) { p->set_flag(1); p->save(); cout << "修改成功!!\n"; break; } else { cout << "请输入正确格式的借书标志!(1确认,2返回):"; } } } else if (p->get_flag() == 0) { cout << "是否将此书修改为可借?(1确认,2返回):"; while (cin >> flag) { if (flag == 2) { break; } if (flag == 1) { p->set_flag(0); p->save(); cout << "修改成功!!\n"; break; } else { cout << "请输入正确格式的借书标志!(1确认,2返回):"; } } } break; case 7: sign = 1; break; default : cout << "输入无效!!"; cin.clear(); break; } if (sign) { break; } } } } void Manger::DeleteBook() { Book* bp = bhead->get_next(); char bookname[20]; int flag = 0; bp->show_book(); cout << "请输入您要删除图书的名称(按0退出):"; while (cin >> bookname) { if (strcmp(bookname, "0") == 0) { return; } while (bp) { if (bp->get_bookname() == bookname) { flag = 1; break; } bp = bp->get_next(); } if (flag == 0) { cout << "未查询到此书!\n" << "请重新输入(按0退出):"; bp = bhead->get_next(); } else { break; } } DeleteReaderBook(bookname); bp = bhead->get_next(); Book* pp = bhead; while (bp) {// bp->next 可能是链表的最后一个 if (bp->get_bookname() == bookname) { if (bp->get_next() == NULL) { bhead->set_next(NULL); } else { pp->set_next(bp->get_next()); } } pp = pp->get_next(); bp = bp->get_next(); } cout << "\n\n\n\n\n\n"; cout << "图书" << bookname << "删除成功!!\n"; cout << "页面即将跳转.."; Sleep(1500); Book* p = new Book; p->save(); } void Manger::DeleteReaderBook(char* bookname) { Reader* rp = rhead->get_next(); Reader* pp = new Reader; int flag = 0; while (rp) { flag = 1; for (int i = 0; i < rp->get_bookcnt(); i++) { if (strcmp(rp->get_booklist(rp, i), bookname) == 0) { flag = i; break; } } if (flag != 1) { for (int j = flag; j < rp->get_bookcnt() - 1; j++) { strcpy(rp->get_booklist(rp, j), rp->get_booklist(rp, j + 1)); } int temp = rp->get_bookcnt(); temp--; rp->set_bookcnt(temp); } rp = rp->get_next(); } pp->save(); } void Manger::BrowseReader() { cout << "账户\t\t" << "密码\t\t" << "密保\t\t" << "性别\t\t" << "姓名\t\t" << "学号\t\t" << "借书次数\n"; Reader* rp = rhead->get_next(); while (rp) { cout << rp->get_account(rp) << "\t\t" << rp->get_password(rp) << "\t\t" << rp->get_spassword(rp) << "\t\t" << rp->get_sex() << "\t\t" << rp->get_name() << "\t\t" << rp->get_number() << "\t\t" << rp->get_bookcnt() << endl; rp = rp->get_next(); } system("pause"); } void Manger::FindPassword() { Manger* mp = mhead->get_next(); char password[200]; char account[200]; char spassword[200]; int flag = 0; int sign = 0; cout << "请输入您的账户(按0退出):"; while (cin >> account) { if (strcmp(account, "0") == 0) { return; } while (mp) { if (strcmp(mp->get_account(mp), account) == 0) { flag = 1; break; } mp = mp->get_next(); } if (flag) { break; } else { printf("此账号不存在!请重新输入(按0退出):"); mp = mhead->get_next(); } } if (flag) { mp = mhead->get_next(); printf("请输入您设置的密保(按0退出):"); while (cin >> spassword) { if (strcmp(spassword, "0") == 0) { return; } while (mp) { if (strcmp(mp->get_spassword(mp), spassword) == 0) { sign = 1; break; } mp = mp->get_next(); } if (sign) { printf("密保输入正确!\n"); printf("您的密码为%s\n", mp->get_password(mp)); system("pause"); return; } else { printf("-------------密保输入错误!请重新输入(按0退出):"); mp = mhead->get_next(); } } } } void Manger::ChangePassword() { DBFile* fp = new DBFile; Manger* p = mhead->get_next(), *mp = new Manger; int flag = 0; int sign = 0; char maccount[200]; char mpassword[200]; char mspassword[200]; cout << "请输入您的账户(按0退出):"; while (cin >> maccount) { if (strcmp(maccount, "0") == 0) { return; } while (p) { if (strcmp(p->account, maccount) == 0) { flag = 1; break; } p = p->get_next(); } if (flag) { break; } else { cout << "此账号不存在!请重新输入(按0退出):"; p = mhead->get_next(); } } if (flag) { p = mhead->get_next(); cout << "请输入您设置的密保(按0退出):"; while (cin >> mspassword) { if (strcmp(mspassword, "0") == 0) { return; } while (p) { if (strcmp(p->spassword, mspassword) == 0) { sign = 1; break; } p = p->get_next(); } if (sign) { cout << "请输入修改后的密码(按0退出):"; while (cin >> mpassword) { if (strcmp(mpassword, "0") == 0) { return; } if (strlen(mpassword) <= 16) { break; } else { cout << "请输入长度为1到16位的密码(按0退出):"; } } strcpy_s(p->password, mpassword); cout << "密码修改成功!"; fp->usesave(p); system("pause"); return; } else { cout << "密保输入错误!!请重新输入(按0退出):"; p = mhead->get_next(); } } } } // 图书类 void Book::creat() { ifstream f_book("book.txt", ios::in); int id; int amount; int flag; string bookname; string author; string press; while (f_book >> id && f_book >> flag && f_book >> amount && f_book >> bookname && f_book >> author && f_book >> press) { Book* p = new Book; p->id = id; p->flag = flag; p->amount = amount; p->bookname = bookname; p->author = author; p->press = press; bend->set_next(p); p->set_next(NULL); bend = bend->get_next(); } f_book.close(); } void Book::save() { Book* bp = bhead->get_next(); ofstream f_book("book.txt", ios::out); while (bp) { f_book << bp->id << " " << bp->flag << " " << bp->amount << " " << bp->bookname << " " << bp->author << " " << bp->press << endl; bp = bp->get_next(); } f_book.close(); } void Book::show_book() { Book* bp = bhead->get_next(); cout << "id号\t\t" << "是否可借\t\t" << "数量\t\t" << "书名\t\t" << "作者\t\t" << "出版社\n"; while (bp) { cout << bp->get_id() << "\t\t" << bp->get_flag() << "\t\t\t" << bp->get_amount() << "\t\t" << bp->get_bookname() << "\t\t" << bp->get_author() << "\t\t" << bp->get_press() << "\n"; bp = bp->get_next(); } } int main() { mhead = new Manger; mend = new Manger; mhead->set_next(NULL); mend = mhead; rhead = new Reader; _rend = new Reader; rhead->set_next(NULL); _rend = rhead; bhead = new Book; bend = new Book; bhead->set_next(NULL); bend = bhead; Reader* rp = new Reader; rp->creat(); Manger* mp = new Manger; mp->creat(); Book* bp = new Book; bp->creat(); DBFile* dbp = new DBFile; system("color B"); dbp->_main(); delete dbp; return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)