You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.6 KiB
104 lines
2.6 KiB
#include<fstream>
|
|
#include<string>
|
|
#include<string>
|
|
#include<graphics.h>
|
|
|
|
bool CusInputBox(char* input, int length, const char* prompt) {
|
|
std::cout << prompt << std::endl;
|
|
std::cin.getline(input, length);
|
|
return true;
|
|
}
|
|
class Account {
|
|
private:
|
|
int num;
|
|
std::string* username;
|
|
std::string* password;
|
|
public:
|
|
Account() {
|
|
username = NULL;
|
|
password = NULL;
|
|
num = 0;
|
|
}
|
|
int& get_num() {
|
|
return num;
|
|
}
|
|
std::string*& get_username() {
|
|
return username;
|
|
}
|
|
std::string*& get_password() {
|
|
return password;
|
|
}
|
|
// 查找输入是否在用户名队列之中
|
|
int find_username(std::string input) {
|
|
for (int i = 0; i < num; i++) {
|
|
if (input == username[i]) return i;
|
|
}
|
|
return -1; // 若不在用户名队列之中则返回-1
|
|
}
|
|
// 用户名与密码是否对应
|
|
bool is_correct(int i, std::string input) {
|
|
return input == password[i];
|
|
}
|
|
};
|
|
|
|
void init_Account(Account& account) {
|
|
std::fstream filename;
|
|
filename.open("account.txt");
|
|
filename >> account.get_num();
|
|
int n = account.get_num();
|
|
account.get_username() = new std::string[n];
|
|
account.get_password() = new std::string[n];
|
|
for (int i = 0; i < n; i++) {
|
|
filename >> account.get_username()[i];
|
|
filename >> account.get_password()[i];
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_in_2(Account& account) {
|
|
init_Account(account);
|
|
bool is_cancel = false;
|
|
std::string username;
|
|
std::string password;
|
|
int i = 0;
|
|
char temp_username[256];
|
|
char temp_password[256];
|
|
|
|
while (true) {
|
|
bool correct = false;
|
|
is_cancel = !InputBox((LPTSTR) & (temp_username), 10, (LPCTSTR)"请输入用户名:", (LPCTSTR)"登录", (LPCTSTR)"0", 0, 0, 0);
|
|
// is_cancel = !CusInputBox(tempInput, 256, "请输入用户名:");
|
|
if (is_cancel) exit(0);
|
|
else {
|
|
username = temp_username;
|
|
for (i = 0; i < account.get_num(); i++) {
|
|
std::string tempstr = account.get_username()[i];
|
|
// username = (std::string)username;
|
|
if (tempstr == username) {
|
|
correct = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!correct) ::MessageBox(GetHWnd(), (LPCTSTR)"无此用户,请重输", (LPCTSTR)"打开", MB_OK); //提示框
|
|
}
|
|
if (correct) break;
|
|
}
|
|
while (true) {
|
|
bool correct = false;
|
|
is_cancel = !InputBox((LPTSTR) & (temp_password), 10, (LPCTSTR)"请输入密码:", (LPCTSTR)"登录", (LPCTSTR)"0", 0, 0, 0);
|
|
if (is_cancel) exit(0);
|
|
else {
|
|
password = temp_password;
|
|
if (account.get_password()[i] == password) correct = true;
|
|
if (!correct) ::MessageBox(GetHWnd(), (LPCTSTR)"密码错误,请重输", (LPCTSTR)"登录", MB_OK); //提示框
|
|
}
|
|
if (correct) {
|
|
break;
|
|
}
|
|
}
|
|
::MessageBox(GetHWnd(), (LPCTSTR)"登陆成功!", (LPCTSTR)"Hello", MB_OK);
|
|
} |