解题思路:
本题乍一看上十分无聊,但又是一个 常见功能的实现,判断密码强度。对于思维锻炼和性子磨练还是有好处的 ^~^ ^~^
1、建议将五个 判分条件 封装成函数,来优化代码的可读性。
#include <stdio.h>
void score_judge(unsigned int score) {
if (score >= 90) {
printf("VERY_SECURE\n");
} else if (score >= 80) {
printf("SECURE\n");
} else if (score >= 70) {
printf("VERY_STRONG\n");
} else if (score >= 60) {
printf("STRONG\n");
} else if (score >= 50) {
printf("AVERAGE\n");
} else if (score >= 25) {
printf("WEAK\n");
} else {
printf("VERY_WEAK\n");
}
}
unsigned int length_judge(unsigned int length) {
if (length >= 8) {
return 25;
} else if (length >=5) {
return 10;
} else {
return 5;
}
}
unsigned int words_judge(unsigned int xiaoxie, unsigned int daxie) {
if ((xiaoxie == 0) && (daxie == 0)) {
return 0;
} else if (((xiaoxie != 0) && (daxie == 0)) || ((xiaoxie == 0) && (daxie != 0))) {
return 10;
} else {
return 20;
}
}
unsigned int shuzi_judge(unsigned int shuzi) {
if (shuzi >1) {
return 20;
} else if (shuzi == 1) {
return 10;
} else {
return 0;
}
}
unsigned int special_char_judge(unsigned int special) {
if (special >1) {
return 25;
} else if (special == 1) {
return 10;
} else {
return 0;
}
}
unsigned int special_score_judge(unsigned int xiaoxie, unsigned int daxie, unsigned int shuzi, unsigned int special) {
unsigned int sum = xiaoxie + daxie;
if ((xiaoxie != 0) && (daxie != 0) && (shuzi != 0) && (special != 0)) {
return 5;
} else if ((sum != 0) && (shuzi != 0) && (special == 0)) {
return 2;
} else if ((sum != 0) && (shuzi != 0) && (special != 0)) {
return 3;
}
return 0;
}
int main(void) {
char str[301] = {0};
unsigned int score = 0;
while (fgets(str, 301, stdin)) {
unsigned length = strlen(str) -1;
score = length_judge(length); // 1、 长度判断
unsigned int xiaoxie = 0;
unsigned int daxie = 0;
unsigned int shuzi = 0;
unsigned int special = 0;
for (unsigned int i = 0; i <length; i++) {
if((str[i] >= 'a') && (str[i] <= 'z')) {
xiaoxie ++;
} else if ((str[i] >= 'A') && (str[i] <= 'Z')) {
daxie++;
} else if ((str[i] >= '0') && (str[i] <= '9')) {
shuzi++;
} else if (((str[i] >= 0x21) && (str[i] <= 0x2F)) || ((str[i] >= 0x3A) && (str[i] <= 0x40)) || ((str[i] >= 0x5B) && (str[i] <= 0x60)) || ((str[i] >= 0x7B) && (str[i] <= 0x7E))) {
special++;
}
}
score += words_judge(xiaoxie, daxie); // 2 字母判断
score += shuzi_judge(shuzi); // 3 数字判断
score += special_char_judge(special); // 4 符号判断
score += special_score_judge(xiaoxie, daxie, shuzi, special); // 奖励判断
score_judge(score); // 根据评分打印
}
return 0;
}