#include <stdio.h>
#include <string.h>
#define N 100
struct user
{
int user_id;
char username[18];
char password[16];
};
int is_username_length_available(char* p);
int is_username_character_available(char* p);
int is_password_length_available(char* p);
int is_password_character_available(char* p);
int is_password_same(char* pre_psd,char* wait_psd);
int user_register(struct user *userinfo,int count);
int loop_username(char* p);
int loop_password(char* p);
void password_input(char* pre_psd,char* re_psd);
int count_password_ckeck(struct user *userinfo,int count);
int is_my_user_id(int input_id,int restore_id);
int is_my_user_psd(char* input_psd,char* p);
int update(struct user *userinfo,int count);
int show_menu();
int i=0;
int main()
{
int num;
struct user userinfo[100];
while(1)
{
switch(show_menu())
{
case 1:
if(user_register(userinfo,i))
{
i++;
}
;break;
case 2:
if(count_password_ckeck(userinfo,i))
{
printf("登陸成功!\n");
}
;break;
case 3:
if(update(userinfo,i))
{
printf("密碼修改成功\n");
}
else
{
printf("密碼修改失敗\n");
}
;break;
}
}
}
//user:
//judge username's length available
int is_username_length_available(char* p)
{
if(strlen(p)>18||strlen(p)<6)
{
return 0;
}
else
{
return 1;
}
}
//judge username's character available
int is_username_character_available(char* p)
{
int i;
if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
{
for(i=0;i<strlen(p);i++)
{
if(((p[i]<'a'&&p[i]>'z') || (p[i]<'A'&&p[i]>'Z') ||(p[i]<'0'&p[i]>'9')))
{
return 0;
}
else if((p[i]==' '))
{
printf("您輸入的用戶命中含有空格,請檢查!\n");;
}
}
}
else if(*p=='_')
{
return 0;
}
else
{
return 1;
}
}
//password:
//is password length available
int is_password_length_available(char* p)
{
if(strlen(p)>16||strlen(p)<6)
{
return 0;
}
else
{
return 1;
}
}
//is password character available
int is_password_character_available(char* p)
{
int i;
if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
{
for(i=0;i<strlen(p);i++)
{
if((p[i]==' ') || (p[i]<'a'&&p[i]>'z') || (p[i]<'A'&&p[i]>'Z'))
{
return 0;
}
else if((p[i]==' '))
{
return 1;
}
}
}
else
{
return 1;
}
}
//ckeck password
int is_password_same(char* pre_psd,char* wait_psd)
{
if(strcmp(pre_psd,wait_psd)==0)
{
return 1;
}
else
{
return 0;
}
}
//loop username error
int loop_username(char* p)
{
if(!is_username_length_available(p))
{
printf("您輸入的用戶名長度應該在6-18范圍,請您檢查!\n");
return 0;
}
else if(!is_username_character_available(p))
{
printf("您輸入的用戶名首字符是下劃線,請您以字母或者數(shù)字開頭!\n");
return 0;
}
else
{
return 1;
}
}
//loop password error
int loop_password(char* p)
{
if(!is_password_length_available(p))
{
printf("您輸入的密碼長度應該在6-16范圍,請您檢查!\n");
return 0;
}
else if(!is_password_character_available(p))
{
printf("您輸入的密碼不符合要求,只有字母,數(shù)字,下劃線等字符才滿足要求,請您檢查!\n");
return 0;
}
else
{
return 1;
}
}
//password input&check
void password_input(char* pre_psd,char* re_psd)
{
printf("請輸入密碼:\n");
fflush(stdin);
scanf("%s",pre_psd);
while(!loop_password(pre_psd))
{
printf("請輸入密碼:\n");
fflush(stdin);
scanf("%s",pre_psd);
printf("請再次輸入密碼:\n");
fflush(stdin);
scanf("%s",re_psd);
while(!is_password_same(pre_psd,re_psd))
{
printf("-------------------------!\n");
}
}
printf("請再次輸入密碼:\n");
fflush(stdin);
scanf("%s",re_psd);
if(is_password_same(pre_psd,re_psd))
{
printf("mimayizhi\n");
}
else
{
printf("mimabuyizhi\n");
password_input(pre_psd,re_psd);
}
}
//user register
int user_register(struct user *userinfo,int count)
{
char pre_psd[16];
char re_psd[16];
printf("請輸入用戶名:\n");
scanf("%s",userinfo[count].username);
while(!loop_username(userinfo[count].username))
{
printf("請輸入用戶名:\n");
scanf("%s",userinfo[count].username);
}
password_input(pre_psd,re_psd);
strcpy(userinfo[count].password,pre_psd);
printf("username:%s\npassword:%s\n",userinfo[count].username,userinfo[count].password);
printf("您已經(jīng)完成注冊!\n您的賬號為:%d\n",userinfo[count].user_id=10000+count);
return 1;
}
//implement login function
int count_password_ckeck(struct user *userinfo,int count)
{
int k;
int temp;
int counter;
char input_psd[16];
for(k=0;k<count;k++)
{
temp=is_my_user_id(counter,userinfo[k].user_id);
while(!temp)
{
temp=is_my_user_id(counter,userinfo[k].user_id);
}
temp=is_my_user_psd(input_psd,userinfo[k].password);
while(!temp)
{
temp=is_my_user_psd(input_psd,userinfo[k].password);
}
}
}
//is my user_id
int is_my_user_id(int input_id,int restore_id)
{
printf("請輸入您的賬號:\n");
scanf("%d",&input_id);
if(input_id==restore_id)
{
return 1;
}
else
{
printf("您輸入的賬號不存在,請檢查!\n");
return 0;
}
}
//is_my_user_psd
int is_my_user_psd(char* input_psd,char* p)
{
printf("請您輸入密碼:\n");
scanf("%s",input_psd);
if(strcmp(input_psd,p)==0)
{
//printf("登陸成功!\n");
return 1;
}
else
{
printf("您輸入的密碼不正確,請檢查后重新輸入!\n");
return 0;
}
}
int update(struct user *userinfo,int count)
{
int counter;
int k;
int temp;
char psd[16];
char psd1[16];
char psd2[16];
for(k=0;k<count;k++)
{
temp=is_my_user_id(counter,userinfo[k].user_id);
while(!temp)
{
temp=is_my_user_id(counter,userinfo[k].user_id);
}
temp=is_my_user_psd(psd,userinfo[k].password);
while(!temp)
{
temp=is_my_user_psd(psd,userinfo[k].password);
}
printf("密碼正確,您可以對密碼進行修改了!\n");
password_input(psd1,psd2);
strcpy(userinfo[k].password,psd1);
}
}
//show menu
int show_menu()
{
int choice;
printf("請您選擇:\n");
printf("1.用戶注冊\n2.用戶登錄\n3.密碼修改\n請輸入1-3\n");
scanf("%d",&choice);
return choice;
}