我的代码
#include<stdio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
typedef struct
{
char data[100];
int top;
}Stack;
int InitStack(Stack stack)
{
stack.top=-1;
return TRUE;
}
int Push(Stack &stack,char &ch)
{
if(stack.top == 99)
return FALSE;
stack.top++;
stack.data[stack.top]=ch;
return TRUE;
}
int Pop(Stack &stack,char &ch)
{
if(stack.top ==-1)
return FALSE;
ch=stack.data[stack.top];
stack.top--;
return TRUE;
}
int IsEmpty(Stack stack)
{
if(stack.top ==-1)
return TRUE;
else
return FALSE;
}
void GetTop(Stack stack,char ch)
{
if(stack.top ==-1)
printf("Õ»¿Õ\n");
else
ch=stack.data[stack.top];
}
int Match(char s,char str)
{
if(str ==')' && s =='(')
return TRUE;
if(str == ']' && s=='[')
return TRUE;
if(str =='}' && s=='{')
return TRUE;
return FALSE;
}
void BracketMatch(char str[])
{
Stack stack;
int i;
char character;
InitStack(stack);
for(i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '(':
case '[':
case '{':
Push(stack,str[i]);
break;
case ')':
case ']':
case '}':
if(IsEmpty(stack))
{
printf("\nÓÒÀ¨ºÅ¶àÓà\n");
return;
}
else
{
GetTop(stack,character);
if(Match(character,str[i]))
Pop(stack,character);
else
printf("\n¶ÔÓ¦µÄÓÒÀ¨ºÅÕÒ²»µ½Æ¥ÅäµÄ×óÀ¨ºÅ\n");
}
}
}
if(IsEmpty(stack))
printf("\nÀ¨ºÅÆ¥Åä!\n");
else
printf("\n×óÀ¨ºÅ¶àÓà!\n");
}
int main()
{
char str[100];
int i=0;
printf("ÇëÊäÈëÒ»¸ö×Ö·û´®,×¢Òâ´øÉÏÀ¨ºÅ\n");
gets(str);
BracketMatch(str);
return 0;
}
不能正常运行麻烦帮忙看下程序,谢谢~