设计原理:主要是利用了栈的结构,在表达式的输入过程中实现对括号是否匹配的判断。根据其括号的原则:小括号之中不能含有大括号或中括号,中括号中不能含有大括号。再由紧密性,左边括号和右边括号是紧密相连的。否则判断为错。其操作为:每输入一个字符打一下回车,若输入括号顺序错误则跳出,并显示错误!附:#include#defineMAX100#defineTRUE1#defineFALSE0#defineEatypedefstructtransition/*建立一个栈*/{charsq[MAX];inttop;}sqstack;sqstackbt;intemptysqstack(sqstackbt)/*判栈空*/{if(bt.top==-1)returnTRUE;elsereturnFALSE;}voidpushsqstack(sqstackbt,charsh)/*入栈*/{if(bt.top==MAX-1){printf("overflow");exit(0);}bt.top++;bt.sq[bt.top]=sh;}voidpopsqstack(sqstackbt)/*出栈*/{intsh;if(bt.top==-1){printf("empty");exit(0);}sh=bt.sq[bt.top];bt.top--;returnsh;}Search(sqstackbt)/*查找括号是否匹配*/{charc=0;printf("Ifyouwanttobreak,pleaseinput'a'\n");while(c!=E&&bt.top<=MAX&&c!='('&&c!='['&&c!='{'){c=getchar();pushsqstack(bt,c);}SearchA(bt,c);SearchB(bt,c);SearchC(bt,c);}SearchA(sqstackbt,charc)/*查找小括号是否匹配*/{if(c=='('){while(c!=')'&&c!='['&&c!=']'&&c!='{'&&c!='}'){c=getchar();pushsqstack(bt,c);}if(c=='(')printf("right\n");elseif(c=='['||c==']'||c=='{'||c=='}')printf("wrong\n");}}SearchB(sqstackbt,charc)/*查找中括号是否匹配*/{if(c=='[')while(c!=']'&&c!='('&&c!=')'&&c!='{'&&c!='}'){c=getchar();pushsqstack(bt,c);}if(c==')'||c=='{'||c=='}')printf("wrong\n");elseif(c=='(')SearchA(bt,c);elseif(c==']')printf("right\n");elseprintf("wrong\n");}SearchC(sqstackbt,charc)/*查找大括号是否匹配*/{if(c=='{')while(c!='}'&&c!='['&&c!=']'&&c!='('&&c!=')'){c=getchar();pushsqstack(bt,c);}if(c==']'||c==')')printf("wrong\n");elseif(c=='[')SearchB(bt,c);elseif(c=='(')SearchA(bt,c);elseif(c=='}')printf("right\n");elseprintf("wrong\n");}main(){inti;bt.top=-1;i=emptysqstack(bt);if(i){Search(bt);}elseexit(0);}
用栈就能实现,这是老师上课讲的吧,望采纳