1. Here the delimiter refers;
2. Here, separator matching mainly refers to matching between left and right brackets of various types. It can be used to check the matching conditions of arithmetic parentheses;
3. Main Principles:
. The use of "stack" is mainly because the earliest matching left brackets appear at the latest and comply with the stack's advanced and backward principles;
. Matching rules:The input expression is checked one by character. The left brackets are used as the inbound brackets. When the right brackets appear, if the stack is empty, the left parentheses are missing. If the stack is not empty, otherwise, the matching is successful. Similar to traversing the entire expression, if the stack is not empty after traversing, the corresponding right brackets are missing; otherwise, the matching is successful.
4. Main Code:
. Stack implementation. The Char [] array is used to simulate the storage structure of the underlying stack.
Package COM. fox. datastructrue. stack; </P> <p> public class stackf {<br/> private char [] stack; <br/> private int length; <br/> private int topindex; </P> <p>/** <br/> * @ Param size <br/> */<br/> Public stackf (INT size) {<br/> stack = new char [size]; <br/> length = size; <br/> topindex =-1; <br/>}< br/>/** <br/> * output stack <br/> * @ return <br/> */<br/> Public char POP () {<br/> char ret = 0; <br/> If (topindex =-1) {<br/> system. out. println ("current stack is empty"); <br/>}else {<br/> ret = stack [topindex]; <br/> topindex --; <br/>}< br/> return ret; <br/>}</P> <p>/** <br/> * stack entry <br/> * @ Param C <br/> */<br/> public void push (char C) {<br/> If (length-1) <= topindex) {<br/> system. out. println ("the current stack is full"); <br/>}else {<br/> topindex ++; <br/> stack [topindex] = C; <br/>}</P> <p>/** <br/> * return the top element of the stack <br/> * @ return <br/> */<br/> Public char PEEK () {<br/> char ret = 0; <br/> If (topindex =-1) {<br/> system. out. println ("current stack is empty"); <br/>}else {<br/> ret = stack [topindex]; <br/>}< br/> return ret; <br/>}</P> <p>/** <br/> * determines whether it is empty. <br/> * @ return <br/> */<br/> Public Boolean isempty () {<br/> If (topindex =-1) {<br/> return true; <br/>}else {<br/> return false; <br/>}</P> <p>/** <br/> * determines whether the disk is full. <br/> * @ return <br/> * /<br/> Public Boolean isfull () {<br/> If (topindex >=( length-1) {<br/> return true; <br/>} else {<br/> return false; <br/>}</P> <p>/** <br/> */<br/> Public void display () {<br/> for (INT I = 0; I <topindex + 1; I ++) {<br/> system. out. print (this. stack [I] + ";"); <br/>}< br/> system. out. println (); <br/>}< br/>
4. matching rules
Package COM. fox. datastructrue. stack; </P> <p> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstreamreader; </P> <p> public class bracketchecker {<br/> Public stackf SF; <br/> // statement to be checked <br/> Public String STR = ""; </P> <p>/** <br/> * @ Param size <br/> */<br/> Public bracketchecker (string Str) {<br/> This. STR = STR; <br/> Sf = new stackf (Str. length (); <br />}</P> <p>/** <br/> * Check matching conditions of brackets! <Br/> */<br/> Public void check () {<br/> char [] STR _ = Str. tochararray (); <br/> for (INT I = 0; I <STR _. length; I ++) {<br/> system. out. print ("no." + I + "characters :"); <br/> If (STR _ [I] = '(' | STR _ [I] = '{' | STR _ [I] = '[') {<br/> SF. push (STR _ [I]); <br/> SF. display (); <br/>}< br/> If (STR _ [I] = ') '| STR _ [I] ='} '| STR _ [I] ='] ') {<br/> If (SF. isempty () {<br/> system. out. println ("no." + I + "Location:" + STR _ [I] + "error! The left parenthesis is missing! "); <Br/>}else {<br/> char popleft = SF. pop (); <br/> If (popleft = '(' & STR _ [I] = ') '| <br/> popleft =' {'& STR _ [I] ='} '| <br/> popleft =' ['& Str _ [I] = ']') {<br/> system. out. print ("pop-up" + popleft + ", the current stack content is:"); <br/> SF. display (); <br/>}else {<br/> system. out. println ("no." + I + "Location:" + STR _ [I] + "error! The symbol does not match! "); <Br/>}< br/> If (SF. isempty () {<br/> system. out. println ("matched successfully! "); <Br/>}else {<br/> system. Out. println (" error! The right parenthesis is missing! "); <Br/>}</P> <p>/** <br/> * testarea <br/> * @ Param Fox <br/> */ <br/> Public static void main (string [] FOX) {<br/> bufferedreader reader = new bufferedreader (New inputstreamreader (system. in); <br/> while (true) {<br/> try {<br/> string STR = reader. readline (); <br/> If (Str. equals ("exit") {<br/> break; <br/>} else {<br/> New bracketchecker (STR ). check (); <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p>
5. Test Results
Input string: {}{} [] [] ()
0th characters :{;
1st characters: pop-up {, the current stack content is:
2nd characters :{;
3rd characters: pop-up {, the current stack content is:
4th characters :[;
5th characters: pop-up [, the current stack content is:
6th characters :[;
7th characters: pop-up [, the current stack content is:
8th characters :(;
9th characters: pop-up (the current stack content is:
10th characters :(;
11th characters :(;(;
12th characters :(;(;(;
13th characters: pop-up (the current stack content is :(;(;
14th characters: pop-up (the current stack content is :(;
15th characters: pop-up (the current stack content is:
Matched successfully!
2010-01-27