zoukankan
html css js c++ java
括号匹配(栈实现)
#include <cstdio> #include <iostream> using namespace std; #define MAXSIZE 20 typedef struct { char *base; char *top; int stacksize; }SqStack; void InitStack(SqStack &S) { S.base = (char *)malloc( MAXSIZE * sizeof(char) ); if(S.base == NULL) exit(-2); S.top = S.base; S.stacksize = MAXSIZE; } void GetTop(SqStack S, char &e) { if(S.top == S.base) return; e = *(S.top - 1); } void Push(SqStack &S, char e) // 不考虑栈满 { *S.top++ = e; } void Pop(SqStack &S, char &e) { if(S.top == S.base) return; S.top--; e = *S.top; } bool Match(char c, SqStack &my_stack, bool &tag) { char e; Pop(my_stack, e); if ( c != e ) { tag = false; free(my_stack.base); return false; // match fail } return true; // match success } void Correct(char *expr, bool &tag) { tag = true; SqStack my_stack; InitStack (my_stack); for( int i = 0; expr[i] != '\0'; i++ ) { char c = expr[i]; switch(c) { case '{' : case '[' : case '(' : Push (my_stack, c); break; case '}' : if( Match('{', my_stack, tag) == false ) // match fail return; break; case ']' : if( Match('[', my_stack, tag) == false ) // match fail return; break; case ')' : if( Match('(', my_stack, tag) == false ) // match fail return; break; default : break; // 其它字符 } } if(my_stack.top != my_stack.base) // e.g.: "[r" tag = false; free(my_stack.base); } int main(void) { // freopen("cin.txt", "r", stdin); char my_expr[MAXSIZE]; while(cin >> my_expr) { bool tag = true; Correct( my_expr, tag); tag ? printf("匹配成功\n") : printf("匹配失败\n"); } return 0; }
(另见
http://blog.csdn.net/justme0/article/details/7424798
)
查看全文
相关阅读:
CUDA编程学习笔记2
CUDA编程学习笔记1
论文阅读 <Relocalization, Global Optimization and Map Merging for Monocular Visual-Inertial SLAM>
Adding Cues (线索、提示) to Binary Feature Descriptors for Visual Place Recognition 论文阅读
Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras 论文摘要
CUDA C编程入门
PatchMatchStereo可能会需要的Rectification
PatchMatch小详解
PatchMatch Stereo
PatchMatch笔记
原文地址:https://www.cnblogs.com/jjtx/p/2533501.html
最新文章
Frotinet60D IP映射 開放指定端口是實際應用
Qnap 中VM下的win7
VMware安装黑群暉5.2
Windows10系統修復
windows 10
windows10 開機失敗,且按F8無法進入安全模式
centos 複製時顯示進度的指令 pv
封鎖指定網站的簡易方法
outlook 2010 自動密送Email
centos 7 安装官方LAMP(Apache+PHP5+MySQL)
热门文章
使用Epplus以及Excel将模板导出excel
CentOS 停止yum命令
使用SQL命令的in¬ in等删除数据的总结
mysql中删除数据的错误:You can't specify target table 'table_name' for update in FROM clause
试下C# 8.0 的switch表达式 (VS2019)
mysql版本不一致,导致导入sql数据失败
github 的22端口无法连接的解决办法
解决使用git,ssh每次都要输入密码
C# 创建一个List<int>类型(int是连续很多个的)的最简单的方法
VS2017快捷键大全
Copyright © 2011-2022 走看看