当前位置:网站首页>C language bracket matching (stack bracket matching C language)
C language bracket matching (stack bracket matching C language)
2022-07-29 01:12:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Given a string of characters , No more than 100 Characters , May include parentheses 、 Numbers 、 Letter 、 Punctuation 、 Space , Program to check the... In this string of characters ( ) ,[ ],{ } match .
Input format : The input gives a line of string in one line , No more than 100 Characters , May include parentheses 、 Numbers 、 Letter 、 Punctuation 、 Space .
Output format : If parentheses are paired , Output yes, Otherwise output no.
sample input 1: sin(10+20) sample output 1: yes sample input 2: {[}] sample output 2: no
Ideas : Enter some strings for the title , Let's keep parentheses first , Judge whether it matches . If you encounter the left bracket , It into the stack , If you encounter a closing bracket , Compare it with the top element , If the match , Out of the stack , Continue to repeat the operation , Until the string is gone . In case of mismatched bracket pairs during the period, it will be output directly no , If the stack is empty , Description matches , It outputs yes.
#include<stdio.h>
#include<string.h>
int left(char c)// Determine whether it is an open bracket , Is to return 1, No return 0.
{
if(c=='('||c=='{'||c=='[')
{
return 1;
}
else
{
return 0;
}
}
int right(char c)// Determine whether it is a right parenthesis , Is to return 1, No return 0.
{
if(c==')'||c=='}'||c==']')
{
return 1;
}
else
{
return 0;
}
}
int check(char left,char right)// Determine whether the left and right parentheses match , Is to return 1, No return 0.
{
if(left=='('&&right==')')
{
return 1;
}
if(left=='{'&&right=='}')
{
return 1;
}
if(left=='['&&right==']')
{
return 1;
}
return 0;
}
int main()// The main function .
{
int i=0,l;// Define loop variables i And string length l.
char stack[200];// Define the character array to store the left parenthesis .
int top=0;// Initialization stack , The stack is empty. , To the top of the stack top=0;
char s[200];// Store string .
gets(s);// Enter a string to s Array , In addition to storing strings in the array , It is also stored at the end “/0”, Is a string .
l=strlen(s);// String length .
for(i=0;i<l;i++)// Traverse the characters in each string .
{
if(left(s[i])==1)// If it is an open bracket, put it on the stack , At the same time, the top of the stack moves upward .
{
stack[top]=s[i];
top++;
}
else if(right(s[i])==1)// If it's right bracket , Then start judging whether it matches in the stack .
{
if(check(stack[top-1],s[i]))// If the match , Then move the top of the stack down , Continue with the next new for loop .
{
top--;
continue;
}
else// If it doesn't match , Then output no, Program return 0, end .
{
printf("no");
return 0;
}
}
}
// If not in for End of cycle , Then you need to judge whether the stack is empty . Because it's not for End of cycle , The description is matched successfully , But there will be special situations, such as ((()), Make the stack not empty . Therefore, whether the brackets match successfully depends not only on whether the right bracket has the left bracket to match , You also need to determine whether the stack is empty .
if(top==0)
{
printf("yes");
return 0;
}
else
{
printf("no");
return 0;
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/129214.html Link to the original text :https://javaforall.cn
边栏推荐
- 【mysql】字符串转int
- How to explain JS' bind simulation implementation to your girlfriend
- The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
- 数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
- 转:认知亚文化
- y80.第四章 Prometheus大厂监控体系及实战 -- kube-state-metrics组件介绍和监控扩展(十一)
- 状态压缩dp-蒙德里安的梦想
- Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
- Django使用MySQL数据库已经存在的数据表方法
- What are the methods to track the real-time market of London Silver?
猜你喜欢

QT静态编译程序(Mingw编译)

Charles -- teach you how to use the packet capturing tool from 0-1

New pseudo personal guide page source code

Wechat campus bathroom reservation applet graduation design finished product (5) assignment
![[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

散列表 ~

Method of converting inline elements to block elements

How to deal with the time, scope and cost constraints in the project?

Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation

Hash table~
随机推荐
APP接入Kakaotalk三方登录
How to create a custom 404 error page in WordPress
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
ActiveMQ basic details
Hilbert 变换与瞬时频率
[Commons lang3 topic] 005- objectutils topic
正则校验与时间格式化
Definition of double linked list~
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
【mysql】多指标历史累计去重问题
表达式求值
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
[Commons lang3 topic] 001 stringutils topic
[raspberry pie] how does the windows computer connect with raspberry pie
[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks
🧐 Table1 | finish your third line watch in one second
Return the member function of *this
🧐 table1 | 一秒搞定你的三线表
Consumer unit