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;
}
相关推荐
青少儿编程课堂36 分钟前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
旖-旎1 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰1 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx1 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer1 小时前
【无标题】
开发语言·c++·算法
昇腾CANN1 小时前
TileLang-Ascend 算子性能优化方法与实操
开发语言·javascript·性能优化·昇腾·cann
John_ToDebug1 小时前
WebHostView 与 TabStrip 交互机制深度解析
c++·chrome·windows
AGV算法笔记2 小时前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉
沐知全栈开发2 小时前
ionic 手势事件详解
开发语言
lsx2024062 小时前
Bootstrap 按钮
开发语言