当前位置:网站首页>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
边栏推荐
- 🧐 Table1 | finish your third line watch in one second
- 正则校验与时间格式化
- [untitled]
- Method of converting inline elements to block elements
- Brushless DC motor controller (how much is the hall charge for changing the motor)
- 【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
- 状态压缩dp-蒙德里安的梦想
- Necessary interview skills for Android (including interview questions and learning materials)
- Oozie工作调度
- Beginner's Guide to electronic bidding
猜你喜欢
随机推荐
Hilbert transform and instantaneous frequency
Summary of process and thread knowledge points 1
[Commons lang3 topic] 004- numberutils topic
【ManageEngine】局域网监控软件是什么,有什么作用
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
C language bracket matching (stack bracket matching C language)
ThinkPHP高仿蓝奏云网盘系统程序
mysql存储过程 实现创建一张表(复制原表的结构新建的表)
State compression DP Mondrian's dream
Cookies and sessions
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
Charles -- 从0-1教你如何使用抓包工具
Consumer unit 消费单元
[ManageEngine] what is the LAN monitoring software and what is its function
Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
(perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
[Commons lang3 topic] 005- objectutils topic
y80.第四章 Prometheus大厂监控体系及实战 -- kube-state-metrics组件介绍和监控扩展(十一)
Self-attention neural architecture search for semantic image segmentation
Interview shock 69: is TCP reliable? Why?