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
)
查看全文
相关阅读:
TClientDataSet[6]: 读取 TClientDataSet 中的图片数据
TClientDataSet[2]: Data、XMLData
TClientDataSet[5]: 读取数据
TClientDataSet[1]: 浏览测试数据
TClientDataSet[3]: 手动建立数据集
从哪查找当前程序所有可用的环境变量?
使用多窗体时, 关于节约内存和加快启动速度的思考与尝试
一句话获取文件的最新修改时间
用 SuperObject 解析淘宝上的 Json 数据 回复 "macrolen" 的问题
“生气”的经典解释
原文地址:https://www.cnblogs.com/jjtx/p/2533501.html
最新文章
[转载]Delphi2010新功能:TDirectory.TFilterPredicate
[转载]Delphi 2010 新增功能:IOUtils单元TFile结构的功能
[转载]Delphi2010新功能:TDirectory.GetFiles 支持通配符
Delphi Project 之工程管理器(Project Manager)
[转载]Delphi 2010 新增功能:IOUtils单元:TPath的方法与属性
[转载]Delphi 2010新功能:IOUtils单元:TDirectory.GetFiles
[转载]delphi2010的几个和当前路径相关的新函数
[转载]delphi2010的几个和当前路径相关的新函数
[转载]Delphi 2010 新功能:IOUtils单元TDirectory的其他功能
[转载]Delphi2010新功能:TDirectory.GetDirectories
热门文章
sql 随机数高效率算法
删除指定行数的数据
记录一次bug。asp.net 编译后 页面一刷新就报错,在刷新就正常。 (vs2005)
SQL Transcation事务回滚等用法
SqlDataReader引起的超时?
怎么删除Temporary ASP.NET Files
转载 DataReader 链接关闭解惑篇
mysql 启动不了 innodb的 一启动就报错的解决方法
SQL IS NOT NULL条件走索引 试验
应用asp.net设置IIS 6的应用程序池(4G内存)
Copyright © 2011-2022 走看看