当前位置:网站首页>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
边栏推荐
- MySQL stored procedure
- 手把手教你安装Latex(保姆级教程)
- [network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
- Introduction of shortest path tree (SPT) and matlab code
- Software designer afternoon question
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- Shell programming specifications and variables
- How to learn R language
- Some operations of Ubuntu remote server configuration database (unable to locate package MySQL server, steps of installing mysql, unable to enter password when logging in MySQL)
- Recursion / backtracking (Part 2)
猜你喜欢

How to solve the problems of MQ message loss, duplication and backlog?

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

Talk about seven ways to realize asynchronous programming

vulnhub:Sar

PTA (daily question) 7-73 turning triangle

Router view cannot be rendered (a very low-level error)

IDEA 连接 数据库

Basic knowledge of PHP language (super detailed)

“吃货联盟定餐系统”

Flyway's quick start tutorial
随机推荐
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
SAP vl02n delivery note posting function WS_ DELIVERY_ UPDATE
Common sparse basis and matlab code for compressed sensing
rk3399 9.0驱动添加Powser按键
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Breadth first search (BFS) and its matlab code
Soft test --- database (4) SQL statement
CDN mode uses vant components, and components cannot be called after they are introduced
Nftscan and nftplay have reached strategic cooperation in the field of NFT data
Shell programming specifications and variables
Oracle实例无法启动的问题如何解决
Use hutool tool class to operate excel with more empty Sheet1
SAP VL02N 交货单过账函数 WS_DELIVERY_UPDATE
17. Design of machine learning system
How to solve the problems of MQ message loss, duplication and backlog?
1331. 数组序号转换 : 简单模拟题
面试被问到了String相关的几道题,你能答上来吗?
刷题的第三十天
pnpm的安装与使用
Attack and defense world web master advanced area web_ php_ include