#include <iostream>
#include <fstream>
#include <io.h>
#include <process.h>
#include <Windows.h>
#include <string>
#include <map>
#define R_OK 4
#define W_OK 2
#define X_OK 1
#define F_OK 0
using namespace std;
int main();
void Init();
void ChangeUserName();
void ChangeUserPassword();
void LoginView();
void WriteUsers();
void UserView();
void WriteBooks();
void SittingView();
void LoadData();
void WriteData();
char *StringToChar(const string &object);
void pause();
void cls();
void AboutAuthor();
void AutoRun(const string &RegName);
int RunAsAdmin(int ShowCmd);
bool IsProcessRunAsAdmin();
void BookList();
void LoadUsers();
void LoadBooks();
void HideWindow();
void BookBorrowed();
void AddBook();
void BorrowBook();
void DeleteBook();
void ReturnBook();
string getDiskName();
void CreateUser();
void RemoveUser();
void UserList();
void SearchBooks();
void Uninstall();
map<string, string> Users;
map<string, string> Books;
string UserName;
bool isUsersChanged = false;
bool isBooksChanged = false;
bool isFirstBoot = _access("Users.dat", F_OK);
void Uninstall() {
cls();
string an;
cout<<"Are you really wanna uninstall? (y/n)"<<endl;
cin>>an;
if (an == "y" || an == "yes"){
cout<<"See you then !"<<endl;
pause();
string BookDat = "del " + getDiskName() + ":\\" + "Books.dat";
string UserDat = "del " + getDiskName() + ":\\" + "Users.dat";
string Myself1 = __argv[0];
string Myself = "del " + Myself1;
fstream u("unist.bat", ios::out);
u << BookDat << endl;
u << UserDat << endl;
u << Myself << endl;
u << "del unist.bat" << endl;
u.close();
WinExec("unist.bat",0);
exit(0);
} else{
SittingView();
}
}
void SearchBooks() {
map<string, string>::iterator iterator;
string name;
bool in = false;
cls();
cout << "Please input the Book's name: " << endl;
cin >> name;
for (iterator = Books.begin(); iterator != Books.end(); iterator++) {
if (iterator->first == name) {
in = true;
}
}
if (in) {
cout << "Yes , it is in bookcase." << endl;
pause();
} else {
cout << "It is not in BookCase. " << endl;
pause();
}
}
void UserList() {
map<string, string>::iterator iterator;
cls();
cout << "===========" << endl;
for (iterator = Users.begin(); iterator != Users.end(); iterator++) {
cout << iterator->first << endl;
}
cout << "===========" << endl;
}
void CreateUser() {
cls();
string name;
string password;
cout << "Please input name and password. (Don't input the account that has been in, or it'll be override)" << endl;
cin >> name >> password;
Users.insert(pair<string, string>(name, password));
isUsersChanged = true;
cout << "Create User Successfully." << endl;
pause();
}
void RemoveUser() {
string name;
UserList();
cout << "Please input the User You Wanna remove: " << endl;
cin >> name;
Users.erase(name);
isUsersChanged = true;
cout << "remove successfully." << endl;
pause();
}
string getDiskName() {
string i = __argv[0];
string result;
int n = i.find('\\');
if (n != -1)
result = i.substr(0, n - 1);
return result;
}
void HideWindow() { //hide main program but will appear subprograms
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
}
bool IsProcessRunAsAdmin() {
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
BOOL b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if (b) {
CheckTokenMembership(nullptr, AdministratorsGroup, &b);
FreeSid(AdministratorsGroup);
}
return b == TRUE;
}
int RunAsAdmin(int ShowCmd) {// (1 == show,0 == hide)
HINSTANCE res;
res = ShellExecute(nullptr, "runas", __argv[0], " ", __argv[0], ShowCmd);
return (int) res;
}
void AutoRun(const string &RegName) { //admin
string temp = R"(REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v )";
const string &autorunName = RegName;
string path = __argv[0];
string cmd = temp + autorunName + " /t REG_SZ /d " + path + " /f";
system(StringToChar(cmd));
}
void pause() {
system("pause");
}
void cls() {
system("cls");
}
char *StringToChar(const string &object) {
char *result = (char *) object.data();
return result;
}
void AddBook() {
cls();
string BookName;
string BookCondition;
string be;
cout << "Enter BookName: " << endl;
cin >> BookName;
cout << "Is this book be borrowed ? (y/n)" << endl;
cin >> be;
if (be == "y" || be == "yes") {
BookCondition = "borrowed";
} else {
BookCondition = "in";
}
cls();
Books.insert(pair<string, string>(BookName, BookCondition));
isBooksChanged = true;
}
void BookList() {
map<string, string>::iterator iterator;
cls();
cout << "===========" << endl;
for (iterator = Books.begin(); iterator != Books.end(); iterator++) {
if (iterator->second == "borrowed")
continue;
cout << iterator->first << endl;
}
cout << "===========" << endl;
}
void BookBorrowed() {
map<string, string>::iterator iterator;
cout << "===========" << endl;
for (iterator = Books.begin(); iterator != Books.end(); iterator++) {
if (!(iterator->second == "borrowed"))
continue;
cout << iterator->first << endl;
}
cout << "===========" << endl;
}
void BorrowBook() {
cls();
string Book;
map<string, string>::iterator iterator;
BookList();
cout << "which Book do you want? " << endl;
cin >> Book;
iterator = Books.find(Book);
if (iterator != Books.end()) {
iterator->second = "borrowed";
isBooksChanged = true;
cout << "Borrow successfully." << endl;
pause();
} else {
cout << "input wrong" << endl;
pause();
}
}
void DeleteBook() {
cls();
BookList();
string Book;
cout << "which book do you want to remove? " << endl;
cin >> Book;
Books.erase(Book);
cls();
BookList();
cout << "remove successfully." << endl;
pause();
}
void ReturnBook() {
cls();
map<string, string>::iterator iterator;
string Book;
cout << endl;
cout << "Here is your borrowed books,which will you return?" << endl;
cout << endl;
BookBorrowed();
cin >> Book;
iterator = Books.find(Book);
if (iterator != Books.end()) {
iterator->second = "in";
isBooksChanged = true;
cout << "Successfully" << endl;
pause();
} else {
cout << "BookCase doesn't have such book." << endl;
pause();
}
}
void UserView() {
cls();
cout << "\t" << "Welcome, " << UserName << "\n";
cout << "\t" << "================================" << endl;
cout << "\t" << "1: AddBook" << "\t" << "2: RemoveBook" << "\n";
cout << "\t" << "3: BorrowBook" << "\t" << "4: ReturnBook" << "\n";
cout << "\t" << "5: SearchBooks" << "\t" << "6: Book List" << "\n";
cout << "\t" << "7: Sittings" << "\t" << "8: exit" << "\n";
cout << "\t" << "================================" << endl;
int Page;
cin >> Page;
switch (Page) {
case 0:
UserView();
break;
case 1:
AddBook();
UserView();
break;
case 2:
DeleteBook();
UserView();
break;
case 3:
BorrowBook();
UserView();
break;
case 4:
ReturnBook();
UserView();
break;
case 5:
SearchBooks();
UserView();
break;
case 6:
BookList();
pause();
UserView();
break;
case 7:
SittingView();
case 8:
HideWindow();
WriteData();
exit(0);
default:
cout << "Doesn't have this command" << endl;
pause();
cls();
UserView();
}
}
void SittingView() {
cls();
int i;
cout << "\t" << "Sittings: " << "\n";
cout << "\t" << "================================" << endl;
cout << "\t" << "1: Change name" << "\t" << "2: Change password" << "\n";
cout << "\t" << "3: Create user" << "\t" << "4: Remove user" << "\n";
cout << "\t" << "5: User List" << "\t" << "6: AutoRun" << "\n";
cout << "\t" << "7: Uninstall" << "\t" << "8: About" << "\n";
cout << "\t" << "================================" << endl;
cout << "\t" << "Press 0 to return" << endl;
cin >> i;
switch (i) {
case 0:
cls();
UserView();
case 1:
ChangeUserName();
SittingView();
break;
case 2:
ChangeUserPassword();
SittingView();
break;
case 3:
CreateUser();
SittingView();
break;
case 4:
RemoveUser();
SittingView();
break;
case 5:
UserList();
pause();
SittingView();
case 6:
cout << "Let Your Bookcase Run when Computer start ? (y/n)" << endl;
char answer;
cin >> answer;
if (answer == 'y') {
AutoRun("BookCase");
} else {
cout << "Okay command successfully canceled" << endl;
}
pause();
SittingView();
break;
case 7:
Uninstall();
break;
case 8:
cls();
AboutAuthor();
break;
default:
cout << "Doesn't have such command" << endl;
pause();
SittingView();
break;
}
}
void AboutAuthor() {
cout << "Author: LongYao" << endl;
cout << "Version: 1.2" << endl;
pause();
cls();
SittingView();
}
void LoginView() {
cls();
if (isFirstBoot) {
cout << "welcome to bookcase, you are the first" << endl;
cout << "your username and password are 'Users'." << endl;
pause();
}
cls();
cout << "Welcome to BookCase." << endl;
cout << "Enter your name and password with enter" << endl;
cout << "Press 'q' to quit." << endl;
string inUserName;
string inPassword;
cin >> inUserName;
if (inUserName == "q")
exit(0);
cin >> inPassword;
map<string, string>::iterator iter;
iter = Users.find(inUserName);
if (iter != Users.end()) {
if (iter->second == inPassword) {
UserName = inUserName;
UserView();
} else {
cout << "Password wrong." << endl;
pause();
LoginView();
}
} else {
cout << "No such account" << endl;
pause();
LoginView();
}
}
void Init() {
HWND hwnd = GetConsoleWindow();
HMENU hmenu = GetSystemMenu(hwnd, false);
RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_MINIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
ShowWindow(hwnd, SW_SHOWNORMAL);
DestroyMenu(hmenu);
ReleaseDC(hwnd, nullptr);
system("title BookCase");
system("color 8b");
system("cls");
LoadData();
}
void LoadData() {
LoadUsers();
LoadBooks();
}
void LoadUsers() {
string name;
string password;
if (isFirstBoot) {
fstream out("Users.dat", ios::out);
out << "Users" << endl;
out << "Users" << endl;
out.close();
isUsersChanged = true;
}
ifstream data("Users.dat", ios::in);
while (getline(data, name) && getline(data, password)) {
Users.insert(pair<string, string>(name, password));
}
data.close();
}
void LoadBooks() {
string name;
string condition;
if (!_access("Books.dat", F_OK)) {
ifstream books("Books.dat", ios::in);
while (getline(books, name) && getline(books, condition)) {//?
Books.insert(pair<string, string>(name, condition));
}
books.close();
} else {
ofstream out("Books.dat", ios::out);
out << "book" << endl;
out << "in" << endl;
out.close();
ifstream books("Books.dat", ios::in);
while (getline(books, name) && getline(books, condition)) {//?
Books.insert(pair<string, string>(name, condition));
}
}
}
void ChangeUserName() {
cls();
do {
string Bname = UserName;
string cname;
string password;
map<string, string>::iterator iterator;
cout << "put in the changed name: " << endl;
cin >> cname;
iterator = Users.find(UserName);
if (iterator != Users.end()) {
password = iterator->second;
} else {
cout << "User wrong." << endl;
break;
}
Users.erase(Bname);
Users.insert(pair<string, string>(cname, password));
isUsersChanged = true;
cout << "Change will take effect in next start" << endl;
pause();
} while (false);
}
void ChangeUserPassword() {
cls();
string CRPassword;
string CHPassword;
map<string, string>::iterator iterator;
do {
iterator = Users.find(UserName);
if (iterator != Users.end()) {
CRPassword = iterator->second;
}
cout << "Please enter your password before you do it: " << endl;
cin >> CHPassword;
if (!(CHPassword == CRPassword)) {
cout << "Password isn't correct" << endl;
pause();
break;
} else {
isUsersChanged = true;
cout << "Okay, Please input the new password: " << endl;
cin >> CHPassword;
iterator->second = CHPassword;
cout << "Change successfully" << endl;
pause();
}
} while (false);
}
void WriteData() {
if (isUsersChanged) {
WriteUsers();
}
if (isBooksChanged) {
WriteBooks();
}
}
void WriteUsers() {
fstream out("Users.dat", ios::out);
map<string, string>::iterator iterator;
for (iterator = Users.begin(); iterator != Users.end(); iterator++) {
out << iterator->first << endl;
out << iterator->second << endl;
}
out.close();
}
void WriteBooks() {
fstream out("Books.dat", ios::out);
map<string, string>::iterator iterator;
for (iterator = Books.begin(); iterator != Books.end(); iterator++) {
out << iterator->first << endl;
out << iterator->second << endl;
}
out.close();
}
int main() {
if (!IsProcessRunAsAdmin()){
RunAsAdmin(1);
exit(0);
}
Init();
LoginView();
}
C++ 实现图书管理系统
最新推荐文章于 2025-05-01 15:11:08 发布