#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include"User.h"
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
static int new_id;
const void print_admin(User &p);
User add_new_user();
const int get_currentID(){return User::currentID;}

int main(int argc, char** argv) {
    vector<User > stu;
    string order;
    cout<<"目前还没有用户    您可以选择"<<endl<<"A.注册新用户      Q.退出程序"<<endl;
    int n1=0;
    while(1)
    {
        if(n1)
        {
            cout<<"欢迎使用本系统。本系统已有"<<get_currentID()-999<<"名用户。您可以选择"<<endl; 
            cout<<"A.注册新用户    L.已有用户登录     Q.退出程序"<<endl;
            cout<<"G.提供管理员权限以获取最新注册用户的信息"<<endl;
        }
        cin>>order;
        if(order=="Q"){break;}    //退出程序 
        else if(order=="G"&&n1!=0)
        {
            cout<<"权限已获取  数据打印中";cout<<". ";_sleep(1*1000);cout<<". ";_sleep(1*1000);cout<<". "<<endl;
            print_admin(stu[get_currentID()-1000]);
        }
        else if(order=="L"&&n1!=0)  //防止有人在没有用户时就尝试登录 
        {                                         //登录系统由此开始 
            while(1)
            {
                cout<<"请输入您的用户ID"<<endl;
                int n2;
                cin>>n2;
                n2-=1000;
                if(n2>=0&&n2<(get_currentID()-999))              //由此开始 与密码相关 
                {
                    cout<<"请输入您的用户密码"<<endl;
                    int n3=0;
                    for(int a1=3;a1>0;a1--)
                    {
                        string c_pass;
                        cin>>c_pass;
                        if(c_pass==(stu[n2].get_password()))
                        {
                            break;
                        }
                        else{cout<<"密码错误,请重新输入!"<<endl;n3++;}
                    }
                    if(n3==3)
                    {
                        cout<<"密码错误次数过多,请稍后再试!"<<endl;
                        break;
                    }
                    else
                    {
                        cout<<"登录成功!欢迎使用,";
                        stu[n2].print("name");
                        int quit=0;
                        while(1) 
                        {
                            cout<<"您可以进行"<<endl<<"P.打印您的资料   C.修改密码    Q.退出登录"<<endl;
                            string order3;
                            cin>>order3;
                            if(order3=="Q")
                            {
                                quit=1;cout<<endl<<endl;break;
                            }
                            else if(order3=="C")               //修改密码由此开始 
                            {
                                cout<<"请输入旧密码"<<endl;
                                string old_pass;
                                int x3=0;
                                for(int a1=3;a1>0;a1--)
                                {
                                    string c_pass;
                                    cin>>c_pass;
                                    if(c_pass==(stu[n2].get_password()))
                                    {
                                        break;
                                    }
                                    else{cout<<"密码错误!"<<endl;x3++;}
                                }
                                    if(x3==3)
                                    {
                                        cout<<"密码错误次数过多,请稍后再试!"<<endl;
                                        continue;
                                    }
                                    else
                                    {
                                        cout<<"请输入新密码"<<endl;
                                        string change_new;
                                        cin>>change_new;
                                        stu[n2].change_password(change_new);
                                        cout<<"请确认您的新密码"<<endl;
                                        stu[n2].print("password");
                                        _sleep(1*1000);
                                    }
                            }                       //修改密码到此结束
                            else if(order3=="P")
                            {
                                cout<<"请输入打印内容"<<endl<<"id / name / all / Q.退出打印模式"<<endl;
                                string print_what;
                                while(1)
                                {
                                    cin>>print_what;
                                    if(print_what=="Q")
                                    {
                                        break;
                                    }  
                                    stu[n2].print_mode(print_what);
                                }
                            }
                             
                             
                        }
                        if(quit)
                        {
                            break;
                        }
                    
                    }
                                                        //由此结束 与密码相关 
                } 
                else
                {
                    cout<<"不存在此用户!"<<endl<<"R.重新输入       Q.退出登录"<<endl;
                    string order1;
                    cin>>order1;
                    if(order1=="R"){continue;}
                    else if(order1=="Q"){break;}
                }
            }
        }                                                 //登录系统到此结束 
        else if(order=="A")                                            //注册系统由此开始 
        {
            stu.push_back(add_new_user());
            cout<<"添加成功!"<<endl; _sleep(1*1000);
            cout<<"您的用户ID是";
            stu[new_id-1000].print("id");
            cout<<endl<<endl<<endl;
        }  _sleep(1.5*1000);                                           //注册系统由此结束 
        n1=1;
    }
    return 0;
}

