当前位置:网站首页>Bracket matching test
Bracket matching test
2022-07-29 01:15:00 【Ma cute's Ma cute】
1、 Algorithm description
Define in the bracket matching algorithm int flag = 1 Variable to mark whether the matching result is successful or failed !
Using data structure stack , Scan strings from left to right :
If encountering the left parenthesis, put it on the stack ;
If you encounter a closing parenthesis :
If the stack is not empty , Use Pop(s,topElem) The operation gets the top element , Match with the current closing parenthesis . If the current right parenthesis does not match the top element , So modify flag The value of is 0, Matching failure , And print an error message !
If the stack is empty , Then it means that all the currently scanned right parentheses cannot match without the left parenthesis , modify flag by 0, Matching failure , And print an error message !
After scanning the whole string from left to right :
If the stack is empty and flag The value of is 1, That is, the matching is successful !( Because if not flag, that (] This is also a successful match ! So the logic of the algorithm will produce errors !)
Otherwise, the matching fails in other cases !
2、 The specific code is as follows
// Bracket matching algorithm
#include<stdio.h>
#include<stdlib.h> //malloc Function header file
#define MaxSize 20
#include<iostream>
using namespace std;
#define OK 1;
#define ERROR 0;
// Structure definition
typedef struct{
char *data; // The base address of the storage space
int top; // The pointer at the top of the stack records the subscript
}SqStack;
// Initialization stack
void InitStack(SqStack &s){
s.data = (char *) malloc(sizeof(char)*MaxSize);
s.top = -1;
}
// Judge whether the stack is empty
int SqStackIsEmpty(SqStack s) {
if (s.top == -1)
return OK; // The stack is empty
return ERROR; // Stack is not empty.
}
// Into the stack,
void Push(SqStack &s,char c){
if(s.top == MaxSize - 1) // Stack full
printf(" Stack full !\n");
s.top = s.top + 1;
s.data[s.top] = c;
}
// The stack,
void Pop(SqStack &s, char &c){
if(SqStackIsEmpty(s))
printf(" The stack is empty !\n");
c = s.data[s.top];
s.top = s.top - 1;
}
// Parentheses match functions
void bracketCheck(char str[], int length){
SqStack s;
InitStack(s);
int flag = 1; // Whether the flags match successfully
for(int i = 0; i<length;i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
Push(s,str[i]); // Scan from left to right , Run the stack operation when you encounter the left parenthesis
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
// When you encounter the right parenthesis, judge the symbol in the stack
if(!SqStackIsEmpty(s))
{
char topElem;
Pop(s,topElem);
if(str[i] == ')' && topElem != '('){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
if(str[i] == ']' && topElem != '['){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
if(str[i] == '}' && topElem != '{'){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
}
else {
printf("\n Matching failure , The first %d One is the single right bracket %c \n",i+1,str[i]); // The stack is empty , There is a single right parenthesis
flag = 0;
}
}
}
if(SqStackIsEmpty(s) && flag == 1){
printf("\n The match is successful !!!!!!!\n");
}
else printf("\n Matching failure !!!!!!!\n");
}
// The main function
int main(){
char str[MaxSize] = "\0"; // Character array initialization \0
//puts(str);
printf(" Please enter the expression you want to judge :\t");
gets(str);
printf("\n");
bracketCheck(str,MaxSize);
}
3、 Core code
// Parentheses match functions
void bracketCheck(char str[], int length){
SqStack s;
InitStack(s);
int flag = 1; // Whether the flags match successfully
for(int i = 0; i<length;i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
Push(s,str[i]); // Scan from left to right , Run the stack operation when you encounter the left parenthesis
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
// When you encounter the right parenthesis, judge the symbol in the stack
if(!SqStackIsEmpty(s))
{
char topElem;
Pop(s,topElem);
if(str[i] == ')' && topElem != '('){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
if(str[i] == ']' && topElem != '['){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
if(str[i] == '}' && topElem != '{'){
flag = 0;
printf(" Matching failure , The first %d Right parenthesis %c And stack top symbol %c Mismatch \n",i+1,str[i],topElem);
}
}
else {
printf("\n Matching failure , The first %d One is the single right bracket %c \n",i+1,str[i]); // The stack is empty , There is a single right parenthesis
flag = 0;
}
}
}
if(SqStackIsEmpty(s) && flag == 1){
printf("\n The match is successful !!!!!!!\n");
}
else printf("\n Matching failure !!!!!!!\n");
}
If not used c Language , The above stack 、 Various operations such as UN stacking can be used c++、java Directly operate on the customized stack , Simplify the code !
4、 Code demonstration



边栏推荐
- C language bracket matching (stack bracket matching C language)
- 进程和线程知识点总结 2
- 写作作业一
- [notes for question brushing] delete continuous nodes with a total value of zero from the linked list
- Irregular clipping of NC data with CDO
- ActiveMQ基本详解
- [web development] basic knowledge of flask framework
- B+ tree~
- Dart array, map, type judgment, conditional judgment operator, type conversion
- 转:认知亚文化
猜你喜欢

Visual full link log tracking

Self made | a 16 bit RISC architecture CPU is self-made by hand

大页内存原理及使用设置

状态压缩dp-蒙德里安的梦想

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income

Intel带你初识视觉识别--OpenVINO

Day2:三种语言暴刷牛客130题

递归与分治

Interview shock 69: is TCP reliable? Why?
![[raspberry pie] how does the windows computer connect with raspberry pie](/img/d6/42685bbc4e4af757867442b63ce9c8.png)
[raspberry pie] how does the windows computer connect with raspberry pie
随机推荐
🧐 Table1 | finish your third line watch in one second
电子招标初学者指南
mysql存储过程 实现创建一张表(复制原表的结构新建的表)
Seven marketing strategies of NFT project
Interview shock 69: is TCP reliable? Why?
How to deal with the time, scope and cost constraints in the project?
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
Beginner's Guide to electronic bidding
【idea】查询字段使用位置
如何执行建设项目的时间影响分析?
ACM SIGIR 2022 | 美团技术团队精选论文解读
QT静态编译程序(Mingw编译)
[raspberry pie] how does the windows computer connect with raspberry pie
如何在WordPress中创建一个自定义404错误页面
对接支付宝支付
Method of converting inline elements to block elements
Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
ActiveMQ基本详解
Intel带你初识视觉识别--OpenVINO