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;
}
相关推荐
寻星探路39 分钟前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024062 小时前
Bootstrap 警告框
开发语言
2601_949146533 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
你撅嘴真丑3 小时前
第九章-数字三角形
算法
曹牧3 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
在路上看风景3 小时前
19. 成员初始化列表和初始化对象
c++
KYGALYX3 小时前
服务异步通信
开发语言·后端·微服务·ruby
uesowys3 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
zmzb01033 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder3 小时前
hot100-二叉树I
数据结构·python·算法·二叉树