solution
潜在的非法字符里可能包含空格,所以不能直接用cin接收string(测试点2)
#include<iostream>
#include<string>
using namespace std;
int judge(string s){
if(s.size() < 6) return 1;
int num = 0, c = 0;
for(int i = 0; i < s.size(); i++){
if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) c = 1;
else if(s[i] >= '0' && s[i] <= '9') num = 1;
else if(s[i] != '.') return 2;
}
if(!num) return 3;
if(!c) return 4;
return 5;
}
int main(){
int n;
string s;
cin >> n;
getchar();
while(n--){
getline(cin, s);
if(judge(s) == 1) cout << "Your password is tai duan le." << endl;
else if(judge(s) == 2) cout << "Your password is tai luan le." << endl;
else if(judge(s) == 3) cout << "Your password needs shu zi." << endl;
else if(judge(s) == 4) cout << "Your password needs zi mu." << endl;
else cout << "Your password is wan mei." << endl;
}
return 0;
}
或者
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
string s;
cin >> n;
getchar();
while(n--){
getline(cin, s);
if(s.size() < 6) cout << "Your password is tai duan le." << endl;
else{
int num = 0, c = 0, no = 0;
for(int i = 0; i < s.size(); i++){
if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) c = 1;
else if(s[i] >= '0' && s[i] <= '9') num = 1;
else if(s[i] != '.') no = 1;
}
if(no) cout << "Your password is tai luan le." << endl;
else if(!num) cout << "Your password needs shu zi." << endl;
else if(!c) cout << "Your password needs zi mu." << endl;
else cout << "Your password is wan mei." << endl;
}
}
return 0;
}
或者
字符判断(数字&字母)
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
string s;
cin >> n;
getchar();
while(n--){
getline(cin, s);
if(s.size() < 6) cout << "Your password is tai duan le." << endl;
else{
int num = 0, c = 0, no = 0;
for(int i = 0; i < s.size(); i++){
if(isalpha(s[i])) c = 1;
else if(isalnum(s[i])) num = 1;
else if(s[i] != '.') no = 1;
}
if(no) cout << "Your password is tai luan le." << endl;
else if(!num) cout << "Your password needs shu zi." << endl;
else if(!c) cout << "Your password needs zi mu." << endl;
else cout << "Your password is wan mei." << endl;
}
}
return 0;
}