正在加载图片...
号正确配对,否则不配对 编程后的整个函数如下(李书P31-32) # define m0100/*m0为算术表达式中最多字符个数* correct(exp, tag) char explo i char st[m0: int top=0, I=I while(i<=mo & tag) f(exp[l==(l|exp[il==[|exp[=={()*遇到(、'[或'{“,则将其入栈* itop++ st top=expli if(exp[]==))/*遇到')’,若栈顶是(,则继续处理,否则以不配对返回* if(st(top]=="()top--, else tag=0 if(exp[i]==)’)/遇到'],若栈顶是{,则继续处理,否则以不配对返回* if(st[top==[top-- if(expi]==))*遇到}’,若栈顶是‘{‘,则继续处理,否则以不配对返回* if(st[ topl== top if(top0)tag=0;/*若栈不空,则不配对* 严题集对应答案: Status AllBrackets Test(char*str)/判别表达式中三种括号是否匹配 InitStack(s) for(p=str; *p p++) if(p=='(I p=l p=t) push(s, "p) else if(*p==)l*p==l*p=)) if(Stack Empty(s))return ERROR; pop(s, c) if(*p=)'&&cl='o) return ERROR; if("p=]'&&cl=D return ERROR; if*p=}&&c!={) return ERROR.∥必须与当前栈顶括号匹配 i//for if(!Stack Empty (s))return ERROR;6 号正确配对,否则不配对。 编程后的整个函数如下(李书 P31—32) #define m0 100 /*m0 为算术表达式中最多字符个数*/ correct(exp,tag) char exp[m0]; int tag; {char st[m0]; int top=0, i=1; tag=1; while (i<=m0 && tag) {if (exp[i]= = ‘(‘||exp[i]= =’[‘||exp[i]= =’{‘) /*遇到‘(‘、’[‘或’{‘,则将其入栈*/ {top++; st[top]=exp[i]; } if (exp[i]= =’)’ ) /*遇到’)’ ,若栈顶是‘(‘,则继续处理,否则以不配对返回*/ if(st[top]= =‘(‘ ) top--; else tag=0; if (exp[i]= =’ )’ ) /*遇到’ ]’ ,若栈顶是‘[‘,则继续处理,否则以不配对返回*/ if(st[top]= =‘[ ‘] top--; else tag=0; if (exp[i]= =’)’ ) /*遇到’ }’ ,若栈顶是‘{‘,则继续处理,否则以不配对返回*/ if(st[top]= =‘{‘ top--; else tag=0; i++; } if(top>0)tag=0; /*若栈不空,则不配对*/ } 严题集对应答案: 3.19 Status AllBrackets_Test(char *str)//判别表达式中三种括号是否匹配 { InitStack(s); for(p=str;*p;p++) { if(*p=='('||*p=='['||*p=='{') push(s,*p); else if(*p==')'||*p==']'||*p=='}') { if(StackEmpty(s)) return ERROR; pop(s,c); if(*p==')'&&c!='(') return ERROR; if(*p==']'&&c!='[') return ERROR; if(*p=='}'&&c!='{') return ERROR; //必须与当前栈顶括号匹配 } }//for if(!StackEmpty(s)) return ERROR;
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有