User add_new_user()
{
    string new_name,new_password;
    cout<<"请设置您的用户名"<<endl;
    cin>>new_name;
    cout<<"请设置密码"<<endl;
    cin>>new_password;
    User newguy(new_name,new_password);
    new_id=newguy.getID();
    return newguy;    
}
const void print_admin(User &p) 
{
    string pass_once;
    int leng;
    pass_once=p.password;
    cout<<"用户id "<<p.id<<endl;
    cout<<"用户名 "<<p.name<<endl;
    cout<<"用户密码 ";
    for(leng=pass_once.length();leng>0;leng--)
    {
        cout<<"*";
     } 
    cout<<endl<<endl<<endl;
    _sleep(2*1000);
}
main
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;

class User
{
    public:
        User(string name1);
        User(string name1,string password1);
        int print(string s1);
        void print_mode(string s1);   //应付最后的打印,比上面一行的多一些内容  
        void change_password(string new_password){password=new_password;}
        const int getID();
        const string get_password();
        friend const void print_admin(User &p);  //应付"管理员权限"
        friend const int get_currentID();
    private:
        static int currentID;
        int id;
        string name;
        string password;
};
User.h
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include"User.h"
using namespace std;

User::User(string name1):id(++currentID),name(name1),password("111111"){
}
User::User(string name1,string password1):id(++currentID),name(name1),password(password1){
}

int User::currentID=999;
int User::print(string s1)
{
    if(s1=="id"){cout<<id<<endl;return 0;}
    else if(s1=="name"){cout<<name<<endl;return 0;}
    else if(s1=="password"){cout<<password<<endl;return 0;}
    else if(s1=="currentID"){cout<<currentID<<endl;return 0;}
    else if(s1=="all"){cout<<id<<endl<<name<<endl<<password<<endl;return 0;}
    else {cout<<"程序错误 请重新输入";return 1;}  
}
void User::print_mode(string s1)
{
    if(s1=="id"){cout<<"您的用户id是 "<<id<<endl;}
    else if(s1=="name"){cout<<"您的用户名是 "<<name<<endl;}
    //else if(s1=="password"){cout<<"您的密码是 "<<password<<endl;}
    else if(s1=="currentID"){cout<<currentID<<endl;}
    else if(s1=="all")
    {
        cout<<"您的用户id是 "<<id<<endl;
        cout<<"您的用户名是 "<<name<<endl;
        //cout<<"您的密码是 "<<password<<endl;
    }
    else {cout<<"程序错误 请重新输入";}  
}

const int User::getID()
{
    return id;
}



const string User::get_password()
{
    return User::password;
}
User.cpp

_(:з」∠)_我太菜了 花了大概2h才写出来。下面演示一下大概功能。

进入程序的时候,还没有用户存在。

我们先借用田所学长和远野学弟的身份注册两个用户

 

然后迫不及待的拿起管理员身份,看看最新一名注册用户的信息。密码是隐私,看不到的。

 

现在让我们用远野学弟的账号登录。。等等 密码是什么来的输错太多,被踢出来了

因为丢人博主能力有限,如果在这里输入用户ID(int)的地方输入了用户名(char),程序就会崩溃。。。

 

登录成功。我们可以按P确认自己的资料。

 

 

原本的密码太难记了,我们还是改掉它吧

鉴于这个丢人博主做不出太多功能,我们再登录一下田所前辈的账号就退出吧。

 

么得了。

 

posted on 2018-05-09 16:25  ikazuchi  阅读(242)  评论(2编辑  收藏  举报