栈实现分隔符匹配

  1 package com.liu.Stack;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 
  7 import org.junit.Test;
  8 
  9 public class BracketChecker {
 10     private String input;
 11     public BracketChecker(String in)
 12     {
 13         input = in;
 14     }
 15         
 16     public void check()
 17     {
 18         int stackSize = input.length();
 19         StackX11 theStack = new StackX11(stackSize);
 20         
 21         for(int j=0;j<input.length();j++)
 22         {
 23             char ch = input.charAt(j);
 24             switch(ch)
 25             {
 26             case '{':
 27             case '[':
 28             case '(':
 29                 theStack.push(ch);
 30                 break;
 31                 
 32             case '}':
 33             case ']':
 34             case ')':
 35                 if(!theStack.isEmpty())
 36                 {
 37                     char chx = theStack.pop();
 38                     if((ch=='}'&&chx!='{') ||
 39                     (ch==')'&&chx!='(') ||
 40                     (ch==']'&&chx!='['))
 41                         System.out.println("error:"+ch+" at "+j);
 42                 }
 43                 else
 44                     System.out.println("error:"+ch+"at"+j);
 45                 break;
 46                 
 47             default:
 48                 break;
 49             }
 50         }
 51         if(!theStack.isEmpty())
 52             System.out.println("error!");
 53         else
 54             System.out.println("success!");
 55     }
 56 }
 57 
 58 class StackX11
 59 {
 60     private int maxsize;
 61     private char[] stackArray;
 62     private int top;
 63     
 64     public StackX11(int maxsize)
 65     {
 66         this.maxsize = maxsize;
 67         stackArray = new char[this.maxsize];
 68         top=-1;
 69     }
 70     
 71     public void push(char c)
 72     {
 73         stackArray[++top] = c;
 74     }
 75     
 76     public char pop()
 77     {
 78         return stackArray[top--];
 79     }
 80     
 81     public char peek()
 82     {
 83         return stackArray[top];
 84     }
 85     
 86     public boolean isEmpty()
 87     {
 88         if(top==-1)
 89             return true;
 90         else
 91             return false;
 92             
 93     }
 94 }
 95 
 96 class BracketsApp
 97 {
 98     
 99     public static void main(String[] args) throws IOException
100     {
101         String input;
102         while(true)
103         {
104             System.out.print("enter string containing delimiters:");
105             System.out.flush();
106             input = getString();
107             if(input.equals(""))
108                 break;
109             BracketChecker theChecker = new BracketChecker(input);
110             theChecker.check();
111         }
112         
113         
114         
115     }
116     
117     public static String getString() throws IOException
118     {
119         InputStreamReader isr = new InputStreamReader(System.in);
120         BufferedReader br = new BufferedReader(isr);
121         String s = br.readLine();
122         return s;
123     }
124     
125 }
原文地址:https://www.cnblogs.com/speaklessdomore/p/3664203.html