鱼C论坛

 找回密码
 立即注册
查看: 1828|回复: 1

1个点未通过,麻烦大佬看看,有啥缺陷

[复制链接]
发表于 2020-3-26 21:29:55 | 显示全部楼层 |阅读模式

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

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

x
题目:
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。

输入格式:
输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。

输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:

如果密码合法,输出Your password is wan mei.;
如果密码太短,不论合法与否,都输出Your password is tai duan le.;
如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。
输入样例:
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6

输出样例:
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.
我只想到的一种缺陷:如果是>5的'.'的字符串改输出啥呢?
  1. #include <iostream>
  2. #include <cctype>
  3. using namespace std;
  4. #include <string>

  5. int main(void)
  6. {
  7.         int index;
  8.         string s1;
  9.         cin >> index;
  10.         int zimu=0,xiaoshudian=0,shuzi=0,qita=0;
  11.         for(int i=0;i<index;i++)
  12.         {
  13.                 zimu=0;xiaoshudian=0;shuzi=0;qita=0;
  14.                 cin >> s1;
  15.                 if(s1.size()<6)
  16.                 {
  17.                         cout<<"Your password is tai duan le."<<endl;
  18.                 }
  19.                 else
  20.                 {
  21.                         for(int j=0;j<s1.size();j++)
  22.                         {
  23.                                 if(s1[j]=='.')
  24.                                 {
  25.                                         xiaoshudian++;
  26.                                         continue;
  27.                                 }
  28.                                 if(isalpha(s1[j]))
  29.                                 {
  30.                                         zimu++;continue;
  31.                                 }
  32.                                 if(isdigit(s1[j]))
  33.                                 {
  34.                                         shuzi++;continue;
  35.                                 }
  36.                                 else
  37.                                 {
  38.                                         qita++;
  39.                                 }
  40.                         }
  41.                         if(qita)
  42.                         {
  43.                                 cout << "Your password is tai luan le."<<endl;
  44.                                 continue;
  45.                         }
  46.                         if(!zimu)
  47.                         {
  48.                                 cout << "Your password needs zi mu."<<endl;
  49.                                
  50.                         }
  51.                         if(!shuzi)
  52.                         {
  53.                                 cout <<"Your password needs shu zi."<<endl;
  54.                        
  55.                         }
  56.                         if(zimu && shuzi)
  57.                         {
  58.                                 cout <<"Your password is wan mei."<<endl;
  59.                        
  60.                         }
  61.                 }
  62.                
  63.         }
  64.        
  65.         system("pause");
  66.         return 0;
  67. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-26 22:45:48 | 显示全部楼层
所有的输入都保存起来,让所有的输出都在一起看看

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>

  4. int main()
  5. {
  6.         int num;
  7.         std::cin >> num;
  8.         std::string *s = new std::string[num];
  9.         for(int i = 0; i < num; ++i) std::cin >> s[i];
  10.         for(int i = 0; i < num; ++i) {
  11.                 if(s[i].size() < 6) {
  12.                         std::cout << "Your password is tai duan le." << std::endl;
  13.                         continue;
  14.                 }
  15.                 bool digit = false;
  16.                 bool alpha = false;
  17.                 bool invalid = false;
  18.                 for(const auto j: s[i]) {
  19.                         if(isalpha(j)) alpha = true;
  20.                         else if(isdigit(j)) digit = true;
  21.                         else if(j != '.') {
  22.                                 invalid = true;
  23.                                 break;
  24.                         }
  25.                 }
  26.                 if(invalid) std::cout << "Your password is tai luan le." << std::endl;
  27.                 else if(alpha && !digit) std::cout << "Your password needs shu zi." << std::endl;
  28.                 else if(!alpha && digit) std::cout << "Your password needs zi mu." << std::endl;
  29.                 else std::cout << "Your password is wan mei." << std::endl;

  30.         }
  31.         delete[] s;
  32.         return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 15:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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