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
)
查看全文
相关阅读:
最长公共子序列
学习MySQL常用操作命令
using的几种用法
C++循环的简单使用【闲来写来练练手~】
使用【数据库收缩功能】实现多个数据文件的合并
Google的十个核心技术(摘自CSDN)
OPENGL入门学习
dive into python 第 5 章 对象和面向对象
[转]已知两圆圆心坐标及半径求两圆交点 (C语言|参数方程求解)
The Python Tutorial 4. More Control Flow Tools的一些小记
原文地址:https://www.cnblogs.com/jjtx/p/2533501.html
最新文章
cmake出现的两个错误的解决办法
常用架构(转载)
推荐:CCTV纪录片[见证·影像志]足球是圆的
新蛋购物经历
转载:资深设计师的29条忠告
weekly review 200913: Normal
转载:Life & The Interview With God
weekly review 200915: Study?
预防猪流感
weekly review 200917: Labour Day
热门文章
weekly review 200914: ReSharper or ReCover
李笑来:十个让你健康、省钱的重要建议
(转载)2011年值得注意的5个设计趋势
(转载)每位开发人员都应铭记的10句编程谚语
(转载)程序员的八种级别
常用页面跳转代码
java学习笔记:【从网络获取图像资源】实现
(转载)SQL Server datetime数据类型设计与优化误区
sql server 创建作业
(转载)网站开发人员应该知道的62件事
Copyright © 2011-2022 走看看