当前位置:网站首页>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



边栏推荐
- Principle and usage setting of large page memory
- 对接支付宝支付
- 进程和线程知识点总结 2
- Wechat campus bathroom reservation applet graduation design finished product (5) assignment
- 正则校验与时间格式化
- Beginner's Guide to electronic bidding
- Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
- Brushless DC motor controller (how much is the hall charge for changing the motor)
- system verilog常用语法
- Flask project architecture (First Edition
猜你喜欢

Interview shock 69: is TCP reliable? Why?

This article enables you to understand the underlying principle of MySQL- Internal structure, index, lock, cluster

如何处理项目中的时间、范围和成本限制?

Talk about the cross end technical scheme

Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report

【idea】查询字段使用位置

表达式求值

ACM SIGIR 2022 | 美团技术团队精选论文解读

18张图,直观理解神经网络、流形和拓扑

Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
随机推荐
Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
可视化全链路日志追踪
regular expression
APP接入Kakaotalk三方登录
Consumer unit 消费单元
How to create a custom 404 error page in WordPress
Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)
MySQL stored procedure realizes the creation of a table (copy the structure of the original table and create a new table)
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
Transfer: cognitive subculture
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
ACM SIGIR 2022 | 美团技术团队精选论文解读
Return the member function of *this
Charles -- 从0-1教你如何使用抓包工具
C language bracket matching (stack bracket matching C language)
[notes for question brushing] binary linked list to integer
SystemVerilog-连接和复制运算符
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Cookies and sessions
消费行业数字化升级成“刚需”,weiit新零售SaaS为企业赋能!