|
发表于 2020-10-22 02:02:00
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#define stack_init_size 40
typedef struct stack
{
char* base;
char* top;
int stacksize;
}sqstack;
void initstack(sqstack* s)
{
s->base = (char*)malloc(stack_init_size * sizeof(sqstack));
if (!s->base)exit(0);
s->top = s->base;
s->stacksize = stack_init_size;
}
void push(sqstack* s, char e)
{
if (!s->base)exit(0);
s->top = s->base + s->stacksize;
s->stacksize += 10;
*s->top++ = e;
}
void pop(sqstack*s, char* e)
{
if (s->top == s->base) return 0;
e = *--s->top;
}
int main()
{
int* base;
int* top;
int stacksize;
sqstack s;
initstack(&s);
char e = {0};
int count = 0;
printf("input:");
scanf_s("%c", &e,1);
if (e != '\n' || e !=')' || e != ']' || e != '}')//第一个字符的判断
{
push(&s, e);
}
else return 0;
while (e != '\n')
{
scanf_s("%c", &e, 1);
char* p;
p = s.top - 1;
switch (e)
{
case')':
if (*p == '[' || *p == '}')
{
printf("0");
}
break;
if (*(p - 1) == '(')
{
pop(&s, &e);
}
continue;
case']':
if (*p == '(' || *p == '}')
{
printf("0");
}
break;
if (*(p - 1) == '[')
{
pop(&s, &e);
}
continue;
case'}':
if (*p == '(' || *p == '[')
{
printf("0");
}
break;
if (*(p - 1) == '{')
{
pop(&s, &e);
}
continue;
case '(':
push(&s, &e);
continue;
case '[':
push(&s, &e);
continue;
case '{':
push(&s, e);
continue;
}
}
if (s.top == s.base)
printf("1");
else
printf("0");
} |
|