C 언어 순서 스 택 에서 사용 하 는 괄호 일치
// :
// :nuaazdh
// :2011 12 5
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define BUFFERSIZE 256
typedef int Status; //
typedef char SElemType; //
typedef struct{//
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S);
// S
Status DestroyStack(SqStack *S);
// S,S
Status ClearStack(SqStack *S);
// S
Status StackEmpty(SqStack S);
// S , TRUE, FALSE
int StackLength(SqStack S);
// S ,
Status GetTop(SqStack S,SElemType *e);
// , e S , OK; FALSE
Status Push(SqStack *S,SElemType e);
// e
Status Pop(SqStack *S,SElemType *e);
// S , S , e , OK, ERROR
Status StackTraverse(const SqStack *S);
//
Status BracketMatch(SqStack *S,const char *string);
// string , TRUE; FALSE
int main()
{
char *string;
SqStack stack;
string=(char*)malloc(sizeof(char)*BUFFERSIZE);
if(!string){
printf(" .
");
exit(0);
}
while(1){
printf(" ( \"!\" ):");
gets(string);
if(string[0]=='!')//
break;
if(TRUE==BracketMatch(&stack,string)){
printf("
.
");
}else{
printf("
.
");
}
}//while
return 0;
}
Status InitStack(SqStack *S){
// S
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base)//
{
printf(" .
");
exit(0);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
Status DestroyStack(SqStack *S){
// S,S
if(!S)//S
{
printf(" , .
");
exit(0);
}
free(S);
return OK;
}
Status ClearStack(SqStack *S){
// S
if(!S)//S
return FALSE;
S->top=S->base;//
return OK;
}
Status StackEmpty(SqStack S){
// S , TRUE, FALSE
if(S.top==S.base)
return TRUE;
else
return FALSE;
}
int StackLength(SqStack S){
// S ,
return S.stacksize;
}
Status GetTop(SqStack S,SElemType *e){
// , e S , OK; FALSE
if(S.top==S.base){
//printf(" .
");
return FALSE;
}else{
*e=*(S.top-1);
//printf("%c",*e);
return OK;
}
}
Status Push(SqStack *S,SElemType e){
// e
if(S->top-S->base>=S->stacksize){// ,
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base)
{
printf(" .
");
exit(0);
}
S->top=S->base+S->stacksize;//
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
Status Pop(SqStack *S,SElemType *e){
// S , S , e , OK, ERROR
if(S->top==S->base){//
printf(" .
");
return ERROR;
}
*e=*(--S->top);
return OK;
}
Status StackTraverse(const SqStack *S){
//
SElemType *p=S->base;
if(S->base==S->top)
{
printf(" .
");
return FALSE;
}
//printf(" :");
while(p!=S->top)
{
printf("%c",*p++);
}
printf("
");
return OK;
}
Status BracketMatch(SqStack *S,const char *string){
// string , TRUE; FALSE
const char *p=string;
SElemType e;
InitStack(S);
while(*p!='\0'){//
switch(*p){// p
case '(':// ,
case '[':
case '{':
Push(S,*p);
//printf("Push %c",*p);
break;
case ')':
if(FALSE==GetTop(*S,&e))
return FALSE;
if(e=='('){
if(ERROR==Pop(S,&e))
return FALSE;
//printf("Push %c",*p);
}else
return FALSE;
break;
case ']':
if(FALSE==GetTop(*S,&e))
return FALSE;
if(e=='['){
if(ERROR==Pop(S,&e))
return FALSE;
//printf("Push %c",*p);
}else
return FALSE;
break;
case '}':
if(FALSE==GetTop(*S,&e))
return FALSE;
if(e=='{'){
if(ERROR==Pop(S,&e))
return FALSE;
//printf("Push %c",*p);
}else
return FALSE;
break;
default:
;
}//switch
p++;
}//while
if(!StackEmpty(*S))// , ,
return FALSE;
return TRUE;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.