typedef char DataType;
#include "SeqStack.h"
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define LEFT_FUHAO "([{<"
#define RIGHT_FUHAO ")]}>"
#define BUFFER_SIZE 1000
char Buffer[BUFFER_SIZE];
//是否为括号,不是返回-1,是左边的返回0,右边的返回1
int IsLeftOrRight(char & c);
//匹配函数,匹配为1, 不匹配为0
int IsMatch(char srcChar, char desChar);
int main()
{
int mainLength = 3;
SeqStack stack;
while (1)
{
cout<<"输入一个括号串看是否匹配!"<<endl;
gets(Buffer);
mainLength = strlen(Buffer);
// char Buffer[3] = {'<','>',' '};
for ( int i = 0; i < mainLength; i++)
{//是左边的括号
if ( IsLeftOrRight(Buffer[i]) == 0)
{
stack.Push(Buffer[i]);
}//是右边的括号,进行下一步处理
else if ( IsLeftOrRight(Buffer[i]) == 1)
{
if ( IsMatch(stack.Pop(),Buffer[i]) == 1)
{//一定要考虑是否比较完成
if ( stack.StackEmpty() && i == mainLength-1)
{
cout<<"匹配!"<<endl;
break;
}
//cout<<"Not match!"<<endl;
}
else
{
cout<<"不匹配!/n";
break;
}
}
else
{
if ( i == mainLength -1)
{
cout<<"不匹配!"<<endl;
break;
}
continue;
}
}
}
return 1;
}
//是否为括号,不是返回-1,是左边的返回0,右边的返回1
int IsLeftOrRight(char & c)
{
char *strTempLeft = new char[4];
char *strTempRight = new char[4];
strcpy(strTempLeft,LEFT_FUHAO);
strcpy(strTempRight,RIGHT_FUHAO);
for (int i = 0; i < 4; i++)
{
if ( strTempLeft[i] == c)
{
return 0;//左边的返回0
}
else if ( strTempRight[i] == c)
{
return 1;//右边的返回1
}
}//都不是则返回-1
return -1;
}
//匹配函数,匹配为1, 不匹配为0
int IsMatch(char srcChar, char desChar)
{
switch ( srcChar)
{
case '(':
if ( desChar == ')')
{
return 1;
}
return 0;
break;//not must
case '[':
if ( desChar == ']')
{
return 1;
}
return 0;
break;
case '{':
if ( desChar == '}')
{
return 1;
}
return 0;
break;
case '<':
if ( desChar == '>')
{
return 1;
}
return 0;
break;
default:
return 0;
break;
}
}