PackageEric.ADT.Application;ImportJava.util.Scanner;ImportEric.ADT.MyStack;ImportEric.ADT.MyStack2;/*** <p> * title:branketmatching * </p> * <p> * Description: This program is mainly the application of the stack, to achieve the problem of matching brackets * </p> * <p& Gt * Location:frostburg * </p> * *@author: Eric.chen * @date: October 4, 2017 pm 8:12:30*/ Public classbranketmatching {//{< ([]);}) Public Static voidMain (string[] args) {Mystack stack=NewMystack (); System.out.println ("Please enter the parentheses string to be instrumented, including <>, (), {},[]:"); Scanner SC=NewScanner (system.in); String Str=Sc.nextline (); if(Str.length ()% 2! = 0) {System.out.println ("Bracket matching failed!!! "); } Else{String Open= "< ({["; String Close= ">)}]"; Char[] Chararray =Str.tochararray (); for(inti = 0; i < chararray.length; i++) { if(Open.indexof (chararray[i])! =-1) {Stack.push (chararray[i]); } Else if(Close.indexof (chararray[i])! =-1) { CharPeek = (Char) Stack.peek (); if(Chararray[i]-Peek = = 1 | | chararray[i]-PEEK = = 2) {Object pop=Stack.pop (); } } } if(Stack.isempty ()) {System.out.println ("Brace matching success!!!" "); } Else{System.out.println ("Bracket matching failed!!! "); } } }}
Java Implementation Bracket Matching