qq_37262350 2017-01-06 13:20 采纳率: 75%
浏览 833
已采纳

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

#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()作为假了,我不知道为什么?请问是我的数组下标有问题吗?谢谢~

  • 写回答

4条回答 默认 最新

  • 企鹅爱吃方便面 2017-01-06 14:05
    关注
    case '\\{' : push(contents[top]);
    case '\\}' : if(pop()=='{')
    
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