当前位置:网站首页>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
边栏推荐
- 进程和线程知识点总结1
- [raspberry pie] how does the windows computer connect with raspberry pie
- (update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
- Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
- Cookie和Session
- “index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
- How to create a custom 404 error page in WordPress
- [Commons lang3 topic] 004- numberutils topic
- [untitled]
- [Commons lang3 topic] 001 stringutils topic
猜你喜欢

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

【树莓派】widows电脑如何与树莓派连接

The method of tracking the real-time market of London Silver

Wechat campus bathroom reservation applet graduation design finished product (5) assignment

Day2: 130 questions in three languages

用CDO进行nc数据的不规则裁剪

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告

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

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

【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
随机推荐
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
Naver 三方登录
电子招标初学者指南
QT static compiler (MinGW compilation)
Necessary interview skills for Android (including interview questions and learning materials)
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
MySQL stored procedure realizes the creation of a table (copy the structure of the original table and create a new table)
18张图,直观理解神经网络、流形和拓扑
散列表 ~
[Commons lang3 topic] 003- randomstringutils topic
新拟态个人引导页源码
B+ 树 ~
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Summary of process and thread knowledge points 2
量化交易之数字货币篇 - 生成foot print因子数据
[Commons lang3 topic] 005- objectutils topic
System Verilog common syntax
如何在WordPress中创建一个自定义404错误页面
TextKit 自定义UILabel识别链接
Charles -- 从0-1教你如何使用抓包工具