括号匹配
1.简述
利用栈的思想来实现括号匹配,假设给你一个"{a+c[(a+b)/d]}",判断括号之间是否两两匹配
算法思想:我们可以利用栈来实现,遍历整个字符串,遇到 [ 或 { 或( 就入栈,遇到右括号就出栈 栈顶元素,
判断出栈元素是否和对应右括号匹配,不匹配则匹配失败,匹配成功则接着遍历,遇到右括号则接着
取栈顶元素判断
2.图示
3.代码部分
3.1 栈代码
括号匹配的核心就是利用了栈后进先出的特点,这里我们实现了对栈的一系列的操作,
方便我们后续调用函数,实现起来更加方便
/**
* 括号匹配的实现
* @Date 2024-08-11 10:48
*/
#include <stdio.h>
#define Max 20
typedef struct Stack{
char data[Max];
int top;//栈顶指针
} Stack;
//初始化栈
void InitStack(Stack &stack){
stack.top = -1;
}
//判断栈空
bool isEmpty(Stack &stack){
if(stack.top == -1){
printf("the stack is empty\n");
return true;
}
return false;
}
//判断栈满
bool isFull(Stack &stack){
if (stack.top == Max - 1){
printf("the stack is full");
return true;
}
return false;
}
//入栈
bool Push(Stack &stack,char x){
//判断栈是否满
if (isFull(stack)){
return false;
}
stack.data[++stack.top] = x;
return true;
}
//出栈
bool Pop(Stack &stack){
//判断栈是否为空
if (isEmpty(stack)){
return false;
}
stack.top--;
return true;
}
//获取栈顶元素(只是获取栈顶元素,不必传入引用类型,但是x是带回元素,需用引用传递)
void getTop(Stack stack,char &x){
//判断栈是否为空
if (isEmpty(stack)){
return;
}
x=stack.data[stack.top];
}
3.2 括号匹配的核心代码
//括号匹配的逻辑
bool isMatch(char A[],Stack &s){
for (int i = 0; A[i]!='\0'; i++) {
if (A[i]=='{'||A[i]=='['||A[i]=='('){
//遇到左括号入栈
Push(s,A[i]);
} else if (A[i]=='}'||A[i]==']'||A[i]==')'){
char x;//取出栈顶元素
getTop(s,x);
Pop(s);//出栈
//如果是右括号则获取栈顶元素,看是否与右括号匹配
//三种情况不匹配,直接返回false
if (A[i]=='}' && x!='{'){
printf("no match {}\n");
return false;
}
if(A[i]==']' && x!='['){
printf("no match []\n");
return false;
}
if(A[i]==')' && x!='('){
printf("no match ()\n");
return false;
}
}
}
//如果此时栈不为空,证明有没有匹配的括号,匹配失败
if (s.top != -1){
printf("the stack not empty\n");
return false;
}
return true;
}
3.3 代码测试
int main(){
Stack S;
InitStack(S);
//准备表达式
char A[] = "{a+[(v+d)/d]+d}";
if (isMatch(A,S)){
printf("match success!!!");
} else{
printf("fail!!!");
}
return 0;
}
3.4 测试结果
4.完整代码
/**
* 括号匹配的实现
* @Date 2024-08-11 10:48
*/
#include <stdio.h>
#define Max 20
typedef struct Stack{
char data[Max];
int top;//栈顶指针
} Stack;
//初始化栈
void InitStack(Stack &stack){
stack.top = -1;
}
//判断栈空
bool isEmpty(Stack &stack){
if(stack.top == -1){
printf("the stack is empty\n");
return true;
}
return false;
}
//判断栈满
bool isFull(Stack &stack){
if (stack.top == Max - 1){
printf("the stack is full");
return true;
}
return false;
}
//入栈
bool Push(Stack &stack,char x){
//判断栈是否满
if (isFull(stack)){
return false;
}
stack.data[++stack.top] = x;
return true;
}
//出栈
bool Pop(Stack &stack){
//判断栈是否为空
if (isEmpty(stack)){
return false;
}
stack.top--;
return true;
}
//获取栈顶元素(只是获取栈顶元素,不必传入引用类型,但是x是带回元素,需用引用传递)
void getTop(Stack stack,char &x){
//判断栈是否为空
if (isEmpty(stack)){
return;
}
x=stack.data[stack.top];
}
//括号匹配的逻辑
bool isMatch(char A[],Stack &s){
for (int i = 0; A[i]!='\0'; i++) {
if (A[i]=='{'||A[i]=='['||A[i]=='('){
//遇到左括号入栈
Push(s,A[i]);
} else if (A[i]=='}'||A[i]==']'||A[i]==')'){
char x;//取出栈顶元素
getTop(s,x);
//如果不是说明匹配成功,括号出栈
Pop(s);
//如果是右括号则获取栈顶元素,看是否与右括号匹配
//三种情况不匹配,直接返回false
if (A[i]=='}' && x!='{'){
printf("no match {}\n");
return false;
}
if(A[i]==']' && x!='['){
printf("no match []\n");
return false;
}
if(A[i]==')' && x!='('){
printf("no match ()\n");
return false;
}
}
}
//如果此时栈不为空,证明有没有匹配的括号,匹配失败
if (s.top != -1){
printf("the stack not empty\n");
return false;
}
return true;
}
int main(){
Stack S;
InitStack(S);
//准备表达式
char A[] = "{a+[(v+d)/d]+d}";
if (isMatch(A,S)){
printf("match success!!!");
} else{
printf("fail!!!");
}
return 0;
}
有错误还请大佬指正啊!!!