# include "stdio.h"
# include "stdlib.h"
# include "string.h"
typedef struct _tegstackitem
{
char param;
int index;
}Sitem,*pSitem;
typedef struct _tegstack
{
pSitem pItem;
struct _tegstack *after;
}Stack,*pStack;
int IsNULL(pStack pTop)//测试栈是否为空
{
if (pTop==NULL)
{
return 1;
}
return 0;
}
int push(pStack* pTop,pSitem pItem)//入栈打操作
{
if (pTop==NULL)
{
*pTop=(pStack)malloc(sizeof(Stack));
if (*pTop==NULL)
{
printf("push first error!!!/n");
return 0;
}
(*pTop)->pItem=pItem;
(*pTop)->after=NULL;
return 1;
}
else
{
pStack point=(pStack)malloc(sizeof(Stack));
if (point==NULL)
{
printf("push error!!!/n");
return 0;
}
point->pItem=pItem;
point->after=*pTop;
*pTop=point;
return 1;
}
}
int pop(pStack *pTop,pSitem *pItem)//出栈操作
{
if (*pTop==NULL)
{
return 0;
}
*pItem=(*pTop)->pItem;
pStack point=*pTop;
*pTop=(*pTop)->after;
free(point);
return 1;
}
int getTopItem(pStack pTop,pSitem *pItem)//获得栈顶项
{
if (pTop->pItem==NULL)
{
return 0;
}
else
{
*pItem=pTop->pItem;
return 1;
}
}
void main( int argc, char *argv[ ], char *envp[ ] )
{
if (argv[1]==NULL)
{
printf("input param!!!/n");
return;
}
int length=strlen(argv[1]);
char * pStr=(char*)malloc(length);
if (pStr==NULL)
{
printf("memery matching error!!!");
return;
}
memcpy(pStr,argv[1],length);
int san=0;
pSitem pItem=NULL;
pStack pTop=NULL;
short flag=0;
char item1,item2;
while(san<length)
{
item1=*(pStr+san);
switch(item1)
{
case '(':
flag=1;
break;
case '[':
flag=1;
break;
case '<':
flag=1;
break;
case '{':
flag=1;
break;
default:
flag=0;
break;
}
if (flag)
{
pItem=(pSitem)malloc(sizeof(Sitem));
pItem->index=san;
pItem->param=item1;
if (!push(&pTop,pItem)||pTop==NULL)
{
printf("push error!!!/n");
exit(1);
}
flag=0;
san++;
continue;//返回执行下一次循环
}
//////////////////////////////////////////////////////////////////////
switch(item1)
{
case ')':
item2='(';
flag=1;
break;
case ']':
item2='[';
flag=1;
break;
case '>':
item2='<';
flag=1;
break;
case '}':
item2='{';
flag=1;
break;
default:
flag=0;
break;
}
if (flag)
{
if(!IsNULL(pTop))
{
if (!pop(&pTop,&pItem))
{
printf("find no matching symbol!/n index(---%d)/n/n",san);
break;
}
if (pItem->param==item2)
{
printf("find matching symbol!/n index(%d---%d)/n/n",pItem->index,san);
free(pItem);
}
else
{
printf("find no matching symbol!/n index(%d---%d)/n/n",pItem->index,san);
free(pItem);
break;
}
}
else
{
printf("first symbol no matching !%d/n",san);
break;
}
flag=0;
}
san++;
}
/////////////////////////////////////
while (!IsNULL(pTop))//为了收回占用的内存防止内存泄漏
{
if (san==length)
{
pop(&pTop,&pItem);
printf("<%d>/"%c/" symbol no matching !/n",pItem->index,pItem->param);
free(pItem);
}
else
{
pop(&pTop,&pItem);
free(pItem);
}
}
/////////////////////////////////////
free(pStr);
}