如何用c++模拟创建用户(只得创建一个用户)

主要功能为:

1、输入密码,进入界面

2、创建用户

3、输入用户的账号与密码

4、在用户里可以修改账号与密码

下一期准备进行模拟安装程序。

以下是目前代码:

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include<stdio.h>
#include <limits>
#include <cstdlib>
#include <cmath>
#define MAX 50
using namespace std;

void sssm();
void show_results();
float result_arr[2] = { 0 };
int arr_index = 0;

void jisuanqi()
{
    float num1 = 0, num2 = 0, final;
    char fh = 0;
    char select = 0;
    cout << "欢迎使用简易计算器" << endl;
    cout << "-----------------" << endl;
    cout << "|    1、进入    |" << endl;
    cout << "-----------------" << endl;
    cout << "|    2、说明    |" << endl;
    cout << "-----------------" << endl;
    cin >> select;
    if (select == '1') {
        system("cls");
        cout << "请输入" << endl;
        sssm();
        goto Z;
    }
    else
        sssm();
Z:cin >> num1;
    while (true) {
        if (cin.fail()) {
            cin.clear(); // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
            cout << "输入无效,请重新输入一个数字:" << endl;
            cin >> num1;
        }
        else {
            break;
        }
    }
    cin >> fh;
    int a = 0;
    double result;
    int final2;
    while (fh != 'n')
    {
        switch (fh)
        {
        case'+':cin >> num2;
            while (true) {
                if (cin.fail()) {
                    cin.clear(); // 清除错误标志
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
                    cout << "输入无效,请重新输入一个数字:" << endl;
                    cin >> num2;
                }
                else {
                    break;
                }
            }final = num1 + num2;
            break;
        case'-':cin >> num2;
            while (true) {
                if (cin.fail()) {
                    cin.clear(); // 清除错误标志
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
                    cout << "输入无效,请重新输入一个数字:" << endl;
                    cin >> num2;
                }
                else {
                    break;
                }
            }final = num1 - num2; break;
        case'*':cin >> num2;
            while (true) {
                if (cin.fail()) {
                    cin.clear(); // 清除错误标志
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
                    cout << "输入无效,请重新输入一个数字:" << endl;
                    cin >> num2;
                }
                else {
                    break;
                }
            }
            final = num1 * num2;
            break;
        case'/':cin >> num2;
            while (true) {
                if (cin.fail()) {
                    cin.clear(); // 清除错误标志
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
                    cout << "输入无效,请重新输入一个数字:" << endl;
                    cin >> num2;
                }
                else {
                    break;
                }
            }
            final = num1 / num2;
            a = num1 - (num2 * static_cast<int>(num1 / num2));
            break;
        case'^':final = num1 * num1; break;
        case'~':cin >> num2;
            while (true) {
                if (cin.fail()) {
                    cin.clear(); // 清除错误标志
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
                    cout << "输入无效,请重新输入一个数字:" << endl;
                    cin >> num2;
                }
                else {
                    break;
                }
            }
            final = num1;
            for (int i = num2; i > 1; i--)
            {
                final = num1 * final;
            }
            break;
        case'c': show_results(); cout << "请输入是否要调用上一次结果(y/n):" << endl; cin >> select; if (select == 'y')final = result_arr[1]; break; // 查看之前的结果
        case'h':if (num1 < 0)cout << "负数没有平方根!" << endl; else  final = sqrt(num1); break;
        case'z':final2 = final; final = final2; break;
        default:cout << "请重新输入" << endl; fh = 0; break;
        }
        system("cls");
        if (a != 0)
        {
            int final1 = final;
            printf("--------------------\n");
            printf("结果是%d····%d\n", final1, a);
            printf("--------------------\n");
            sssm();
            final = final1;
            a = 0;
            result_arr[arr_index % 2] = final; // 存放结果到数组中
            arr_index++;
        }
        else
        {
            printf("---------------------\n");
            printf("结果是%.2f\n", final);
            printf("---------------------\n");
            sssm();
            result_arr[arr_index % 2] = final; // 存放结果到数组中
            arr_index++;
        }
    a:        cin >> fh;
        if (fh == 'q')
        {
            printf("已清空\n");
            goto Z;
        }
        if (fh == 't')
        {
            return;
        }
        else
        {
            num1 = final;
            continue;
        }
    }
}

