您的位置:首页 > 编程语言 > C语言/C++

括号匹配 c++

2015-07-13 16:44 267 查看
基本思想:

碰到”(”存入堆栈

碰到”)”弹出堆栈顶端的”(”

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: