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
)
查看全文
相关阅读:
NLP(五)
pyinstaller+wxpython+selinum
C++ 动态库和静态库
谷粒商城(三) 部署
Centos使用KVM创建虚拟机
C++指针
C++异常处理
C++流类库与输入/输出
C++泛型程序设计及STL的结构
selenium java maven testNg环境搭建
原文地址:https://www.cnblogs.com/jjtx/p/2533501.html
最新文章
使用CSS3制作72个webapp图标
纯CSS3画出小黄人并实现动画效果
CSS3实现10种Loading效果
使用CSS3画出一个叮当猫
canvas-简单快速实现知乎登录页背景效果
纯CSS3制作九款可爱复古相机
纯CSS3实现兔斯基简单害羞表情
多栏自适应布局+水平垂直居中+清除浮动——集锦
CSS3弹性伸缩布局(二)——flex布局
CSS3弹性伸缩布局(一)——box布局
热门文章
CSS3实现几个常用的网页小效果
C#调用C++ 动态链接库dll
c#的dllimport使用方法详解(Port API)
二代身份证读卡器二次开发
端口(port)详解
斑马打印机ZT410中文打印
SetWindowLong函数GetWindowLong函数
Delphi中HInstance
Socket套接字
TServerSocket组件
Copyright © 2011-2022 走看看