密码的简单模拟

 1 #include<stdio.h>
 2 char password[]="592026",intput[10000];
 3 int main()
 4 {
 5     int p();
 6     int i,n=5,t;
 7     char sr;
 8     printf("You have only 5 chances!\n");
 9     while(n--)
10     {
11         i=0;
12         printf("Please intput password:");
13         while((sr=getch())!='\r')
14         {
15             if(sr=='\b')
16             {
17                 printf("\b \b");
18                 intput[--i]='\0';
19             }
20             else
21             {
22                 intput[i++]=sr;
23                 printf("*");
24             }
25         }
26         printf("\n");
27         t=p();
28         if(t)
29             break;
30         printf("\n");
31     }
32     if(!t)
33         printf("Input more than 5 times!");
34 }
35 
36 int p()
37 {
38     int i=0,t=0;
39     while(password[i]!='\0')
40     {
41         if(!i&&intput[i]==password[i])
42             t=1;
43         if(intput[i]!=password[i])
44         {
45             t=0;
46             break;
47         }
48         if(password[i+1]=='\0'&&intput[i+1]!='\0')
49             t=0;
50         ++i;
51     }
52     if(t)
53         printf("Password is right!\n");
54     else
55         printf("Password is error!\n");
56     return t;
57 }

 

posted @ 2016-11-13 12:47  Posase  阅读(205)  评论(0编辑  收藏  举报