您的位置:逆风者 数据结构 正文
原作者:www.upwinder.com 添加时间:2007-09-02 原文发表:2007-08-31 人气:22 来源:未知

#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"; /*密码,全局变量*/
typedef char SElemType;
typedef struct STACK /*定义栈类型*/
{
SElemType *base;
SElemType *top;
int stacksize;
int length;
}SqStack,*Stack;
typedef int Status;
void InitStack(Stack *S) /*初始化栈*/
{
*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) /* 销毁栈*/
{
free((*S)->base);
free((*S));
return OK;
}
void ClearStack(Stack *S) /*把栈置为空*/
{
(*S)->top=(*S)->base;
(*S)->length=0;
}
Status StackEmpty(SqStack S) /*判断栈空否*/
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
void Push(Stack *S,SElemType e) /*把数据压入栈*/
{
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) /*删除栈顶元素*/
{
if((*S)->top==(*S)->base) return ERROR;
(*S)->top--;
--(*S)->length;
return OK;
}
Status GetTop(Stack S,SElemType *e)/*返回栈顶元素*/
{
if(S->top==S->base) return ERROR;
*e=*(S->top-1);
S->top--;
}
void Change(SqStack S,char *a) /*将栈中的元素按反序付给 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("输入一个 %d 个数的密码,你只有 3 次机会:\n",k);
printf("C:\\>");
for(;;)
{ if(i>=3)
{ i ;
clrscr();
gotoxy(1,1); /*定位黑屏光标位置*/
break;
}
else if(i>0&&i<3)
{ gotoxy(5,2);
for(j=1;j<=(*s)->length;j )printf(" ");
gotoxy(5,2);ClearStack(s);}
for(;;) /* 密码输入,可退格 */
{ch=getch(); /* 退格 的ASCII 是8 */
if(ch!=13) /* 判断是否为回车,不是则把它付给下面*/
{if(ch==8) {Pop(s);gotoxy(4 j,2);printf(" ");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;}}/*最上面的for完*/
if(i==4) printf("\n密码错误,即将退出");
else printf("\n密码正确\n");
free(a);
}
main()
{Stack s;
clrscr();/*清屏*/
InitStack(&s);
Control(&s);
getch();
DestroyStack(&s);
}

相关文章

链表基本操作的程序实现
关于文件管理系统的数据结构模拟
数据结构教程 第二十课 广义表
数据结构教程 第三十六课 选择排序,归并排
线索二叉树算法
数据结构--序言
数据结构教程 第十课 栈的表示与实现
数据结构教程 第二十六课 图的定义与术语
排序及查找方法
数据结构教程 第十六课 串操作应用举例
数据结构教程 第三十课 静态查找表(二)有
有向图转换
JAVA编写的拼图游戏移动算法,简单易懂
数据结构教程 第四十课 总复习
无向图转换
简单的行编辑器
数据结构教程 第五课 线性表的类型定义
Huffman编码生成程序
数据结构教程 第二十五课 单元测验

相关评论


本文章所属分类:首页 数据结构

  热门关键字: