c语言怎么输出不了括号,只要输出 )] 这两个括号右边结果一定是匹配失败...

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

只要输出 )] 这两个括号右边结果一定是匹配失败

#include

#include

#define TRUE 1

#define FALSE 0

typedef struct {

char aa;

}ch;

typedef struct sq{

struct sq *next;

ch ch1;

}sq;

sq *InitStack(sq *le){

le=NULL;

return le;

}

sq *Push(sq *le,ch *cha){

sq *le1;

le1=(sq *)malloc(sizeof(sq));

if(le1==NULL){

printf("增加节点失败了");

exit(1);

}

//*le1->ch1=*cha;

le1->next=le;

le1->ch1=*cha;

le=le1;

return le;

}

sq *Pop(sq *le,ch *cha){

if(NULL==le){

printf("站空了,不能出站");

exit(1);

}

sq *le1;

le1=le;

*cha=le->ch1;

le=le->next;

free(le1);

return le;

}

ch GetHead(sq *le){

if(le==NULL){

printf("站为空,不能取站定元素");

exit(1);

}

return le->ch1;

}

int StackEmpty(sq *le){

if(NULL==le)

return TRUE;

else

return FALSE;

}

int main(){

sq *le;

int chr;

int flag=1;

ch *cha;

le=InitStack(le);

while('#'!=(chr=getchar()) && flag){

//if('#'!=(chr=getchar()) && flag){

switch(chr){

case '[':

case '(':

cha=(ch *)malloc(sizeof(ch));//需要申请地址空间用于储存内容

if(NULL==cha){

printf("申请空间地址为空");

exit(1);

}

cha->aa=chr;

le=Push(le,cha);

free(cha);

break;

case ')':

if(!StackEmpty(le)&&')'==GetHead(le).aa){//

le=Pop(le,cha);

printf("%c ",cha->aa);//

}

else

flag=0;

break;

case ']':

if(!StackEmpty(le)&&']'==GetHead(le).aa){

cha=(ch *)malloc(sizeof(ch));//需要申请地址空间用于储存内容

if(NULL==cha){                //

printf("申请空间地址为空");

exit(1);

}

le=Pop(le,cha);

printf("%c ",cha->aa);

free(cha);

}

else

flag=0;

break;

}

//}

}

if(!StackEmpty(le) && flag)

printf("匹配成功");

else

printf("匹配失败");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值