栈 括号匹配
来源:互联网 发布:手机淘宝用户怎么注册 编辑:程序博客网 时间:2024/06/11 09:58
#include <cstdio>
#include <stdlib.h>
#define Stack_size 100
typedef char ElemType;
typedef struct Node
{
ElemType elem[Stack_size];
int top;
} Seqstack;
void Initstack (Seqstack *s)
{
s->top=-1;
}
int Push(Seqstack *s, ElemType x) //将元素x入栈
{
if(s->top==Stack_size-1) return false;
s->top++;
s->elem[s->top]=x;
return true;
}
#include <stdlib.h>
#define Stack_size 100
typedef char ElemType;
typedef struct Node
{
ElemType elem[Stack_size];
int top;
} Seqstack;
void Initstack (Seqstack *s)
{
s->top=-1;
}
int Push(Seqstack *s, ElemType x) //将元素x入栈
{
if(s->top==Stack_size-1) return false;
s->top++;
s->elem[s->top]=x;
return true;
}
int Pop(Seqstack *s, ElemType *x) //将栈顶元素出栈 通过x带出
{
if(s->top==-1) return false;
else{
*x=s->elem[s->top];
s->top--;
return true;
}
}
{
if(s->top==-1) return false;
else{
*x=s->elem[s->top];
s->top--;
return true;
}
}
int Gettop(Seqstack *s, ElemType *x) //读出栈顶元素 通过x带出
{
if(s->top==-1) return false;
else{
*x=s->elem[s->top];
return true;
}
}
{
if(s->top==-1) return false;
else{
*x=s->elem[s->top];
return true;
}
}
int Isempty(Seqstack *s) //判断栈是否为空 空的话返回1 非空返回0
{
if(s->top==-1) return true;
else return false;
}
{
if(s->top==-1) return true;
else return false;
}
int IsFull(Seqstack *s ) //判断栈是否满了 满的话返回1 不满返回0
{
if(s->top==Stack_size-1) return true;
else return false;
}
{
if(s->top==Stack_size-1) return true;
else return false;
}
int Match(char a,char b)
{
if(a=='(' && b==')') return 1;
if(a=='{' && b=='}') return 1;
if(a=='[' && b==']') return 1;
return 0;
}
void BracketMatch(char *str )
{
Seqstack s;char ch;
Initstack(&s);
for(int i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '(':
case '[':
case '{':
Push(&s,str[i]);
break;
case ')':
case ']':
case '}':
if(Isempty(& s)) {
printf("匹配不成功\n");
return ;
}
else{
Gettop(&s,&ch);
if(Match(ch,str[i])) Pop(&s,&ch);
else {
printf("匹配不成功\n");
return;
}
}
}
}
if(Isempty(&s)) printf("匹配成功\n");
else printf("左括号多余\n");
}
int main()
{
char str[20];
printf("请输入要匹配的括号\n");
scanf("%s",&str);
BracketMatch(str);
return 0;
}
{
if(a=='(' && b==')') return 1;
if(a=='{' && b=='}') return 1;
if(a=='[' && b==']') return 1;
return 0;
}
void BracketMatch(char *str )
{
Seqstack s;char ch;
Initstack(&s);
for(int i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '(':
case '[':
case '{':
Push(&s,str[i]);
break;
case ')':
case ']':
case '}':
if(Isempty(& s)) {
printf("匹配不成功\n");
return ;
}
else{
Gettop(&s,&ch);
if(Match(ch,str[i])) Pop(&s,&ch);
else {
printf("匹配不成功\n");
return;
}
}
}
}
if(Isempty(&s)) printf("匹配成功\n");
else printf("左括号多余\n");
}
int main()
{
char str[20];
printf("请输入要匹配的括号\n");
scanf("%s",&str);
BracketMatch(str);
return 0;
}
阅读全文
0 0
- 栈---括号匹配
- 括号匹配(栈)
- [栈] 括号匹配问题
- 栈实例-括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 栈之括号匹配
- 括号匹配(栈)
- 栈-括号匹配
- 括号匹配(栈)
- 栈&&括号匹配
- uva673 栈 括号匹配
- 栈实现括号匹配
- E-栈--括号匹配
- 括号匹配(栈)
- 栈 括号匹配程序
- 栈--括号匹配
- git提示error setting certificate verify locations解决办法
- 负数的二进制表现形式
- NFS服务器的安装与配置
- 【模拟】HDU6147[Pokémon GO II]题解
- 2017暑期训练总结
- 栈 括号匹配
- 容器及配置那些事
- 常用网站
- Apache&AWstats分析系统部署
- Mysql报错注入原理分析(count()、rand()、group by)
- es6(3)
- The form bound to the class does not have a valid binding for the root component
- 二叉树-创建和遍历
- YOLO v2