Set the password with the stack

Source: Internet
Author: User
Tags clear screen continue printf

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
Char password[10]= "abcdef"; /* Password, global variable * *
typedef char Selemtype;
typedef struct STACK/* Definition Stack type * *
{
Selemtype *base;
Selemtype *top;
int stacksize;
int length;
}sqstack,*stack;
typedef int STATUS;
void Initstack (Stack *s)/* Initialize STACK * *
{
*s= (Sqstack *) malloc (sizeof (sqstack));
(*s)->base= (Selemtype *) malloc (stack_init_size*sizeof (Selemtype));
if (!) ( *s)->base) exit (-1);
(*s)->top= (*s)->base;
(*s)->stacksize=stack_init_size;
(*s)->length=0;
}
Status destroystack (Stack *s)/* Destroy Stack * *
{
Free ((*s)->base);
Free ((*s));
return OK;
}
void Clearstack (Stack *s)/* Put the stack as empty */
{
(*s)->top= (*s)->base;
(*s)->length=0;
}
Status stackempty (Sqstack S)/* To determine whether the stack is empty * *
{
if (s.top==s.base) return TRUE;
Else
return FALSE;
}
void Push (Stack *s,selemtype e)/* Press the data into the stack * *
{
if ((*s)->top-(*s)->base>= (*s)->stacksize)
{
(*s)->base= (Selemtype *) realloc (*s)->base,
((*s)->stacksize + 2) * sizeof (Selemtype));
if (!) ( *s)->base) exit (-1);
(*s)->top= (*s)->base+ (*s)->stacksize;
(*s)->stacksize + + 2;
}
* ((*s)->top++) =e;
+ + (*s)->length;
}
Status Pop (Stack *s)/* Delete Stack top element * *
{
if ((*s)->top== (*s)->base) return ERROR;
(*s)->top--;
--(*s)->length;
return OK;
}
Status GetTop (Stack s,selemtype *e)/* Return top element of stack * *
{
if (s->top==s->base) return ERROR;
*e=* (s->top-1);
s->top--;
}
void Change (Sqstack s,char *a)/* Pay the elements in the stack in reverse order to a/*
{int n=s.length-1;
while (! Stackempty (S))
GetTop (&s,&a[n--]);
}
void control (Stack *s)
{int i=0,k,j=0;
Selemtype ch,*a;
K=strlen (PASSWORD);
printf ("Enter a password of%d number, you only have 3 chances: \ n", K);
printf ("c:\\>");
for (;;)
{if (i>=3)
{i++;
CLRSCR ();
Gotoxy (1,1); /* Position black screen cursor position * *
Break
}
else if (i>0&&i<3)
{Gotoxy (5,2);
For (j=1;j<= (*s)->length;j++) printf ("");
Gotoxy (5,2); Clearstack (s);}
for (;;)/* Password input, can be backspace * *
{Ch=getch ();/* BACKSPACE ASCII is 8 */
if (ch!=13)/* To determine whether to enter, not to pay it below * * *
{if (ch==8) {Pop (s); Gotoxy (4+j,2);p rintf (""); Gotoxy (4+j,2);}
else {printf ("*"); Push (s,ch);}
j= (*s)->length;}
else break;
}
i++;
if (k!=j) continue;
Else
{a= (Selemtype *) malloc ((*s)->length*sizeof (Selemtype));
Change (**s,a);
For (j=1;j<= (*s)->length;)
{if (a[j-1]==password[j-1]) j + +;
else {j= (*s)->length+2;break}}
if (j== (*s)->length+2) continue;
else break;}} /* The top for end * *
if (i==4) printf ("\ n password error, imminent exit");
else printf ("\ n password is correct \");
Free (a);
}
Main ()
{Stack s;
CLRSCR ()/* Clear Screen * *
Initstack (&s);
Control (&s);
Getch ();
Destroystack (&s);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.