当前位置:网站首页>C语言括号匹配(栈括号匹配c语言)
C语言括号匹配(栈括号匹配c语言)
2022-07-28 22:52:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
给定一串字符,不超过100个字符,可能包括括号、数字、字母、标点符号、空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配。
输入格式: 输入在一行中给出一行字符串,不超过100个字符,可能包括括号、数字、字母、标点符号、空格。
输出格式: 如果括号配对,输出yes,否则输出no。
输入样例1: sin(10+20) 输出样例1: yes 输入样例2: {[}] 输出样例2: no
思路:题目输入一些字符串,我们就先保留括号之类的,判断是否匹配。如果遇到左括号,就入栈,如果遇到一个右括号,就与栈顶元素比较,如果匹配,出栈,就继续重复操作,直到字符串没有了。期间一旦出现不匹配的括号对就直接输出no ,如果栈空了,说明匹配了,就输出yes。
#include<stdio.h>
#include<string.h>
int left(char c)//判断是否为左括号,是返回1,否返回0.
{
if(c=='('||c=='{'||c=='[')
{
return 1;
}
else
{
return 0;
}
}
int right(char c)//判断是否为右括号,是返回1,否返回0.
{
if(c==')'||c=='}'||c==']')
{
return 1;
}
else
{
return 0;
}
}
int check(char left,char right)//判断是否左右括号匹配,是返回1,否返回0.
{
if(left=='('&&right==')')
{
return 1;
}
if(left=='{'&&right=='}')
{
return 1;
}
if(left=='['&&right==']')
{
return 1;
}
return 0;
}
int main()//主函数。
{
int i=0,l;//定义循环变量i和字符串长度l。
char stack[200];//定义字符数组存放左括号。
int top=0;//初始化栈,栈为空,栈顶top=0;
char s[200];//存放字符串。
gets(s);//输入字符串到s数组中,数组中除了存放了有字符串,末尾还存放了“/0”,以表示是字符串。
l=strlen(s);//字符串长度。
for(i=0;i<l;i++)//遍历每个字符串中的字符。
{
if(left(s[i])==1)//如果是左括号入栈,同时栈顶向上移动。
{
stack[top]=s[i];
top++;
}
else if(right(s[i])==1)//如果是右括号,那么开始在栈中判断是否匹配。
{
if(check(stack[top-1],s[i]))//如果匹配,那么栈顶下移,继续执行下一次新的for循环。
{
top--;
continue;
}
else//如果不匹配,那么输出no,程序返回0,结束。
{
printf("no");
return 0;
}
}
}
//如果不是在for循环中结束,那么就需要判断栈是否为空。因为不是在for循环中结束,说明都匹配成功了,但会出现特殊情况比如((()),令栈不为空。所以是否括号匹配成功不仅要判断是否右括号都有左括号使其匹配,还需要判断栈是否为空。
if(top==0)
{
printf("yes");
return 0;
}
else
{
printf("no");
return 0;
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129214.html原文链接:https://javaforall.cn
边栏推荐
- Api 接口优化有哪些技巧?
- PTA (one question per day) 7-76 ratio
- Shell编程规范与变量
- 还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
- MySQL的存储过程
- Upload Excel files with El upload and download the returned files
- Requestvideoframecallback() simple instance
- 15.模型评估和选择问题
- What are the skills of API interface optimization?
- andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
猜你喜欢

I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
![[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver](/img/06/5e417bb97e309b6ee27dc693cabb85.png)
[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver

Advanced area of attack and defense world web masters supersqli

Anomaly detection and unsupervised learning (1)

PTA (one question per day) 7-76 ratio

Api 接口优化的那些技巧

Laravel permission control

16.偏差、方差、正则化、学习曲线对模型的影响

Statistical analysis of time series

Software designer afternoon question
随机推荐
PTA (daily question) 7-72 calculate the cumulative sum
@Detailed explanation of the use of transactional annotation
Applet waterfall flow, upload pictures, simple use of maps
I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
Simple use and understanding of laravel message queue
Attack and defense world web master advanced area web_ php_ include
Api 接口优化的那些技巧
Alibaba code index technology practice: provide reading experience of local IDE for code review
15. Model evaluation and selection
递归/回溯刷题(下)
“吃货联盟定餐系统”
SurfaceControl和SurfaceFlinger通信
Shell编程规范与变量
Applet verification code login
Surfacecontrol and surfaceflinger communication
MySQL stored procedure
分布式限流 redission RRateLimiter 的使用及原理
2022DASCTF7月赋能赛(复现)
Minimum dominating set (MDS) and its matlab code
[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine