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条)

报告相同问题?

悬赏问题

  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题