代码使我快乐doge 2021-12-16 22:45 采纳率: 100%
浏览 79
已结题

学生信息管理系统管理员、用户注册登陆模块

使用了map模板定义了map<int,User>user_map;

#include <iostream>
#include<string>
#include <map>
#include <fstream>
#include <algorithm>
#include "User.h"
using namespace std;
map<int,User>user_map;

void user_signin();
int main() {
    cout<<"欢迎使用本系统!"<<endl;
    cout<<"-----------------------------"<<endl;
    cout<<"1.管理员登陆"<<endl;
    cout<<"2.用户登陆"<<endl;
    cout<<"3.用户注册"<<endl;
    cout<<"4.退出"<<endl;
    cout<<"-----------------------------"<<endl;
    cout<<"请输入你的选择:"<<endl;
    int choice;
    cin>>choice;
    switch (choice) {
        case 1:
            break;
        case 2:
            user_signin();
            break;
        case 3:
            break;
        case 4:
            break;
    }
return 0;
}

void user_signin(){
    cout<<"请输入账号:"<<endl;
    User user;
    int i;
    cin>>i;
    user.set_id(i);
    cout<<"请输入密码:"<<endl;
    int j;
    cin>>j;
    user.set_password(j);
    user_map.insert(map<int, User>::value_type(user.getid(),user));
}

这是User.h:

#ifndef MAIN_CPP_USER_H
#define MAIN_CPP_USER_H
#include <iostream>
using namespace std;

class User {
private:
    int id;
    int password;
public:
    User();
    User(int i,int p):id{i},password(p){
    };
    ~User();
    void set_id(int id){
        this->id=id;
    };
    int getid(){
        return id;
    }
    void set_password(int password){
        this->password=password;
    }
    int getpassword(){
        return password;
    }
};


#endif

怎么实现管理员,用户的注册登陆功能,而且要把管理员、用户的id 、password各自保存在txt文件中

  • 写回答

3条回答 默认 最新

  • togolife 2021-12-17 00:40
    关注
    
    #include <iostream>
    #include <string>
    #include <map>
    #include <fstream>
    #include <sstream>
    #include <algorithm>
    #include "User.h"
    using namespace std;
    map<int,User> user_map;
    map<int,User> admin_map;
    void user_signin();
    
    void load_admin()
    {
        ifstream fp("./admin.txt");
        if (!fp.is_open()) {
            return;
        }
        string line;
        while (getline(fp, line)) {
            stringstream ss;
            ss.str(line);
            User user;
            int id;
            string password;
            ss >> id >> password;
            user.set_id(id);
            user.set_password(password);
            admin_map.insert(map<int, User>::value_type(user.getid(),user));
        }
        fp.close();
    }
    
    void load_user()
    {
        ifstream fp("./user.txt");
        if (!fp.is_open()) {
            return;
        }
        string line;
        while (getline(fp, line)) {
            stringstream ss;
            ss.str(line);
            User user;
            int id;
            string password;
            ss >> id >> password;
            user.set_id(id);
            user.set_password(password);
            user_map.insert(map<int, User>::value_type(user.getid(),user));
        }
        fp.close();
    }
    
    void save_admin()
    {
        ofstream fp("./admin.txt");
        if (!fp.is_open()) {
            return;
        }
        for (map<int, User>::iterator it = admin_map.begin(); it != admin_map.end(); ++it) {
            fp << it->second.getid() << " " << it->second.getpassword() << endl;
        }
        fp.close();
    }
    
    void save_user()
    {
        ofstream fp("./user.txt");
        if (!fp.is_open()) {
            return;
        }
        for (map<int, User>::iterator it = user_map.begin(); it != user_map.end(); ++it) {
            fp << it->second.getid() << " " << it->second.getpassword() << endl;
        }
        fp.close();
    }
    
    void admin_login() {
        cout<<"请输入账号:"<<endl;
        int i;
        cin>>i;
        cout<<"请输入密码:"<<endl;
        string password;
        cin>>password;
        map<int, User>::iterator it = admin_map.find(i);
        if (it == admin_map.end()) {
            cout << "管理员账号不存在" << endl;
            return;
        }
        if (it->second.getpassword() != password) {
            cout << "管理员密码不正确" << endl;
            return;
        }
        cout << "管理员登录成功" << endl;
    }
    
    void user_login() {
        cout<<"请输入账号:"<<endl;
        int i;
        cin>>i;
        cout<<"请输入密码:"<<endl;
        string password;
        cin>>password;
        map<int, User>::iterator it = user_map.find(i);
        if (it == user_map.end()) {
            cout << "用户账号不存在" << endl;
            return;
        }
        if (it->second.getpassword() != password) {
            cout << "用户密码不正确" << endl;
            return;
        }
        cout << "用户登录成功" << endl;
    }
    
    void user_signin(){
        cout<<"请输入账号:"<<endl;
        User user;
        int i;
        cin>>i;
        if (user_map.find(i) != user_map.end()) {
            cout << "用户已存在" << endl;
            return;
        }
        user.set_id(i);
        cout<<"请输入密码:"<<endl;
        string j;
        cin>>j;
        user.set_password(j);
        user_map.insert(map<int, User>::value_type(user.getid(),user));
    }
    
    int main() {
        load_admin();
        load_user();
        bool flag = true;
        while (flag) {
            cout<<"欢迎使用本系统!"<<endl;
            cout<<"-----------------------------"<<endl;
            cout<<"1.管理员登陆"<<endl;
            cout<<"2.用户登陆"<<endl;
            cout<<"3.用户注册"<<endl;
            cout<<"4.退出"<<endl;
            cout<<"-----------------------------"<<endl;
            cout<<"请输入你的选择:"<<endl;
            int choice;
            cin>>choice;
            switch (choice) {
                case 1:
                    admin_login();
                    break;
                case 2:
                    user_login();
                    break;
                case 3:
                    user_signin();
                    break;
                case 4:
                    flag = false;
                    break;
            }
        }
        save_admin();
        save_user();
        return 0;
    }
    

    User.h

    #ifndef MAIN_CPP_USER_H
    #define MAIN_CPP_USER_H
    #include <string>
    using namespace std;
    
    class User {
    private:
        int id;
        string password;
    public:
        User(){}
        User(int i,string p):id{i},password(p){
        };
        ~User(){}
        void set_id(int id){
            this->id=id;
        };
        int getid(){
            return id;
        }
        void set_password(string password){
            this->password=password;
        }
        string getpassword(){
            return password;
        }
    };
     
    #endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据