错误的括号代码
2015-10-09 09:15
423 查看
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top; //栈指针
} SqStack; //顺序栈类型定义
void InitStack(SqStack *&s); //初始化栈
void DestroyStack(SqStack *&s); //销毁栈
bool StackEmpty(SqStack *s); //栈是否为空
int StackLength(SqStack *s); //返回栈中元素个数——栈长度
bool Push(SqStack *&s,ElemType e); //入栈
bool Pop(SqStack *&s,ElemType &e); //出栈
bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素
void DispStack(SqStack *s); //输出栈
bool Match(char exp[]);
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
int StackLength(SqStack *s) //返回栈中元素个数——栈长度
{
return(s->top+1);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if (s->top==MaxSize-1) //栈满的情况,即栈上溢出
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
return true;
}
void DispStack(SqStack *s) //输出栈
{
int i;
for (i=s->top;i>=0;i--)
printf("%c ",s->data[i]);
printf("\n");
}
bool Match(char exp[])
{
int i;
char e;
bool match=true;
SqStack *st;
InitStack(st);
for(i=0; exp[i]!='\0'&&match; i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{')
Push(st,exp[i]);
else if(exp[i]==')'||exp[i]==']'||exp[i]=='}')
{
if(GetTop(st,e)==true)
{
if(exp[i]=='('&&e=='(')
{
match=true;
Pop(st,e);
}
else match=false;
if(exp[i]=='['&&e=='[')
{
match=true;
Pop(st,e);
}
else match=false;
if(exp[i]=='{'&&e=='{')
{
match=true;
Pop(st,e);
}
else match=false;
}
}
}
if(!StackEmpty(st))
match=false;
DestroyStack(st);
return match;
}
int main()
{
char exp[50];
printf("请输入表达式:");
scanf("%exp", exp);
if(Match(exp))
printf("配对正确!!\n");
else
printf("配对错误!!\n");
return 0;
}
![](https://img-blog.csdn.net/20151009091515458)
错误之处:
![](https://img-blog.csdn.net/20151009091839989)
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top; //栈指针
} SqStack; //顺序栈类型定义
void InitStack(SqStack *&s); //初始化栈
void DestroyStack(SqStack *&s); //销毁栈
bool StackEmpty(SqStack *s); //栈是否为空
int StackLength(SqStack *s); //返回栈中元素个数——栈长度
bool Push(SqStack *&s,ElemType e); //入栈
bool Pop(SqStack *&s,ElemType &e); //出栈
bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素
void DispStack(SqStack *s); //输出栈
bool Match(char exp[]);
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
int StackLength(SqStack *s) //返回栈中元素个数——栈长度
{
return(s->top+1);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if (s->top==MaxSize-1) //栈满的情况,即栈上溢出
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
return true;
}
void DispStack(SqStack *s) //输出栈
{
int i;
for (i=s->top;i>=0;i--)
printf("%c ",s->data[i]);
printf("\n");
}
bool Match(char exp[])
{
int i;
char e;
bool match=true;
SqStack *st;
InitStack(st);
for(i=0; exp[i]!='\0'&&match; i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{')
Push(st,exp[i]);
else if(exp[i]==')'||exp[i]==']'||exp[i]=='}')
{
if(GetTop(st,e)==true)
{
if(exp[i]=='('&&e=='(')
{
match=true;
Pop(st,e);
}
else match=false;
if(exp[i]=='['&&e=='[')
{
match=true;
Pop(st,e);
}
else match=false;
if(exp[i]=='{'&&e=='{')
{
match=true;
Pop(st,e);
}
else match=false;
}
}
}
if(!StackEmpty(st))
match=false;
DestroyStack(st);
return match;
}
int main()
{
char exp[50];
printf("请输入表达式:");
scanf("%exp", exp);
if(Match(exp))
printf("配对正确!!\n");
else
printf("配对错误!!\n");
return 0;
}
错误之处:
相关文章推荐
- 【译】依赖注入:代码的一针强心剂
- java的数据类型以及对应得取值范围
- 【第6周 项目3 - 括号的匹配】
- Java基础知识强化之IO流笔记31:转换流出现的原因和格式
- qt+vs 系统找不到指定的路径
- Spring AOP原理及简单应用
- NetBeans IDE 8.0.2 单选按钮的使用
- Lua高级教程Metatables
- python实现的各种排序算法代码
- java强软弱虚引用详解(转载)
- Java的绘图模式使用浅析
- PHP中$_SERVER的详细参数与说明
- 2015-10-9 【项目 - 猴子选大王】
- C# 之 获取文件名及拓展名
- Java实现操作excel表格
- 【第6周 项目2 - 建立链栈算法库】
- C# 之 日常积累(二)
- Spring如何通过注解方式注入静态属性
- VS2010+2.4.11的persistence.cpp line 5008 错误
- 第六周—项目3 - 括号的匹配