<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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("nn"); 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("nn"); printf("tttt*******************************n"); printf("tttt** 1.修改id號 **n"); printf("tttt** **n"); printf("tttt** 2.修改圖書名稱 **n"); printf("tttt** **n"); printf("tttt** 3.修改圖書作者 **n"); printf("tttt** **n"); printf("tttt** 4.修改出版社 **n"); printf("tttt** **n"); printf("tttt** 5.修改圖書數量 **n"); printf("tttt** **n"); printf("tttt** 6.修改是否可借 **n"); printf("tttt** **n"); printf("tttt** 7.返回 **n"); printf("tttt*******************************"); printf("nn"); } void browsemenu() { printf("nnnn"); printf("tttt***********************************n"); printf("tttt** 1.按id號瀏覽 **n"); printf("tttt** **n"); printf("tttt** 2.按圖書數量瀏覽 **n"); printf("tttt** **n"); printf("tttt** 3.返回 **n"); printf("tttt** **n"); printf("tttt** 4.退出 **n"); printf("tttt***********************************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 << "nnnnnnn"; 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] = ' '; 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 << "nnnnnnn"; 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] = ' '; 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 << "nnnnnnn"; 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("nnnnnnn"); printf("ttttt*******************************n"); printf("ttttt** **n"); printf("ttttt 讀者%s註冊成功!! n", raccount); printf("ttttt** 即將跳轉至主頁面... **n"); printf("ttttt** **n"); printf("ttttt*******************************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("nnnnnnn"); printf("tttttt您還未借閱任何書籍!n"); } printf("nnnnnn"); for (int i = 0; i < cnt; i++) { printf("t第%d次借閱的書籍:", i + 1); printf("%st", pp->get_booklist(pp, i)); cout << "借閱數量:" << pp->get_books() << endl; } printf("nn請輸入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 << "nnnn"; 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("nnnnnnn"); printf("tttttt您還未借閱任何書籍!n"); system("pause"); return; } printf("nnnnnn"); for (int i = 0; i < cnt; i++) { cout << "-------------第" << i + 1 << "次借閱的書籍:" << pp->booklist[i] << "tt"; 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("您的密碼為%sn", 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("nnnnnnnnn"); printf("tttttt感謝您的使用!!nnnnn"); 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("nnnnnn"); printf("ttttt********************************n"); printf("ttttt** **n"); printf("ttttt** 1.管理員找回密碼 **n"); printf("ttttt** **n"); printf("ttttt** 2.使用者找回密碼 **n"); printf("ttttt** **n"); printf("ttttt** 3.返回 **n"); printf("ttttt** **n"); printf("ttttt********************************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 << "nnnnnnn"; 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] = ' '; 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 << "nnnnnnn"; 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] = ' '; 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 << "nnnnnnn"; 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 << "nnnnnnn"; 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("nnnnnnn"); printf("ttttt*******************************n"); printf("ttttt** **n"); printf("ttttt 管理員%s註冊成功!! n", maccount); printf("ttttt** 即將跳轉至主頁面... **n"); printf("ttttt** **n"); printf("ttttt*******************************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 << "nnnnn"; 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("nnnnnn"); printf("ttttt********************************n"); printf("ttttt** **n"); printf("ttttt 圖書%d新增成功! n", bend->get_id()); printf("ttttt** **n"); printf("ttttt********************************n"); Sleep(1000); fp->cls(); cout << "nnnnn"; 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 << "nnnnnn"; 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 << "賬戶tt" << "密碼tt" << "密保tt" << "性別tt" << "姓名tt" << "學號tt" << "借書次數n"; Reader* rp = rhead->get_next(); while (rp) { cout << rp->get_account(rp) << "tt" << rp->get_password(rp) << "tt" << rp->get_spassword(rp) << "tt" << rp->get_sex() << "tt" << rp->get_name() << "tt" << rp->get_number() << "tt" << 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("您的密碼為%sn", 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號tt" << "是否可借tt" << "數量tt" << "書名tt" << "作者tt" << "出版社n"; while (bp) { cout << bp->get_id() << "tt" << bp->get_flag() << "ttt" << bp->get_amount() << "tt" << bp->get_bookname() << "tt" << bp->get_author() << "tt" << 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; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45