C语言实现密码输入显示星号 VS2010 亲测通过 功能加强版

// 登录界面   第一次可设置密码   之后的登录要输入密码进行验证    密码长度8~12
//长度过长会截断取前12位,不足8位会提示出错  要求重新输入

//密码中必须有 字母数字和特殊字符   否则会报错

//  VC 6.0


#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
#define LEN 12
static int count=0;
int main(){
	char password[LEN+1];
	memset(password,NULL,LEN+1);
	char ch;
	if(count==0){
loop_passwordSet:
		printf("If this is your first time to login,pls set your password.\n");
		printf("the length of password has a length between 8~12 without any unprint charators.\n");
		printf("if the password has a length more than 12,it will be trancted\n");
		printf("密码需有 至少各一位的 数字  字母 和特殊字符\n");
		printf("========================================\n");
	int inputLength=0;
	ch=getch();	
	while(ch=='\b'){
		printf("\a");
		ch=getch();
	}
	while(isprint(ch) || ch=='\b'){
		if(ch!=' '){			//正常输入
			putchar('*');
			password[inputLength]=ch;
			inputLength++;
			ch=getch();
		}
		if((!isprint(ch) || ch==' ')&&(ch!='\b'))		//有不可打印字符  或者 空格键 时截断    当然没有删除字符动作出现
			break;
		while(ch=='\b'){
			printf("\b");
			inputLength--;
			while(inputLength==-1){
			printf("\a");   删除完毕要是继续删除的话会警报提示
			ch=getch();
			if(isprint(ch) && ch!=' ' && ch!='\b'){
				inputLength++;
				break;
			}
			}
			ch=getch();
		}
		if(inputLength==LEN)			//长度已满时截断
			break;		
	}
	if(inputLength<7){
	printf("\n");
	printf("too SHORT,pls try again!\n");
	printf("\n");
	goto loop_passwordSet;
	}
	//   这一段时检测设置的密码强度是否满足要求
	{
		int i=0;			
		int num=0,alpha=0,spe=0;   //分别代表数字 字母 特殊字符的个数
		for(i=0;i<inputLength;i++){						
			if(isalpha(password[i]));
				alpha++;
			if(isdigit(password[i]))
				num++;
			if(ispunct(password[i]))				//ispunc  判断是否为为标点符号或特殊符号
				spe++;				
		}
		if(alpha&&num&& spe==0){
			printf("\n");
			printf("输入的密码不合要求,请重新输入!\n");
			goto loop_passwordSet;
		}
		
	}
	//
	printf("\n");
	printf("what you have just input is: %s\n",password);
	printf("Press Y/N to confirm.\n");
	ch=getch();
	switch(ch){
	case 'Y'  :{
		printf("password set completed!\n");
		count=1;
		break;
	}
	  case 'y'  :{
		printf("password set completed!\n");
		count=1;
		break;
	}
	case 'N': {
		printf("password set canceled.\n");
		goto loop_passwordSet;
	}
	case 'n': {
		printf("password set canceled.\n");
		goto loop_passwordSet;
	}
	
	default: {
		printf("invalid input,pls try again.\n");
		goto loop_passwordSet;
	}
	}
	}
	if(count==1){
	char toMatchPassword[LEN+1];	
loop_inputPassword:
	memset(toMatchPassword,NULL,LEN+1);
	int toMatchLength=0;
	printf("input your password to login.\n");
		printf("===================\n");
	ch=getch();
	while(ch=='\b'){
		printf("\a");
		ch=getch();
	}
	while(isprint(ch) || ch=='\b'){
		if(ch!=' '){
			putchar('*');
			toMatchPassword[toMatchLength]=ch;
			toMatchLength++;		
			ch=getch();
		}
		if((!isprint(ch) || ch==' ')&&(ch!='\b')){
		break;
		}
		while(ch=='\b'){
		printf("\b");
		toMatchLength--;
		while(toMatchLength==-1){
			printf("\a");				//删除完毕要是继续删除的话会警报提示
			ch=getch();
			if(ch=='\b')
				toMatchLength=-1;
			else
				toMatchLength=0;
		}
			ch=getch();
		}		
		if(toMatchLength==LEN){
		break;
		}
	}
	if(strcmp(password,toMatchPassword)==0){
		printf("password Correct!  LOGIN SUCCESS!\n");
		printf("**********************\n");
		printf("**********************\n");
		printf("**   WELCOME       **\n");
		printf("**********************\n");
		printf("**********************\n");
		printf("***********   By ZHAOs*\n");
		Sleep(2000);
	}
	if(strcmp(password,toMatchPassword)!=0){
		printf("\n");
		printf(" input error!   Y to try again, N to quit!	(NO more than 3 times) \n");
	static	int inputTimes=0;
		char ch=getch();
		switch(ch){
		case 'Y':{
				inputTimes++;
				if(inputTimes==3){
					printf("too many times... now the system will quit..\n");
					Sleep(2000);
					exit(0);
				}
				 goto loop_inputPassword;
				 		 }
		case 'y':{
				inputTimes++;
				if(inputTimes==3){
					printf("too many times... now the system will quit..\n");
					Sleep(2000);
					exit(0);
				}
				 goto loop_inputPassword;
				 		 }
		case 'N':{
					exit(0);
				 }
		case 'n':{
					exit(0);
				 }
				 default:
					 {
						printf("invalid input, now the system would abort....\n");
						Sleep(2000);
						exit(0);
					 }
		}
	}
	}
system("pause");
return 0;
}



尼玛 一个弱弱的功能居然有两百行。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值