自己写的一个登陆验证
自己写的一个登陆验证可以上下切换,和删除
程序代码:
#include<stdio.h> #include<conio.h> #include "windows.h" #define ORG_X 20 #define ORG_Y 3 /* **定义账号,密码最大位数 */ #define HMAX 10 #define PMAX 10 /* **定义账号,密码 */ #define HH "2007121148" #define PP "771691" int main(void) { int password(void); if( 1==password() ) { printf("欢迎!\n"); } else { printf("非法用户!\n"); } return 0; } void gotoxy(int x,int y) { COORD c; c.X=x-1; c.Y=y-1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } int password(void) { int h_x,p_x,now_y; /*h_x,p_x,now_y当前帐号,密码,行光标位置*/ int count_h=0,count_p=0;/*已输入帐号,密码个数*/ char ch; char h[HMAX+1],p[PMAX+1];/*保存账号,密码*/ p_x=h_x=ORG_X; now_y=ORG_Y; gotoxy(ORG_X-5,ORG_Y+1); printf("密码:"); gotoxy(ORG_X-5,ORG_Y); printf("帐号:"); while((ch=getch())!=13) { /* **处理切换 */ if( ('\t'==ch) && ORG_Y==now_y ) { now_y+=1; gotoxy(p_x,now_y); continue; } if( ('\t'==ch) && ORG_Y+1==now_y) { now_y-=1; gotoxy(h_x,now_y); continue; } /* **处理删除 */ if( ((char)8==ch) && h_x>ORG_X && ORG_Y==now_y ) { h[ORG_X - h_x]=0; h_x--; count_h--; gotoxy(h_x,now_y); printf(" "); gotoxy(h_x,now_y); continue; } if( ((char)8==ch) && p_x>ORG_X && ORG_Y+1==now_y ) { p[ORG_X - p_x]=0; p_x--; count_p--; gotoxy(p_x,now_y); printf(" "); gotoxy(p_x,now_y); continue; } /* **处理输入 */ if(ORG_Y==now_y && count_h<HMAX && ch!=8) { h[h_x - ORG_X]=ch; printf("%c",ch); h_x++; count_h++; } if(ORG_Y+1==now_y && count_p<PMAX && ch!=8) { p[p_x - ORG_X]=ch; printf("*"); p_x++; count_p++; } } p[p_x-ORG_X]=0; printf("\n"); h[h_x-ORG_X]=0; /* **验证密码 */ if( 0==strcmp(h,HH) ) { if( 0==strcmp(p,PP) ) { system("cls"); return 1; } } system("cls"); return 0; }