解题思路:
依次判断条件并将结果存入总分数score
中,由于rust
中的char
类型的is_ascii_digit()
、is_ascii_uppercase()
、is_ascii_lowercase()
、is_ascii_punctuation()
四个函数可以分别用来判断数字、大小写和字符,所以就偷懒采用而不判ascii
码了。
以下代码虽然行数较多,但是顺序阅读就会发现使用了较多的match
表达式,逻辑较为简单。
use std::io::{self, *};
fn main() {
let stdin = io::stdin();
for line in stdin.lock().lines() {
let ll = line.unwrap();
let ll = ll.trim();
let mut score = 0u8;
score += match ll.len() {//长度
0..=4 => 5,
5..=7 => 10,
_ => 25,
};
let (mut has_digit,mut has_up ,mut has_low,mut has_punc,mut has_more_dig,mut has_more_punc) = (false,false,false,false,false,false);
for i in ll.chars() {
if i.is_ascii_digit() {
if !has_digit {
has_digit = true;
}else {
has_more_dig = true;
continue;
}
}else if i.is_ascii_uppercase() {
if !has_up {
has_up = true;
}else {
continue;
}
}else if i.is_ascii_lowercase() {
if !has_low {
has_low = true;
}else {
continue;
}
}else if i.is_ascii_punctuation() {
if !has_punc {
has_punc = true;
}else {
has_more_punc = true;
continue;
}
}
}
score += match (has_low,has_up) {//字母
(false,false) => 0,
(true,true) => 20,
_ => 10,
};
score += match (has_digit,has_more_dig) {//数字
(false,false) => 0,
(true,true) => 20,
_ => 10,
};
score += match (has_punc,has_more_punc) {//符号
(false,false) => 0,
(true,true) => 25,
_ => 10,
};
score += match (has_digit,has_up,has_low,has_punc) {//奖励
(true,true,true,true) => 5,
(true,true,false,true) | (true,false,true,true) => 3,
(true,true,_,false) | (true,_,true,false) => 2,
_ => 0,
};
println!("{}",match score {
0..=24 => "VERY_WEAK",
25..=49 => "WEAK",
50..=59 => "AVERAGE",
60..=69 => "STRONG",
70..=79 => "VERY_STRONG",
80..=89 => "SECURE",
_ => "VERY_SECURE",
})
}
}