void sssm() {
    cout << "-----------------------------------" << endl;
    cout << "|    1、+加法    |    2、-减法    |" << endl;
    cout << "-----------------------------------" << endl;
    cout << "|    3、*乘法    |    4、/除法    |" << endl;
    cout << "-----------------------------------" << endl;
    cout << "|    5、^平方    |    6、h开方    |" << endl;
    cout << "-----------------------------------" << endl;
    cout << "|    7、c是查看上一次结果         |" << endl;
    cout << "-----------------------------------" << endl;
    cout << "|    8、z是去掉小数点后面的数字   |" << endl;
    cout << "-----------------------------------" << endl;
    cout << "|    9、q清空    |    10、t退出   |" << endl;
    cout << "-----------------------------------" << endl;

}
void show_results() {
    cout << "上一次计算结果是: " << result_arr[1] << endl;
    system("pause");
}
bool checkPassword1(const std::string& password1) {
    // 检查密码是否符合要求,这里只作简单示例
    if (password1.length() < 6) {
        return false;
    }
    // 还可以进行其他复杂的密码检查规则
    return true;
}

void savePassword1(const std::string& password1) {
    std::ofstream file("password1.txt");
    if (file.is_open()) {
        file << password1;
        file.close();
        std::cout << "密码保存成功!" << std::endl;
    }
    else {
        std::cout << "密码保存失败!" << std::endl;
    }
}

std::string readPassword1() {
    std::string password1;
    std::ifstream file("password1.txt");
    if (file.is_open()) {
        file >> password1;
        file.close();
    }
    return password1;
}
bool checkPassword(const std::string& password) {
    // 检查密码是否符合要求,这里只作简单示例
    if (password.length() < 6) {
        return false;
    }
    
    // 还可以进行其他复杂的密码检查规则
    return true;
}

void savePassword(const std::string& password) {
    std::ofstream file("password.txt");
    if (file.is_open()) {
        file << password;
        file.close();
        std::cout << "密码保存成功!" << std::endl;
    }
    else {
        std::cout << "密码保存失败!" << std::endl;
    }
}

std::string readPassword() {
    std::string password;
    std::ifstream file("password.txt");
    if (file.is_open()) {
        file >> password;
        file.close();
    }
    return password;
}
bool checkUser(const std::string& User) {
    // 检查密码是否符合要求,这里只作简单示例
    if (User.length() < 1) {
        return false;
    }
    if (User.size() < 0)
    {
        return 0;
    }
    // 还可以进行其他复杂的密码检查规则
    return true;
}

void saveUser(const std::string& User) {
    std::ofstream file("User.txt");
    if (file.is_open()) {
        file << User;
        file.close();
        std::cout << "用户保存成功!" << std::endl;
    }
    else {
        std::cout << "用户保存失败!" << std::endl;
    }
}

std::string readUser() {
    std::string User;
    std::ifstream file("User.txt");
    if (file.is_open()) {
        file >> User;
        file.close();
    }
    return User;
}

