求大神帮我看看这个简单的栈调用,新手程序媛不胜感激!

#include
#include
#include
#define STACK_SIZE 100
/*external variables*/
char contents[STACK_SIZE];
int top=0;
void make_empty(void);
bool is_empty(void);
bool is_full(void);
void push(char i);
char pop(void);
int stack_overflow(void);
void stack_underflow(void);
void read_line(char str[]);
int main(void)
{
int i,len;
make_empty();
printf("Enter parenteses and/or braces: ");
read_line(contents);
len=strlen(contents);
for(i=0;i<=len;i++)
{
switch(contents[i]){
case '(':push(contents[top]);
break;
case '{':push(contents[top]);
break;
case ')':if(pop()=='(')
break;
else{printf("Unmatched!");
goto END;}
case '}':if(pop()=='{')
break;
else{printf("Unmatched!");
goto END;}
case '\n':if(is_empty())
printf("Matched!");
else printf("Unmatched!");
}
}
END:
return 0;
}
void make_empty(void)
{
top=0;
}
bool is_empty(void)
{
return top==0;
}
bool is_full(void)
{
return top==STACK_SIZE;
}
void push(char i)
{
if (is_full())
{
stack_overflow();
}
else
contents[top++]=i;
}
char pop(void)
{
if (is_empty())
stack_underflow();
else
return contents[--top];
}
int stack_overflow(void)
{
printf("Stack overflow!");
return 1;
}
void stack_underflow(void)
{
printf("Stack underflow!");
}
void read_line(char str[])
{
int len,i=0;
do{if(i<STACK_SIZE)
str[i++]=getchar();
}while(str[i-1]!='\n');}请问为什么我输入()()的时候显示匹配,输入(){}的时候就显示不匹配了?我调试之后发现每次到}的时候程序就把pop()作为假了,我不知道为什么?请问是我的数组下标有问题吗?谢谢~

case '\\{' : push(contents[top]);
case '\\}' : if(pop()=='{')

图片说明

--top换成top--,应该就可以了

刚才调试了一下你的程序,在case语句中
case '\{' :
case '\}' :
改成上面这样就行了。

自己多调试以后就能找出错误了。。。。。