当前位置:网站首页>括号匹配的检验
括号匹配的检验
2022-07-29 00:04:00 【马可爱家的马可爱】
1、算法描述
在括号匹配算法中定义int flag = 1变量来标记匹配结果是成功还是失败!
利用数据结构栈,从左到右依次扫描字符串:
若是遇到左括号入栈;
若是遇到右括号:
若栈非空,使用Pop(s,topElem)操作得到栈顶元素,与当前的右括号进行匹配。若是当前的右括号与栈顶元素不匹配,那么修改flag的值为0,匹配失败,并且打印错误信息!
若栈为空,则说明当前所有扫描的右括号均无法没有左括号与之匹配,修改flag为0,匹配失败,并且打印错误信息!
从左到右扫描完整个字符串之后:
若栈为空且 flag的值为1,即为匹配成功!(因为若是没有flag,那么(]这种情况也是匹配成功!所以算法的逻辑将会产生错误!)
否则其余情况均是匹配失败!
2、具体代码如下
//括号匹配算法
#include<stdio.h>
#include<stdlib.h> //malloc函数的头文件
#define MaxSize 20
#include<iostream>
using namespace std;
#define OK 1;
#define ERROR 0;
//结构体定义
typedef struct{
char *data; //存储空间的基地址
int top; //栈顶指针记录下标
}SqStack;
//初始化栈
void InitStack(SqStack &s){
s.data = (char *) malloc(sizeof(char)*MaxSize);
s.top = -1;
}
//判断栈是否为空
int SqStackIsEmpty(SqStack s) {
if (s.top == -1)
return OK; //栈空
return ERROR; //栈不为空
}
//入栈操作
void Push(SqStack &s,char c){
if(s.top == MaxSize - 1) //栈满
printf("栈满!\n");
s.top = s.top + 1;
s.data[s.top] = c;
}
//出栈操作
void Pop(SqStack &s, char &c){
if(SqStackIsEmpty(s))
printf("栈空!\n");
c = s.data[s.top];
s.top = s.top - 1;
}
//括号匹配函数
void bracketCheck(char str[], int length){
SqStack s;
InitStack(s);
int flag = 1; //标志是否匹配成功
for(int i = 0; i<length;i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
Push(s,str[i]); //从左到右扫描,遇见左括号就执行入栈操作
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
//遇见右括号判断栈中符号
if(!SqStackIsEmpty(s))
{
char topElem;
Pop(s,topElem);
if(str[i] == ')' && topElem != '('){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
if(str[i] == ']' && topElem != '['){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
if(str[i] == '}' && topElem != '{'){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
}
else {
printf("\n匹配失败,第 %d 个是单身右括号 %c \n",i+1,str[i]); //栈空,存在单身右括号
flag = 0;
}
}
}
if(SqStackIsEmpty(s) && flag == 1){
printf("\n匹配成功!!!!!!!\n");
}
else printf("\n匹配失败!!!!!!!\n");
}
//主函数
int main(){
char str[MaxSize] = "\0"; //字符数组初始化\0
//puts(str);
printf("请输入您要判断的表达式:\t");
gets(str);
printf("\n");
bracketCheck(str,MaxSize);
}
3、核心代码
//括号匹配函数
void bracketCheck(char str[], int length){
SqStack s;
InitStack(s);
int flag = 1; //标志是否匹配成功
for(int i = 0; i<length;i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
Push(s,str[i]); //从左到右扫描,遇见左括号就执行入栈操作
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
//遇见右括号判断栈中符号
if(!SqStackIsEmpty(s))
{
char topElem;
Pop(s,topElem);
if(str[i] == ')' && topElem != '('){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
if(str[i] == ']' && topElem != '['){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
if(str[i] == '}' && topElem != '{'){
flag = 0;
printf("匹配失败,第 %d 个右括号 %c 与栈顶符号 %c 不匹配\n",i+1,str[i],topElem);
}
}
else {
printf("\n匹配失败,第 %d 个是单身右括号 %c \n",i+1,str[i]); //栈空,存在单身右括号
flag = 0;
}
}
}
if(SqStackIsEmpty(s) && flag == 1){
printf("\n匹配成功!!!!!!!\n");
}
else printf("\n匹配失败!!!!!!!\n");
}
如果不使用c语言,上面的入栈、退栈等各种操作可以使用c++、java中自定义好的栈来直接操作,简化代码!
4、代码演示



边栏推荐
- SystemVerilog join and copy operators
- 18张图,直观理解神经网络、流形和拓扑
- What opportunities does the London gold real-time market bring?
- 量化交易之数字货币篇 - 生成foot print因子数据
- Solid smart contract tutorial (5) -nft auction contract
- (完美解决)为什么在train/val/test数据集上用train模式效果都很好,但是在eval模式下全部很差
- Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
- Beginner's Guide to electronic bidding
- 转:认知亚文化
- Visual full link log tracking
猜你喜欢

Summary of process and thread knowledge points 1

DDD领域驱动设计如何进行工程化落地

电子招标初学者指南

图扑软件亮相 2022 福州数博会,携手共创数字新时代

nep 2022 cat

ThinkPHP高仿蓝奏云网盘系统程序

Univariate function integration 1__ Indefinite integral

一元函数积分学之1__不定积分

【idea】查询字段使用位置

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
随机推荐
[Commons lang3 topic] 002 randomutils topic
【mysql】字符串转int
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
写作作业一
【unity】将unity编辑c#配置为vscode
Recursion and divide and conquer
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
mysql存储过程 实现创建一张表(复制原表的结构新建的表)
🧐 Table1 | finish your third line watch in one second
Visual full link log tracking
[Commons lang3 topic] 003- randomstringutils topic
【目标检测】YOLOR理论简介+实践测试VisDrone数据集
Summary of process and thread knowledge points 2
system verilog常用语法
Prometheus 的 API 稳定性保障
[Commons lang3 topic] 001 stringutils topic
电子招标初学者指南
Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
[AD learning] the course of PCB drawing in this marine vehicle competition
Linux Redis 源码安装