括号匹配 c++
2015-07-13 16:44
295 查看
基本思想:
碰到”(”存入堆栈
碰到”)”弹出堆栈顶端的”(”
碰到”(”存入堆栈
碰到”)”弹出堆栈顶端的”(”
#include "stdafx.h" #include <iostream> #include <stack> #include <string> using namespace std; int main(){ string s = "(1+5)+3+(3()"; stack<char> st; for(int i = 0;i<s.length();i++){ char c = s[i]; if(c=='('){ st.push(c); }else{ if(c==')'){ if(st.empty()){ cout<<"no ( ,still have )"<<endl; break; }else{ st.pop(); } } } } if(!st.empty()){ cout<<"no ) , still have ("<<endl; } system("pause"); return 0; }
相关文章推荐
- c++语言常用转义序列符号
- C++实现python标准库中的Counter
- C/C++拾遗(十九):MFC - 一个简单的窗口程序
- 简单字符串模式匹配算法的C++实现
- c++链表实例与链表基本知识
- vs2013 远程调试 (c++)
- 推荐!国外程序员整理的 C++ 资源大全
- C++ 字符串编程训练5
- 简易C\C++内存池
- c++的进程和线程【windows】
- C++多线程编程简单实例
- C++ clock()函数实现功能介绍
- c语言中,指针加1的情况.指针变量详细介绍
- 学习C++的记录
- C语言中将字符串转换为整数
- C++ 拷贝构造函数
- printf的用法(此文章用于申请搜狐自媒体)作者胡金峰
- C++ 构造函数使用new
- c++变量的作用域、生存期和可见性
- char使用的常见问题