【输入形式】含括号的算数表达式
【输出形式】1/0
【样例输入】3+(44*[5-{6*[7*(45-10)]}])
【样例输出】1
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
typedef struct
{
char *top;
char *base;
int stacksize;
}SqStack;
void NewStack(SqStack *S)
{
S->base = (SqStack *)malloc(N * sizeof(SqStack));
if(!S->base)
exit(-1);
S->top = S->base;
S->stacksize = N;
}
void Push(SqStack *S, char e)
{
if(S->top - S->base >= S->stacksize)
{
int size;
size =(S->stacksize+N)*sizeof(SqStack);
S->base = (SqStack *)realloc(S->base, size);
if(!S->base)
exit(-1);
S->top = S->base + S->stacksize;
S->stacksize = S->stacksize + N;
}
*S->top++= e;
}
int StackEmpty_Sq(SqStack *s)
{
if(s->base == s->top)
return OK;
else
return ERROR;
}
int StackLength_Sq(SqStack *s)
{
return s->top - s->base;
}
int ClearStack_Sq(SqStack *s)
{
if(s->base != NULL)
s->base = s->top;
return OK;
}
void DestroyStack(SqStack *s)
{
if(s->base != NULL)
{
free(s->base);
s->stacksize = 0;
s->base = s->top = NULL;
}
}
void Pop(SqStack *s)
{
if(s->top == s->base)
return ERROR;
--s->top;
}
int putlength(SqStack *s)
{
int i = 0;
int *base;
base = s->base;
while(s->base<s->top)
{
s->base++;
i++;
}
s->base = base;
return i;
}
void Zhuan(char *ch)
{
if(*ch == ')')
*ch = '(';
if(*ch == ']')
*ch = '[';
if(*ch == '}')
*ch = '{';
}
void Function(SqStack *S, int n)
{
int i = 0;
char *top = NULL, *base = NULL;
base = S->base;
top = S->top;
while(base < top)
{
top--;
Zhuan(top);
if(*top == *S->base)
{
i++;
S->base++;
continue;
}
else
break;
}
if(i == n)
printf("1");
else
printf("0");
}
void puta(SqStack *s)
{
while(s->base<s->top)
{
printf("%c", *s->base);
s->base++;
}
}
int main()
{
SqStack S;
NewStack(&S);
int i = 0;
char ch[100];
gets(ch);
int n = strlen(ch);
int m = 0;
for(i = 0; i < n; i++)
{
if(ch[i]=='('||ch[i] == '{'||ch[i]=='}'||ch[i]==')'||ch[i] == '['||ch[i]==']')
{
Push(&S, ch[i]);
m++;
}
}
Function(&S, m);
return 0;
}