C/C++ code#ifndef _TYPE_SELEMTYPE
#define _TYPE_SELEMTYPE
typedef char SElemType;
#endif
typedef SElemType * ElemLink;
#include
#include
#include
#include "../SqStack_use.h" //栈的存储结构
#include "../SqStack_ops.c" //栈的基本操作
//Status 在SqStack_use.h文件中有定义
Status IsParentheses ( char left, char right ); //判断是否匹配
Status parenthesesMatch ( char *str );
Status parenthesesMatch ( char *str )
{
SqStack S;
SElemType e;
ElemLink p;
int len;
int i;
InitStack_Sq(&S); //此函数在SqStack_ops.c中
len = strlen(str);
p = NULL;
for ( i=0; i
Push_Sq(&S,*(str+i));
if (p!=NULL && IsParentheses(*p, *(S.top-1)) == YES) {
Pop_Sq(&S, &e);
Pop_Sq(&S, &e);
}
p = S.top-1;
}
if (StackEmpty_Sq(S) == YES)
return YES;
else
return NO;
}
Status IsParentheses ( char left, char right )
{
if (left=='(' && right==')')
return YES;
else if (left=='[' && right==']')
return YES;
else if (left=='{' && right=='}')
return YES;
else
return NO;
}
int main ( int argc, char *argv[] )
{
char str[20];
do {
printf ( "==========================\n" );
printf ( "intput some parentheses(input one char to exit):\n" );
scanf("%s", str);
if (strlen(str) == 1)
break;
#ifdef _TYPE_SELEMTYPE
typedef float SElemType;
SElemType num = 100.01;
printf ( "num is :%f\n" , num);
#endif
//我这样用了,不知道对不对,但是编译通过了,请各位指教啊
if (parenthesesMatch(str) == YES)
printf ( "parentheses matching! congratulations!\n" );
else
printf ( "sorry parentheses not matching!\n" );
} while (1);
return EXIT_SUCCESS;
}