#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct password
{
char seg;
struct password *link;
}password;
int main()
{
char c = '\0';
int i = 0;
int j = 0;
int passlen = 0;
int correct = 0;
int currentvalid = 0;
int errornum = 0;
char passwords[] = "qinghuazhu";
password *temp = NULL;
password *tail = NULL;
password *passhead = NULL;
while(passhead == NULL)
passhead = (password *)malloc(sizeof(password));
for (i = 0; i < 3; i++)
{
currentvalid = 0;
passhead->link = NULL;
tail = passhead;
printf("Password:");
while(1)
{
c = getch();
if(c == (char)13)
break;
else
{
if(c == (char)8)
{
if(passhead->link != NULL && currentvalid > 0)
{
temp = passhead;
while(temp->link != tail)
temp = temp->link;
free(tail);
temp->link = NULL;
tail = temp;
currentvalid--;
printf("\b \b");
}
}
else
{
temp = NULL;
while(temp == NULL)
temp = (password *)malloc(sizeof(password));
temp->seg = c;
temp->link = NULL;
tail->link = temp;
tail = temp;
currentvalid++;
printf("*");
}
}
}
passlen = strlen(passwords);
j = 0;
temp = passhead->link;
while(temp != NULL && j < passlen)
{
if(passwords[j] != temp->seg)
break;
else
{
j++;
temp = temp->link;
}
}
printf("\n");
if(temp == NULL && j == passlen)
{
printf("password correct. Welcome\n");
correct = 1;
}
else
{
errornum++;
printf("password error. Sorry. ERROR NO. = %d\n", errornum);
}
temp = passhead->link;
while(temp != NULL)
{
tail = temp->link;
free(temp);
temp = tail;
}
passhead->link = temp;
if(correct == 1)
{
free(passhead);
break;
}
}
return 1;
}