关于第5周括号匹配问题的修改方案

来源:互联网 发布:淘宝网店铺招牌图片 编辑:程序博客网 时间:2024/04/26 12:01

问题及代码:

#include <stdio.h>#include "sqstack.h"bool isMatch(char *st){    int d=1, i;    char c;    SqStack *s;    InitStack(s);    for(i=0; st[i]!='\0'&&d; i++)    {        switch(st[i])        {        case'(':        case'[':        case'{':            Push(s, st[i]);            break;        case')':            Pop(s, c);            if(c!='(') d=0;            c='0';            break;        case']':            Pop(s, c);            if(c!='[') d=0;            c='0';            break;        case'}':            Pop(s,c);            if(c!='{') d=0;            c='0';            break;        }    }    if(StackEmpty(s)&&d==1)        return true;    else        return false;}int main(){    char st[50];    printf("请输入表达式:");    scanf("%s", st);    if(isMatch(st))        printf("配对正确!!\n");    else        printf("配对错误!!\n");    return 0;}



运行结果:





知识点总结:

把原来的代码判断语句后加上了c=‘0’这一句,改变c的值,使其不影响下次括号匹配的判断。




心得体会:

这只是我个人的改法,欢迎各位大神来积极评论。




0 0