您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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; }
讀到這里,這篇“C++如何實現圖書管理系統”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。