package Eric.ADT.Application; import java.util.Scanner; import Eric.ADT.MyStack; import Eric.ADT.MyStack2; /** * <p> * Title:BranketMatching * </p> * <p> * Description:本程序主要是栈的应用,实现括号匹配的问题 * </p> * <p> * Location:Frostburg * </p> * * @author: Eric.Chen * @date:2017年10月4日下午8:12:30 */ public class BranketMatching { // {<([])>}) public static void main(String[] args) { MyStack stack = new MyStack(); System.out.println("请输入要进行检测的括号字符串,包括<>,(),{},[]:"); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); if (str.length() % 2 != 0) { System.out.println("括号匹配失败!!!"); } else { String open = "<({["; String close = ">)}]"; char[] charArray = str.toCharArray(); for (int i = 0; i < charArray.length; i++) { if (open.indexOf(charArray[i]) != -1) { stack.push(charArray[i]); } else if (close.indexOf(charArray[i]) != -1) { char peek = (char) stack.peek(); if (charArray[i] - peek == 1 || charArray[i] - peek == 2) { Object pop = stack.pop(); } } } if (stack.isempty()) { System.out.println("括号匹配成功!!!"); } else { System.out.println("括号匹配失败!!!"); } } } }