鱼C论坛

 找回密码
 立即注册
查看: 829|回复: 4

代码中的问题

[复制链接]
发表于 2023-12-6 03:42:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
// 判断密码是否为中等强度密码
bool is_password_medium(string password) {
    int len = password.length();
    if (len < 8) {
        return false;
    }
    bool has_digit = false, has_lower = false, has_upper = false;
    for (int i = 0; i < len; i++) {
        if (is_digit(password[i])) {
            has_digit = true;
        }
        if (is_lower(password[i])) {
            has_lower = true;
        }
        if (is_upper(password[i])) {
            has_upper = true;
        }
    }
    return has_digit && has_lower && has_upper;
}

// 判断密码是否为强密码
bool is_password_strong(string password) {
    int len = password.length();
    if (len < 8) {
        return false;
    }
    bool has_digit = false, has_lower = false, has_upper = false, has_other = false;
    for (int i = 0; i < len; i++) {
        if (is_digit(password[i])) {
            has_digit = true;
        }
        else if (is_lower(password[i])) {
            has_lower = true;
        }
        else if (is_upper(password[i])) {
            has_upper = true;
        }
        else {
            has_other = true;
        }
    }
    return has_digit && has_lower && has_upper && has_other;
}
代码方法如上,当我在main函数中执行时,输入密码“ADAWDADA1231adadad”本应该输出的时高强度,但是代码输出的是中强度,烦请大佬看看这两个方法该如何修改才能正确执行,题目如下
           中强度:密码长度大于等于8个字符,并且包含至少一个数字和一个字母(可以是大小写)
             高强度:密码长度大于等于8个字符,并且包含至少一个数字、一个小写字母和一个大写字母
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-6 03:42:45 | 显示全部楼层
问题出在判断是否包含数字、小写字母和大写字母的地方。目前的代码中,通过调用 is_digit()、is_lower() 和 is_upper() 函数来判断,但是这些函数需要传入一个字符,并且返回的是布尔值,而不是判断整个字符串是否包含这些字符。

所以,你需要修改这部分代码,使用循环遍历整个字符串,判断是否包含数字、小写字母和大写字母。以下是修改后的代码:

// 判断密码是否为中等强度密码
bool is_password_medium(string password) {
    int len = password.length();
    if (len < 8) {
        return false;
    }
    bool has_digit = false, has_lower = false, has_upper = false;
    for (int i = 0; i < len; i++) {
        if (isdigit(password[i])) { // 使用isdigit()函数判断是否为数字
            has_digit = true;
        }
        if (islower(password[i])) { // 使用islower()函数判断是否为小写字母
            has_lower = true;
        }
        if (isupper(password[i])) { // 使用isupper()函数判断是否为大写字母
            has_upper = true;
        }
    }
    return has_digit && has_lower && has_upper;
}

// 判断密码是否为强密码
bool is_password_strong(string password) {
    int len = password.length();
    if (len < 8) {
        return false;
    }
    bool has_digit = false, has_lower = false, has_upper = false, has_other = false;
    for (int i = 0; i < len; i++) {
        if (isdigit(password[i])) { // 使用isdigit()函数判断是否为数字
            has_digit = true;
        }
        else if (islower(password[i])) { // 使用islower()函数判断是否为小写字母
            has_lower = true;
        }
        else if (isupper(password[i])) { // 使用isupper()函数判断是否为大写字母
            has_upper = true;
        }
        else {
            has_other = true;
        }
    }
    return has_digit && has_lower && has_upper && has_other;
}

这样修改后的代码会在循环中遍历字符串的每个字符,并针对每个字符进行判断,从而正确判断密码的强度。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-6 03:49:41 | 显示全部楼层
不行,这样都没办法运行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-6 03:50:07 | 显示全部楼层
永远Forever 发表于 2023-12-06 03:49
不行,这样都没办法运行

非常抱歉,我犯了个错误。是的,这些函数是用于单个字符的判断,而不是用于整个字符串的判断。为了解决这个问题,我们可以使用迭代器遍历字符串中的每个字符,并使用`isdigit()`、`islower()`和`isupper()`函数来判断字符是否满足条件。以下是修改后的代码:
#include <iostream>#include <string>using namespace std;// 判断密码是否为中等强度密码bool is_password_medium(string password) {    int len = password.length();    if (len < 8) {        return false;    }    bool has_digit = false, has_lower = false, has_upper = false;    for (auto it = password.begin(); it != password.end(); ++it) {        if (isdigit(*it)) { // 使用isdigit()函数判断是否为数字            has_digit = true;        }        if (islower(*it)) { // 使用islower()函数判断是否为小写字母            has_lower = true;        }        if (isupper(*it)) { // 使用isupper()函数判断是否为大写字母            has_upper = true;        }    }    return has_digit && has_lower && has_upper;}// 判断密码是否为强密码bool is_password_strong(string password) {    int len = password.length();    if (len < 8) {        return false;    }    bool has_digit = false, has_lower = false, has_upper = false, has_other = false;    for (auto it = password.begin(); it != password.end(); ++it) {        if (isdigit(*it)) { // 使用isdigit()函数判断是否为数字            has_digit = true;        }        else if (islower(*it)) { // 使用islower()函数判断是否为小写字母            has_lower = true;        }        else if (isupper(*it)) { // 使用isupper()函数判断是否为大写字母            has_upper = true;        }        else {            has_other = true;        }    }    return has_digit && has_lower && has_upper && has_other;}int main() {    string password;    cout << "请输入密码:";    cin >> password;    if (is_password_strong(password)) {        cout << "密码强度:高" << endl;    }    else if (is_password_medium(password)) {        cout << "密码强度:中" << endl;    }    else {        cout << "密码强度:低" << endl;    }    return 0;}
这样修改后的代码可以正确运行,并根据输入的密码输出密码的强度。请尝试运行一下,看看是否能够得到正确的结果。如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-6 09:14:32 | 显示全部楼层
本帖最后由 jackz007 于 2023-12-6 09:19 编辑
// 判断密码是否为强密码
bool is_password_strong(string password) {
. . . . . .
    return has_digit && has_lower && has_upper && has_other;  // && has_other 画蛇添足了,只要去掉就好
    就是说,把
    return has_digit && has_lower && has_upper && has_other; 
    改为
    return has_digit && has_lower && has_upper; 
    即可。
    至于原因嘛,因为你输入的密码 “ADAWDADA1231adadad” 中根本就没有 has_other 的成分存在,从而就干扰了函数的判断结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-27 02:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表