当前位置: 首页 > news >正文

C++(类模板的运用)

 

使用vector实现一个简单的本地注册登录系统 注册:将账号密码存入vector里面,注意防重复判断 登录:判断登录的账号密码是否正确 

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

struct usr {
    string pin;
    string psw;
};

vector<usr> readUsers(const string& filename) {
    vector<usr> users;
    ifstream infile(filename);
    string pin, psw;
    while (infile >> pin >> psw) {
        users.push_back({pin, psw});
    }
    infile.close();
    return users;
}

bool isDuplicate(const vector<usr>& users, const string& pin) {
    for (const auto& u : users) {
        if (u.pin == pin) {
            return true;
        }
    }
    return false;
}

bool loginValid(const vector<usr>& users, const string& pin, const string& psw) {
    for (const auto& u : users) {
        if (u.pin == pin && u.psw == psw) {
            return true;
        }
    }
    return false;
}

void writeUser(const string& filename, const usr& user) {
    ofstream outfile(filename, ios::app); // 追加写入
    outfile << user.pin << " " << user.psw << endl;
    outfile.close();
}

int main() {
    const string filename = "1.txt";

    while (true) {
        cout << "选择操作:" << endl;
        cout << "1. 注册" << endl;
        cout << "2. 登录" << endl;
        cout << "3. 退出" << endl;

        int choice;
        cin >> choice;

        if (choice == 1) {
            usr user;
            cout << "请输入账号和密码:" << endl;
            cin >> user.pin >> user.psw;

            vector<usr> users = readUsers(filename);
            if (isDuplicate(users, user.pin)) {
                cout << "账号已存在" << endl;
            } else {
                writeUser(filename, user);
                cout << "注册成功" << endl;
            }
        }
        else if (choice == 2) {
            string pin, psw;
            cout << "请输入账号和密码:" << endl;
            cin >> pin >> psw;

            vector<usr> users = readUsers(filename);
            if (loginValid(users, pin, psw)) {
                cout << "登录成功" << pin << endl;
            } else {
                cout << "登录失败" << endl;
            }
        }
        else if (choice == 3) {
            cout << "已退出" << endl;
            break;
        }
        else {
            cout << "请输入正确选项" << endl;
        }

        cout << endl;
    }

    return 0;
}

相关文章:

  • ConfigurationProperties和PropertySource两个注解的区别。
  • 案例分享(七):实现Apache-sharding-proxy的监控
  • 【redis】简介及在springboot中的使用
  • 学习比较JVM篇(六):解读GC日志
  • [ctfshow web入门] web16
  • 离散数学问题集--问题5.9
  • 【UnityEditor扩展】如何在 Unity 中创建棱柱体(用作VR安全区检测),同时在编辑器插件中实现与撤销/恢复功能
  • flink Shuffle的总结
  • [ctfshow web入门] web19
  • 第四讲:类与对象(下)
  • 如何在React中集成 PDF.js?构建支持打印下载的PDF阅读器详解
  • mapbox基础,加载栅格图片到地图
  • QMT实盘代码案例教学:etf全球配置策略
  • 深入理解Java性能调优与JVM底层机制
  • 柯尼卡美能达CA-410-CA-VP427 P427
  • 从 “单打独斗” 到 “生态共赢” 跨境货源池的协同增长逻辑
  • 摄像头模块对焦方式的类型
  • Java基础 4.7
  • 基于Python的二手房数据挖掘与可视化深度分析
  • STM32单片机入门学习——第22节: [7-2] AD单通道AD多通道
  • 海口做网站公司/百度客户端
  • 公安局 网站备案/线上推广网络公司
  • 数据库 搭建 网站/优化模型数学建模
  • 泰安正规网站建设公司电话/cms网站
  • 介绍自己的做的网站/百度官网app
  • 建设网站的基本步骤/成都最新消息今天