#include<windows.h>
#include<stdio.h>
#define FAIL -1
#define SUCCESS 0
char *next_token(char *s)
{
while(*s!='/0'&&*s!='['&&*s!=']'&&*s!='('&&*s!=')'&&*s!='{'&&*s!='}')
s++;
return s;
}
char UnChange(char *s )
{
switch( *s )
{
case '(':
return ')';
case ')':
return '(';
case '[':
return ']';
case ']':
return '[';
case '{':
return '}';
case '}':
return '{';
default:
return *s;
}
}
char* IsRecurMatch(char *string)
{
static int iCount = 0;
char cCurChar;
char *cur_char = NULL;
char *pcNext = NULL;
cur_char = next_token(string);
switch(*cur_char)
{
case '(':
case '[':
case '{':
cCurChar = UnChange(cur_char);
pcNext = IsRecurMatch(cur_char+1);
if( pcNext == '/0' )
{
return pcNext;
}
if( cCurChar == *pcNext )
{
//printf("字符%c匹配成功!/n", cCurChar);
pcNext = next_token(pcNext+1);
return pcNext;
}
else
{
printf("错误:字符%c匹配失败: %c != %c/n", *pcNext, UnChange(&cCurChar), *pcNext);
exit(1);
}
case ')':
case ']':
case '}':
return cur_char;
default:
return cur_char;
}
}
int Is_Match(char *string)
{
static int iCount = 0;
char *cTemp = IsRecurMatch(string);
iCount ++;
switch(*cTemp)
{
case ')':
case '}':
case ']':
{
if( iCount == 0 )
{
printf("错误:算术表达式不能以 %c 开头/n", *cTemp);
exit(1);
}
else
{
printf("错误:算术表达式 %c 无法匹配/n", *cTemp);
exit(1);
}
}
case '(':
case '[':
case '{':
Is_Match(cTemp);
break;
case '/0':
return 0;
default:
printf("不知道的错误!");
exit(1);
}
}
void main()
{
int iRet = Is_Match("{1klk}");
if( iRet == 0 )
{
printf("成功!/n");
}
else
{
printf("失败!/n");
}
}