注意gets这个函数不建议使用,用的时候会出现告警;

 第一个code的e类型为int行,主要是因为无论是“(”或是“[”都可以转换为ascll码,第二个code为字符型

View Code
#include <stdio.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2

typedef int SElemType;

struct SqStack{
SElemType *base;
SElemType *top;
int stacksize;
};

#define SqStack struct SqStack

int InitStack(SqStack *S)
{
if ( !(S->base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof(SElemType))))
exit(3);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}

int DestroyStack(SqStack *S)
{
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return 1;

}

int ClearStack(SqStack *S)
{
S->top=S->base;
return 1;
}

int StackEmpty(SqStack *S)
{
if((*S).top == (*S).base )
return 1;
else
return 0;

}

int StackLength(SqStack *S)
{
return S->top - S->base;
}

int GetTop(SqStack *S,SElemType *e)
{
if(S->top>S->base)
{
*e=*(S->top - 1);
return 1;
}else
return 0;
}

int Push(SqStack *S,SElemType e)
{
if ( S->top - S->base >= S->stacksize )
{
S->base=(SElemType *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S->base)
exit(3);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top)++=e;
return 1;
}

int Pop(SqStack *S,SElemType *e)
{
if(S->top == S->base)
return 0;
*e=*--(S->top);
return 1;
}

int StackTraverse(SqStack *S,int (*visit)(SElemType))
{
while(S->top > S->base){
visit(*(S->base)++);
}
printf("\n");
return 1;
}

void check()
{
SqStack s;
SElemType e;
char ch[80],*p;
if(InitStack(&s))
{
printf("please input some express sentence\n");
gets(ch);
p=ch;
// scanf("%s",ch);
// p=ch;
while(*p)
{
switch(*p)
{
case '(':
case '[':Push(&s,*p++);
break; // put '[' into stack,then p++
case ')':
case ']':if (!StackEmpty(&s))
{
Pop(&s,&e);
if(*p==')' && e !='(' || *p == ']' && e != '[')
{
printf("don't match\n");
exit(0);
} else {
p++;
break;
}

} else {
printf("lack the left '(' or '['\n");
exit(0);
}
default:p++;
}
}
if (StackEmpty(&s))
printf("match!\n");
else
printf("lack the right ')' or ']'\n");

}
}

int main(){
check();
}
View Code
#include <stdio.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2

typedef int SElemType;

struct SqStack{
SElemType *base;
SElemType *top;
int stacksize;
};

#define SqStack struct SqStack

int InitStack(SqStack *S)
{
if ( !(S->base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof(SElemType))))
exit(3);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}

int DestroyStack(SqStack *S)
{
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return 1;

}

int ClearStack(SqStack *S)
{
S->top=S->base;
return 1;
}

int StackEmpty(SqStack *S)
{
if((*S).top == (*S).base )
return 1;
else
return 0;

}

int StackLength(SqStack *S)
{
return S->top - S->base;
}

int GetTop(SqStack *S,SElemType *e)
{
if(S->top>S->base)
{
*e=*(S->top - 1);
return 1;
}else
return 0;
}

int Push(SqStack *S,SElemType e)
{
if ( S->top - S->base >= S->stacksize )
{
S->base=(SElemType *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S->base)
exit(3);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top)++=e;
return 1;
}

int Pop(SqStack *S,char *e)
{
if(S->top == S->base)
return 0;
*e=*--(S->top);
return 1;
}

int StackTraverse(SqStack *S,int (*visit)(SElemType))
{
while(S->top > S->base){
visit(*(S->base)++);
}
printf("\n");
return 1;
}

void check()
{
SqStack s;
// SElemType e;
char ch[80],*p,*e;
if(InitStack(&s))
{
printf("please input some express sentence\n");
gets(ch);
p=ch;
// scanf("%s",ch);
// p=ch;
while(*p)
{
switch(*p)
{
case '(':
case '[':Push(&s,*p++);
break; // put '[' into stack,then p++
case ')':
case ']':if (!StackEmpty(&s))
{
Pop(&s,e);
if(*p==')' && *e !='(' || *p == ']' && *e != '[')
{
printf("don't match\n");
exit(0);
} else {
p++;
break;
}

} else {
printf("lack the left '(' or '['\n");
exit(0);
}
default:p++;
}
}
if (StackEmpty(&s))
printf("match!\n");
else
printf("lack the right ')' or ']'\n");

}
}

int main(){
check();
}




posted on 2011-11-25 11:20  hotty  阅读(273)  评论(0)    收藏  举报