酷代码 AI
菜单
服务商

网站密码有效性检查:代码实现与详细解读

```cpp #include <iostream> #include <string> #include <sstream> using namespace std; // 检查密码是否合规 bool isValidPassword(const string& password) { // 检查密码长度 if (password.length() < 6 || password.length() > 12) { return false; } bool hasLower = false; bool hasUpper = false; bool hasDigit = false; bool hasSpecial = false; // 遍历密码中的每个字符 for (char c : password) { if (c >= 'a' && c <= 'z') { hasLower = true; } else if (c >= 'A' && c <= 'Z') { hasUpper = true; } else if (c >= '0' && c <= '9') { hasDigit = true; } else if (c == '!' || c == '@' || c == '#' || c == '$') { hasSpecial = true; } else { // 包含非法字符 return false; } } // 检查是否至少包含两种字符类型和一个特殊字符 int typeCount = (hasLower? 1 : 0) + (hasUpper? 1 : 0) + (hasDigit? 1 : 0); return typeCount >= 2 && hasSpecial; } int main() { string input; getline(cin, input); // 使用字符串流分割输入 stringstream ss(input); string password; while (getline(ss, password, ',')) { if (isValidPassword(password)) { cout << password << endl; } } return 0; } ``` 代码解释: 1. `isValidPassword` 函数: - 首先检查密码长度是否在 6 到 12 个字符之间。 - 遍历密码中的每个字符,标记是否包含小写字母、大写字母、数字和特殊字符。 - 若包含非法字符,直接返回 `false`。 - 最后检查是否至少包含两种字符类型和一个特殊字符。 2. `main` 函数: - 读取一行输入。 - 使用 `stringstream` 按逗号分割输入字符串。 - 对每个分割后的密码调用 `isValidPassword` 函数检查是否合规,若合规则输出。 ######[AI问答 | 567点数解答 | 2025-11-26 20:44:53]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]