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;
}
相关推荐
蒟蒻小袁几秒前
力扣面试150题--二叉树的最大深度
算法·leetcode·面试
bj328118 分钟前
树的同构问题--Python
开发语言·python·算法
八股文领域大手子30 分钟前
单机 vs 分布式:Java 后端限流的选择题
java·开发语言·数据结构·算法·spring
小羊在奋斗32 分钟前
基于C++、JsonCpp、Muduo库实现的分布式RPC通信框架
c++·分布式·rpc
wjm04100635 分钟前
C++八股--three day --设计模式之单例和工厂
c++·单例模式·设计模式
纪元A梦1 小时前
华为OD机试真题——告警抑制(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
java·c语言·javascript·c++·python·华为od
钢铁男儿2 小时前
C# 类成员的访问:内部与外部
服务器·开发语言·c#
keep intensify2 小时前
【数据结构】--- 双向链表的增删查改
c语言·数据结构·算法·链表
mahuifa2 小时前
(34)VTK C++开发示例 ---将图片映射到平面
c++·平面·3d·vtk·cmake
薛慕昭2 小时前
《ESP32无线网络编程全攻略:从STA/AP模式到NTP时间同步》
开发语言·单片机·嵌入式硬件