学习一下
如果括号里有其他内容该怎么解,就像有些笔试题把括号当成包装盒,把括号里面的内容当成礼物,然后判断盒子里有没有礼物
可见
支持
嗯嗯
用于学习
用于学习
我要看代码
bangbangbang
请问代码是什么
看一哈
比较比较
有点东西
啊这{:10_269:}{:10_269:}
感谢
来看看源代码!
#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;
}
voidpop(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");
}
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef int Elemtype;
#define ERROR 0
#define OK 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define x 20
typedef struct {
Elemtype *top;
Elemtype *base;
int stacksize;
}Stack;
Stack S;
Status Initstack(Stack S)
{
if(S.base==0)
return 0;
S.base=(Elemtype*)malloc(STACK_INIT_SIZE*sizeof(Elemtype));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 1;
}
Status Pushstack(Stack &S,Elemtype &e)
{
if(S.base==S.top)
return 0;
if(S.top-S.base==S.stacksize)
{
S.base=(Elemtype*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(Elemtype));
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+STACKINCREMENT;
}
*S.top=e;
S.top++;
return 1;
}
Status Popstack(Stack &S,Elemtype e)
{
if(S.base==S.top)
return 0;
e=*S.top;
S.top--;
return 1;
}
int matchstack(char *p)
{
Elemtype e;
while(*p!=0)
{
if(*p=='('||*p=='[')
{
Pushstack(S,e);
}
*(p++);
if(*p==')'||*p==']')
{
Popstack(S,e);
}
*(p++);
}
if(S.base>S.top)
return 0;
else
return 1;
}
int main()
{
char c;
gets(c);
if(matchstack(c)==1)
printf("成功\n");
else
printf("失败\n");
return 0;
}
感谢
nb