C++---day7

复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Stu {
private:

public:

};

// 自定义 vector 类,重载 << 运算符方便添加元素
template <class T>
class myVector : public vector<T> {
public:
    myVector& operator<<(const T& val) {
        this->push_back(val);
        return *this;
    }
};
                                                                                                                                
// 选择登录还是注册功能
int choose() {
    int num;
    cout << "请选择登录或注册功能(登录输入 1,注册输入 2,退出输入 3)" << endl;
    cin >> num;
    return num;
}

// 注册函数
bool registerAccount(myVector<string>& accounts, const string& username, const string& password) {
    for (size_t i = 0; i < accounts.size(); i += 2) {
        if (accounts[i] == username) {
            cout << "该账号已存在,请重新选择操作!" << endl;
            return false;
        }
    }
    accounts << username << password;
    cout << "注册成功!" << endl;
    return true;
}

// 登录函数
bool loginAccount(const myVector<string>& accounts, const string& username, const string& password) {
    for (size_t i = 0; i < accounts.size(); i += 2) {
        if (accounts[i] == username && accounts[i + 1] == password) {
            cout << "登录成功!" << endl;
            return true;
        }
    }
    cout << "账号或密码错误,请重新选择操作!" << endl;
    return false;
}

int main(int argc, const char** argv) {
    myVector<string> accounts;
    int choice;
    string username, password;

    while (true) {
        choice = choose();
        switch (choice) {
            case 1: {
                cout << "请输入用户名:";
                cin >> username;
                cout << "请输入密码:";
                cin >> password;
                loginAccount(accounts, username, password);
                break;
            }
            case 2: {
                cout << "请输入用户名:";
                cin >> username;
                cout << "请输入密码:";
                cin >> password;
                registerAccount(accounts, username, password);
                break;
            }
            case 3:
                cout << "退出系统!" << endl;
                return 0;
            default:
                cout << "无效的选择,请重新输入!" << endl;
        }
    }

    return 0;
}
相关推荐
pumpkin845141 小时前
Rust Mock 工具
开发语言·rust
love530love1 小时前
【笔记】在 MSYS2(MINGW64)中安装 python-maturin 的记录
运维·开发语言·人工智能·windows·笔记·python
阿卡蒂奥2 小时前
C# 结合PaddleOCRSharp搭建Http网络服务
开发语言·http·c#
kingmax542120083 小时前
【洛谷P9303题解】AC- [CCC 2023 J5] CCC Word Hunt
数据结构·c++·算法·广度优先
白熊1883 小时前
【机器学习基础】机器学习入门核心算法:XGBoost 和 LightGBM
人工智能·算法·机器学习
bai_lan_ya4 小时前
数据结构-排序-排序的七种算法(2)
数据结构·算法·排序算法
泉飒4 小时前
lua注意事项
开发语言·笔记·lua
hao_wujing5 小时前
使用逆强化学习对网络攻击者的行为偏好进行建模
开发语言·网络·php
还是鼠鼠5 小时前
单元测试-概述&入门
java·开发语言·后端·单元测试·log4j·maven
全域智图5 小时前
元胞自动机(Cellular Automata, CA)
人工智能·算法·机器学习