int main() {
    int a11 = 3;
    int a10 = 3;
    char c;
A100:    std::string currentPassword;
    std::string newPassword;
    
    // 读取以前保存的密码
    std::string savedPassword = readPassword();

    // 如果没有保存的密码,要求设置新密码
    if (savedPassword.empty()) {
        std::cout << "欢迎首次使用,请设置新密码:";
        std::cin >> newPassword;
    }
    else {
        // 获取当前密码
        std::cout << "请输入当前密码:";
        string currentPassword;
        char ch;

        // 隐藏密码输入
        while ((ch = _getch()) != '\r') {
            if (ch == '\b') {
                if (currentPassword.length() > 0) {
                    currentPassword.pop_back();
                    cout << "\b \b";
                }
            }
            else {
                currentPassword += ch;
                cout << "*";
            }
        }

        // 验证当前密码
        if (currentPassword != savedPassword) {

            a10--;
            while (a10 > 0)
            {
                printf("\n\n还有%d次机会\n", a10);
                std::cout << "提醒:" << savedPassword[0] << std::endl;
                goto A100;
            }
            std::cout << "密码错误,无法进入程序!" << std::endl;
            return 0;
        }
    }
    int a = 0;
    std::string currentPassword1;
    std::string newPassword1;

    // 读取以前保存的密码
    std::string savedPassword1 = readPassword1();
    std::string currentUser;
    std::string newUser;
    // 读取以前保存的用户信息
    std::string savedUser = readUser();
    std::cout << endl;
b:    std::cout << "请选择1-创建用户 2-打开用户 3-修改密码 4-查看用户名"<<endl;
    std::cin >> a;
    if (a == 1)
    {
        cout << "欢迎首次使用,请设置新用户信息:" << endl;
        cout << "请输入用户名:";

        std::cin >> newUser; // 读取新用户名
        cout << "请输入密码:";
        std::cin >> newPassword1; // 读取新密码
        savePassword1(newPassword1);saveUser(newUser);
        goto b;
    }
    else
        if (a == 2)
        {
            // 获取当前用户信息
            cout << "请输入当前用户名:";
            string currentUser;

            char ch;

            // 隐藏密码输入
            while ((ch = _getch()) != '\r') {
                if (ch == '\b') {
                    if (currentUser.length() > 0) {
                        currentUser.pop_back();
                        cout << "\b \b";
                    }
                }
                else {
                    currentUser += ch;
                    cout << "*";
                }
            } // 读取当前用户名
            cout << endl;
        a:            cout << "请输入当前密码:";
            string currentPassword1;
            

            // 隐藏密码输入
            while ((ch = _getch()) != '\r') {
                if (ch == '\b') {
                    if (currentPassword1.length() > 0) {
                        currentPassword1.pop_back();
                        cout << "\b \b";
                    }
                }
                else {
                    currentPassword1 += ch;
                    cout << "*";
                }
            }
            cout << endl;
            // 读取当前密码
            std::string savedPassword1 = readPassword1();
            // 验证当前用户信息
            if (currentUser != savedUser) {
                cout << "用户名错误,无法进行修改!" << endl;
                goto b;
            }
            if (currentPassword1 != savedPassword1) {

                a11--;

                while (a11 > 0)
                {
                    if (a11 == 1)
                    {
                        std::cout << "1-忘记密码 2-退出";
                        int a1;
                        std::cin >> a1;
                        if (a1 == 1)
                        {
                            std::cout << "请输入用户名";
                            std::cin >> currentUser;
                            if (currentUser == savedUser)
                            {
                                std::cout << savedPassword1;
                            }
                            return 0;
                        }
                    }

                    printf("\n\n还有%d次机会\n", a11);
                    std::cout << "提醒:" << savedPassword1[0] << std::endl;
                    goto a;
                }
                std::cout << "密码错误,无法进入程序!" << std::endl;
                return 0;
            }

            else {
                std::cout << "登录成功";
                int b = 0;
            c:                std::cout << "1-修改用户名 2-修改密码 3-打开应用";
                std::cin >> b;
                if (b == 1)
                {
                    // 获取当前用户
                    std::cout << "请输入当前用户名:";
                    std::cin >> currentUser;

                    // 验证当前密码
                    if (currentUser != savedUser) {
                        std::cout << "用户名错误,无法进行修改!" << std::endl;
                        return 0;
                    }

                    // 获取新密码
                    std::cout << "请输入新用户名:";
                    std::cin >> newUser;
                    // 验证新密码
                    if (!checkUser(newUser)) {
                        std::cout << "新用户名不符合要求,修改失败!" << std::endl;
                        return 0;
                    }

                    // 保存密码
                    saveUser(newUser);

                    // 修改密码成功
                    std::cout << "用户名修改成功!" << std::endl;
                    goto c;
                }
                else
                    if (b == 2)
                    {
                        std::string currentPassword1;
                        std::string newPassword1;

                        // 读取以前保存的密码
                        std::string savedPassword1 = readPassword1();

                        // 如果没有保存的密码,要求设置新密码
                        if (savedPassword1.empty()) {
                            std::cout << "欢迎首次使用,请设置新密码:";
                            std::cin >> newPassword1;
                        }
                        else {
                            // 获取当前密码
                            std::cout << "请输入当前密码:";
                            std::cin >> currentPassword1;

                            // 验证当前密码
                            if (currentPassword1 != savedPassword1) {
                                std::cout << "密码错误,无法进行修改!" << std::endl;
                                return 0;
                            }

                            // 获取新密码
                            std::cout << "请输入新密码:";
                            std::cin >> newPassword1;
                        }

                        // 验证新密码
                        if (!checkPassword1(newPassword1)) {
                            std::cout << "新密码不符合要求,修改失败!" << std::endl;
                            return 0;
                        }

                        // 保存密码
                        savePassword1(newPassword1);

                        // 修改密码成功
                        std::cout << "密码修改成功!" << std::endl;
                        goto c;

                    }
                    else
                        if (b == 3)
                        {
                            int a=0, b=0, c=0, d=0;
                            char a1 = 0;
                            int arr[10];
                            int i, j, t;
                            cout << "请输入需要进入的程序"<<endl;
                            
                            cout << "1-简易计算器" << endl << "2-规定数字,进行排序" << endl ;
                            cin >> a;
                            switch (a)
                            {
                            case 1:jisuanqi(); goto c; break;
                            case 2:goto B; break;
                            }
A:                          
                            
B:                          cout << "请输入一共有多少个数字"<<endl;
                            cin >> b;
                            for (int i = 1; i <= b; i++)
                            {
                                cout << "请输入第" << i << "个数字" << endl;
                                cin >> arr[i];
                            }
                            for (j = 0; j < b - 1; j++)
                                for (i = 0; i < b - 1; i++)
                                
                                if (arr[i] > arr[i + 1])
                                {
                                    int temp = arr[i];
                                    arr[i] = arr[i + 1];
                                    arr[i + 1] = temp;
                                }
                                
                            for (i = 1; i <= b; i++)
                            cout << arr[i]<<endl;
                            goto c;
                        }

            }
        }
            else
                if (a == 3)
                {
                    std::string currentPassword;
                    std::string newPassword;

                    // 读取以前保存的密码
                    std::string savedPassword = readPassword();

                    // 如果没有保存的密码,要求设置新密码
                    if (savedPassword.empty()) {
                        std::cout << "欢迎首次使用,请设置新密码:";
                        std::cin >> newPassword;
                    }
                    else {
                        // 获取当前密码
                        std::cout << "请输入当前密码:";
                        std::cin >> currentPassword;

                        // 验证当前密码
                        if (currentPassword != savedPassword) {
                            std::cout << "密码错误,无法进行修改!" << std::endl;
                            return 0;
                        }

                        // 获取新密码
                        std::cout << "请输入新密码:";
                        std::cin >> newPassword;
                    }

                    // 验证新密码
                    if (!checkPassword(newPassword)) {
                        std::cout << "新密码不符合要求,修改失败!" << std::endl;
                        return 0;
                    }

                    // 保存密码
                    savePassword(newPassword);

                    // 修改密码成功
                    std::cout << "密码修改成功!" << std::endl;
                }
                else
                    if (a == 4)
                    {
                        std::cout << savedUser;
                        goto b;
                    }
            return 0;
        }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值