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;
}
相关推荐
王的宝库12 小时前
GO常用标准库包
开发语言·后端·golang
geovindu12 小时前
python: Face Recognition
开发语言·后端·python·人脸识别
不会就选b12 小时前
数据结构之树&&二叉树(二)
数据结构·算法
平头哥技术团队12 小时前
Day 13 | 调 line-height 和 margin:三处间距让名片页脱离模板感
开发语言·前端·javascript·学习·html5
黑马程序员毕设13 小时前
基于Java的仪器管理系统设计与实现
java·开发语言·spring boot·后端·微信小程序
平头哥AI13 小时前
Day 13 | 一个函数回两个值:Go 的 error 是从哪冒出来的
开发语言·后端·golang
渡之13 小时前
ArduPilot (APM)滤波器之SlewLimiter 深度解析
算法
asdzx6713 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
zander25813 小时前
LeetCode 1143. 最长公共子序列
开发语言·python·算法
Sumerking13 小时前
llc_control.c 专项评审(v3 更新版)
c语言·开发语言·算法·obc