#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()作为假了,我不知道为什么?请问是我的数组下标有问题吗?谢谢~
求大神帮我看看这个简单的栈调用,新手程序媛不胜感激!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
悬赏问题
- ¥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代码的移植问题