📄 check.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
typedef char SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 2
struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
};
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=* --S.top;
return OK;
}
int StackEmpty(SqStack S)
{
return S.top==S.base;
}
void check()
{
SqStack s;
SElemType ch[80],*p,e;
if(InitStack(s))
{
scanf("%s",ch);
p=ch;
while(*p)
{
switch(*p)
{
case '(':
case '[':Push(s,*p++);
break;
case ')':
case ']':if(!StackEmpty(s))
{
Pop(s,e);
if(( *p==')' && e!='(' ) || ( *p==']' && e!='[' ))
{
printf("isn't matched pairs\n");
exit(ERROR);
}
else
{
p++;
break;
}
}
else
{
printf("lack of left parenthesis\n");
exit(ERROR);
}
default:p++;
}
}
if(StackEmpty(s)) printf("matching\n");
else printf("lack of right parenthesis\n");
}
}
int main()
{
check();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -