1 #include <stdio.h>
2 #include <getch.h>
3
4 //密码最大长度
5 #define LEN_MAX 12
6
7 //密码最小长度
8 #define LEN_MIN 6
9
10 //密码字符范围
11 #define Scope_of_password (33 <= n && n <= 126)
12
13 //密码获取
14 void getch_mima(char* mima)
15 {
16 int i=0;
17 while(1)
18 {
19 int n=getch(); //获取字符
20 if(10 == n && i>LEN_MIN-1) //回车结束
21 {
22 mima[i] = 0;
23 return;
24 }
25 else if(127 == n) //退格显示
26 {
27 i--;
28 printf("\b \b");
29 }
30 else if(Scope_of_password && i<LEN_MAX) //密码获取并显示*,限制字符范围。
31 {
32 mima[i]=n;
33 i++;
34 printf("*");
35 }
36 }
37 }
38 int main(int argc,const char* argv[])
39 {
40 char mima[LEN_MAX+1];
41 getch_mima(mima);
42 printf("%s",mima);
43
44 }