判断表达式括号是否匹配,C语言堆栈实现
2017-11-27 16:48
597 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct Sq{
char *base;
char *top;
int stacksize;
}SqStack;
void InitStack(SqStack *s)
{
s->base =(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base )
exit(0);
s->top =s->base ;
s->stacksize =STACK_INIT_SIZE;
}
void Push(SqStack *s,char e)
{
if(s->top -s->base ==s->stacksize )
s->base =(char *)realloc(s->base ,(s->stacksize +STACKINCREMENT)*sizeof(char));
if(!s->base )
exit(0);
s->stacksize +=STACKINCREMENT;
*(s->top )=e;
s->top ++;
}
void GetTop(SqStack *s,char *e)
{
if(s->top ==s->base )
exit(0);
*e=*(s->top -1);
}
void Pop(SqStack *s)
{
if(s->top ==s->base )
exit(0);
s->top --;
}
int main()
{
SqStack *s;
s=(SqStack *)malloc(sizeof(SqStack));
InitStack(s);
int flag=1;
int i=0;
char str[128];
gets(str);
char *e=(char *)malloc(sizeof(char));
while(str[i]!='\0'&&flag)
{
switch(str[i])
{
case '[':Push(s,str[i]);break;
case '{':Push(s,str[i]);break;
case '(':Push(s,str[i]);break;
case ']':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='[')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
case ')':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='(')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
case '}':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='{')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
default:break;
}
i++;
}
if(s->base ==s->top )
flag=0;
if(flag==1)
printf("YES!\n");
else printf("NO!\n");
return 0;
}
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct Sq{
char *base;
char *top;
int stacksize;
}SqStack;
void InitStack(SqStack *s)
{
s->base =(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base )
exit(0);
s->top =s->base ;
s->stacksize =STACK_INIT_SIZE;
}
void Push(SqStack *s,char e)
{
if(s->top -s->base ==s->stacksize )
s->base =(char *)realloc(s->base ,(s->stacksize +STACKINCREMENT)*sizeof(char));
if(!s->base )
exit(0);
s->stacksize +=STACKINCREMENT;
*(s->top )=e;
s->top ++;
}
void GetTop(SqStack *s,char *e)
{
if(s->top ==s->base )
exit(0);
*e=*(s->top -1);
}
void Pop(SqStack *s)
{
if(s->top ==s->base )
exit(0);
s->top --;
}
int main()
{
SqStack *s;
s=(SqStack *)malloc(sizeof(SqStack));
InitStack(s);
int flag=1;
int i=0;
char str[128];
gets(str);
char *e=(char *)malloc(sizeof(char));
while(str[i]!='\0'&&flag)
{
switch(str[i])
{
case '[':Push(s,str[i]);break;
case '{':Push(s,str[i]);break;
case '(':Push(s,str[i]);break;
case ']':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='[')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
case ')':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='(')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
case '}':if(s->top !=s->base )
{
GetTop(s,e);
if(*e=='{')
{
Pop(s);
flag=1;
break;
}
else flag=0; break;
}
else flag=0;break;
default:break;
}
i++;
}
if(s->base ==s->top )
flag=0;
if(flag==1)
printf("YES!\n");
else printf("NO!\n");
return 0;
}
相关文章推荐
- (容器配接器)堆栈应用:判断表达式中的括号是否匹配
- 用堆栈实现给定数学表达式括号是否匹配 推荐
- 常见算法题:判断表达式括号是否匹配
- 用栈判断表达式中的括号是否匹配
- 华为2014机考题目_判断if括号匹配是否合法_堆栈_简单的方法- -
- 判断表达式括号是否匹配java和js版本
- 如何判断一个表达式中的左右括号是否匹配
- 括号成对出现、判断括号是否匹配(不用堆栈)
- 判断表达式左右括号是否匹配(栈)
- [置顶] 【C语言】判断花括号{}是否匹配
- 括号成对出现、判断括号是否匹配(不用堆栈)
- 判断表达式中的括号是否匹配
- C语言实现括号匹配,中缀表达式转后缀表达式并计算的算法
- 数据结构算法——判断表达式中的括号是否匹配
- 判断字符串中的括号是否匹配-C语言
- 简单判断表达式括号是否匹配
- 华为2014机考题_判断if括号是否匹配_堆栈
- 栈的基本操作及如何判断一个表达式中的左右括号是否匹配
- 【C语言】判断花括号{}是否匹配
- 利用栈判断输入的表达式中的括号是否匹配(假设只含有左、右括号)