#include
#include
#include
#include
using namespace std;
#define FILENAME "Account.txt"
#define SIZE 100 //最大容量
map
class User {
public:
//显示菜单
void ShowMenu() {
cout << "欢迎使用账号管理系统" << endl;
cout << "1.注册" << endl;
cout << "2.登录" << endl;
cout << "0.退出" << endl;
}
//注册账号
void Register(User *user) {
//读取已注册账号记录
user->Read();
int i_acc;
int i_psw;
int i_psw02; //用于校验两次密码是否一致
string str_acc;
string str_psw;
string str_psw02;
for (int i = arr_user.size(); i < SIZE; i++) {
FLAG01:
cout << "请输入账号:";
cin >> str_acc;
//检测是否含特殊字符
for (int i = 0; i < str_acc.size(); i++) {
if (str_acc[i] >= 48 && str_acc[i] <= 57) {
continue;
}
else {
cout << "使用非法字符,请重试" << endl;
goto FLAG01;
break;
}
}
i_acc = atoi(str_acc.c_str());
//查找是否已注册
auto pos_acc = arr_user.find(i_acc);
if (pos_acc != arr_user.end()) {
cout << "已存在该账号,请返回登陆" << endl;
system("pause");
system("cls");
return;
}
FLAG02:
cout << "请输入密码:";
cin >> str_psw;
//检测是否含特殊字符
for (int i = 0; i < str_psw.size(); i++) {
if (str_psw[i] >= 48 && str_psw[i] <= 57) {
continue;
}
else {
cout << "使用非法字符,请重试" << endl;
goto FLAG02;
break;
}
}
i_psw = atoi(str_psw.c_str());
//注册密码 二次确认
cout << "请再次输入密码:";
cin >> str_psw02;
//检测是否含特殊字符
for (int i = 0; i < str_psw02.size(); i++) {
if (str_psw02[i] >= 48 && str_psw02[i] <= 57) {
continue;
}
else {
cout << "使用非法字符,请重试" << endl;
goto FLAG02;
break;
}
}
i_psw02 = atoi(str_psw02.c_str());
if (i_psw != i_psw02) {
cout << "两次密码不一致,请重新输入" << endl;
goto FLAG02;
}
//插入map
arr_user.insert(make_pair(i_acc, i_psw));
//保存文件
user->Save();
cout << "注册成功,请返回登陆" << endl;
system("pause");
system("cls");
break;
}
}
//读取数据
void Read() {
ifstream fin;
fin.open(FILENAME, ios::in);
if (!fin.is_open()) {
//cout<<"记录为空";
return;
}
int i_acc;
int i_psw;
auto it = arr_user.begin();
//存入map
while (!fin.eof()) {
fin >> i_acc;
fin >> i_psw;
arr_user.insert(make_pair(i_acc, i_psw));
}
fin.close();
}
//存储数据
void Save() {
ofstream fout;
fout.open(FILENAME, ios::out);
for (auto it = arr_user.begin(); it != arr_user.end(); it++) {
fout << it->first << " " << it->second << endl;
}
fout.close();
}
//登陆账号
void Login(User* user) {
user->Read();
//判断是否已有注册账号
ifstream fin;
char ch;
fin.open(FILENAME, ios::in);
fin >> ch;
if (fin.eof()) {
//cout << "文件为空" << endl;
cout << "无账号记录" << endl;
system("cls");
return;
}
int i_acc;
int i_psw;
string str_acc;
string str_psw;
//输入账号
FLAG03:
cout << "请输入账号:";
cin >> str_acc;
//检测是否含特殊字符
for (int i = 0; i < str_acc.size(); i++) {
if (str_acc[i] >= 48 && str_acc[i] <= 57) {
continue;
}
else {
cout << "使用非法字符,请重试" << endl;
goto FLAG03;
break;
}
}
i_acc = atoi(str_acc.c_str());
//查找是否存在该账号
auto pos_acc = arr_user.find(i_acc);
if (pos_acc == arr_user.end()) {
cout << "该账号未注册" << endl;
goto FLAG03;
}
//输入密码
FLAG04:
cout << "请输入密码:";
cin >> str_psw;
//检测是否含特殊字符
for (int i = 0; i < str_psw.size(); i++) {
if (str_psw[i] >= 48 && str_psw[i] <= 57) {
continue;
}
else {
cout << "使用非法字符,请重试" << endl;
goto FLAG04;
break;
}
}
i_psw = atoi(str_psw.c_str());
//匹配账号密码
if (pos_acc->second == i_psw) {
cout << "登陆成功" << endl;
}
else {
cout << "密码错误,请重新登录" << endl;
goto FLAG03;
}
system("pause");
system("cls");
}
};
int main() {
User user;
int choice = 0;
while (true) {
//菜单调用
user.ShowMenu();
cin >> choice;
switch (choice) {
case 1://注册
user.Register(&user);
break;
case 2://登录
user.Login(&user);
break;
case 0://退出
exit(0);
break;
default:
cout << "输入有误,请重新输入" << endl;
break;
}
}
system("pause");
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)