1.思维导图
2.
3.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 用户结构体
struct User {
string username;
string password;
};
// 全局用户列表
vector<User> users;
// 注册函数
void registerUser() {
string username, password;
cout << "=== 注册 ===" << endl;
cout << "请输入用户名: ";
cin >> username;
// 检查用户名是否已存在
auto it = find_if(users.begin(), users.end(),
[&username](const User& u) { return u.username == username; });
if (it != users.end()) {
cout << "用户名已存在,请选择其他用户名!" << endl;
return;
}
cout << "请输入密码: ";
cin >> password;
// 创建新用户并添加到vector
users.push_back({username, password});
cout << "注册成功!" << endl;
}
// 登录函数
void loginUser() {
string username, password;
cout << "=== 登录 ===" << endl;
cout << "请输入用户名: ";
cin >> username;
cout << "请输入密码: ";
cin >> password;
// 查找用户
auto it = find_if(users.begin(), users.end(),
[&username, &password](const User& u) {
return u.username == username && u.password == password;
});
if (it != users.end()) {
cout << "登录成功!欢迎," << username << "!" << endl;
} else {
cout << "用户名或密码错误!" << endl;
}
}
// 主菜单
void showMenu() {
cout << "\n=== 欢迎使用本地注册登录系统 ===" << endl;
cout << "1. 注册" << endl;
cout << "2. 登录" << endl;
cout << "3. 退出" << endl;
cout << "请选择操作: ";
}
int main() {
int choice;
while (true) {
showMenu();
cin >> choice;
switch (choice) {
case 1:
registerUser();
break;
case 2:
loginUser();
break;
case 3:
cout << "感谢使用,再见!" << endl;
return 0;
default:
cout << "无效选择,请重新输入!" << endl;
}
}
return 0;
}