当前位置:网站首页>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
边栏推荐
- Camera Hal OEM module ---- CMR_ preview.c
- Api 接口优化有哪些技巧?
- Anti shake and throttling
- Upload Excel files with El upload and download the returned files
- Shell programming specifications and variables
- Anomaly detection and unsupervised learning (1)
- 动态规划问题(二)
- Calculate properties and listeners
- 110 MySQL interview questions and answers (continuously updated)
- [development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver
猜你喜欢
![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)
[micro services ~nacos] Nacos service providers and service consumers
![[small bug diary] Navicat failed to connect to MySQL | MySQL service disappeared | mysqld installation failed (this application cannot run on your computer)](/img/ac/f63e370df72ace484a618cf946d4b7.png)
[small bug diary] Navicat failed to connect to MySQL | MySQL service disappeared | mysqld installation failed (this application cannot run on your computer)

Idea connection database

Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接

DCAT in laravel_ Admin preliminary use record

聊聊异步编程的 7 种实现方式

PTA (daily question) 7-69 narcissus number

MySQL事务(transaction) (有这篇就足够了..)

会议OA项目之会议通知&会议反馈&反馈详情功能

Teach you how to install latex (nanny level tutorial)
随机推荐
总结:POD与容器的区别
【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发
2022dasctfjuly empowerment competition (reappearance)
NPM run serve stuck at 40%
Router view cannot be rendered (a very low-level error)
Requestvideoframecallback() simple instance
@PostConstruct注解详解
Application and principle of distributed current limiting redistribution rratelimiter
聊聊异步编程的 7 种实现方式
【MySQL 8】Generated Invisible Primary Keys(GIPK)
多线程顺序运行的几种方法,面试可以随便问
"Food alliance ordering system"
Alibaba Code代码索引技术实践:为Code Review提供本地IDE的阅读体验
17. Design of machine learning system
1331. 数组序号转换 : 简单模拟题
requestVideoFrameCallback() 简单实例
Flyway's quick start tutorial
Kali installs burpsuite professional
Google browser, no installation required
Dynamic programming problem (2)