括号配对检查
检查表达式中的括号是否配对,
如果配对,则返回true
否则,返回false
////////////////////////////////////////////////////////////
// 源程序
// 功能:检查表达式中的括号是否配对
// 时间:21:35 2005-10-2
// 最后修改时间:21:35 2005-10-2
////////////////////////////////////////////////////////////
#include "iostream.h"
#define max 1024
bool bracketCheck( const char exp[] )
{
char ch;
int p = 0;
ch = exp[p++];
//char stack[max];
int top = 0;
while( ch )
{
if( ch == '(' )
{
top++;
//stack[top] = ch;
}
else if( ch == ')' )
{
top--;
if( top < 0 )
return false;
//if( stack[top] != '(' )
// return false;
}
ch = exp[p++];
}
if( top != 0 )
return false;
return true;
}
void main()
{
if( bracketCheck( "()()()())" ) )
cout<<"it is ok"<<endl;
else
cout<<"it is not match"<<endl;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////