1 #ifndef USER.HPP 2 #define USER.HPP 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 int BF(string S,string T); 7 class User 8 { 9 private: 10 string name; 11 string password; 12 string email; 13 static int count; 14 public: 15 User(string n1):name(n1),password("111111"),email(""){++count;} 16 User(string n1,string p1,string e1):name(n1),password(p1),email(e1){++count;}
17 void set_email(string e1); 18 void set_email(); 19 void change_passwd(); 20 void print_info()const; 21 static void print_n();
22 ~User(){--count;}; 23 }; 24 int User::count=0; 25 void User::set_email() 26 { 27 string e1; 28 cout<<"Enter email address: "; 29 cin>>e1; 30 int i,flag=1; 31 while(flag) 32 { 33 for(i=0;e1[i]&&flag;i++) 34 { 35 if(e1[i]=='@') 36 flag=0; 37 } 38 if(flag||!BF(e1,".com")) 39 { 40 cout<<"Invalid email address!Please enter again:"; 41 cin>>e1; 42 } 43 } 44 email=e1; 45 cout<<"Email is set successfully..."<<endl; 46 } 47 void User::change_passwd() 48 { 49 using namespace std; 50 cout<<"Enter old password:"; 51 int i=1; 52 while(i<4) 53 { 54 string p; 55 cin>>p; 56 if(p!=password) 57 cout<<"Password input error.Please re-enter again:"; 58 else 59 { 60 cout<<"Enter new password: "; 61 int flag=1; 62 while(flag) 63 { 64 int count=0; 65 cin>>p; 66 if(p.length()>=6) 67 { 68 for(int i=0;i<p.length();i++) 69 if(isdigit(p[i])) 70 count++; 71 if(count==p.length()) 72 cout<<"Your password is too simple.Please re-enter again:"; 73 else 74 flag=0; 75 } 76 else 77 cout<<"Your password is too short.Please re-enter again:"; 78 } 79 password=p; 80 cout<<"New password is set successfully..."<<endl; 81 break; 82 } 83 i++; 84 } 85 if(i==4) 86 cout<<"Please try after a while."<<endl; 87 } 88 void User:: print_info()const 89 { 90 using namespace std; 91 cout<<"name:"<<name<<endl<<"password: ******"<<endl<<"email: "<<email<<endl; 92 } 93 void User::print_n() 94 { 95 if(count==0) 96 cout<<"There is "<<count<<" user."<<endl; 97 else 98 cout<<"There are "<<count<<" users."<<endl; 99 } 100 int BF(string S,string T) 101 { 102 int i=0,j=0; 103 while (i<S.length()&&j<T.length()) 104 { 105 if(S[i]==T[j]) 106 { 107 i++; 108 j++; 109 } 110 else 111 { 112 i=i-j+1; 113 j=0; 114 } 115 } 116 if(j==T.length()) 117 return 1; 118 else 119 return 0; 120 } 121 #endif
1 #include"User.hpp" 2 #include<iostream> 3 int main() 4 { 5 using namespace std; 6 cout << "testing 1......" << endl; 7 User user1("Tonny", "321321", "xxs@hotmail.com"); 8 user1.print_info(); 9 cout << endl<< "testing 2......" << endl; 10 User user2("Conner"); 11 user2.change_passwd(); 12 user2.set_email(); 13 user2.print_info(); 14 User::print_n(); 15 }
注意string类型的使用,使用前要添加using namespace std;语句在宏定义后面。