::  ::  ::  ::  :: 管理

利用栈实现括号匹配

Posted on 2008-06-16 04:14 nt05 阅读(912) 评论(0)  编辑 收藏 引用 所属分类: cpp
#include<iostream>
#include<assert.h>
#include<deque>
#include<stack>
using namespace std;

//预定义
#define maxsize 100
#define TRUE 1
#define FALSE 0

char s[maxsize];

//功能函数
int match(char e[])
{
 typedef allocator<char> Myal;
 typedef deque<char,Myal> Myimpl;
 typedef stack<char,Myimpl>Mycont;
 Mycont::container_type *P_cont=(Myimpl*)0;
 Mycont::value_type *p_val=(char*)0;
 Mycont::size_type *p_size=(size_t*)0;
 
 Mycont stack;
 bool flag=TRUE;
 int i=0;
 while(e[i]!='@'&&flag)
 {
  if(e[i]=='('||e[i]=='['||e[i]=='{')
   stack.push(e[i]);
  switch(e[i])
  {case')':
   if(stack.top()=='(')
    stack.pop();
   else
    flag=FALSE;
   break;
  case']':
   if(stack.top()=='[')
    stack.pop();
   else
    flag=FALSE;
   break;
  case'}':
   if(stack.top()=='{')
    stack.pop();
   else
    flag=FALSE;
   break;
  }
  i++;
  
  
 }
 if(!stack.empty())
   flag=FALSE;
  return flag;
 
}

//主函数:测试
int main()
{
 char e[7]={'(',')','[',']','{','7','@'};
 bool index=match(e);
 cout<<index;
}