您好,登錄后才能下訂單哦!
這篇文章主要介紹“通過棧校驗分隔符匹配的方法”,在日常操作中,相信很多人在通過棧校驗分隔符匹配的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”通過棧校驗分隔符匹配的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
package helloclean; public class StackX { private int maxSize; private char[] stackArray; private int top; public StackX(int max){ maxSize = max; stackArray = new char[maxSize]; top = -1; } public void push(char j) { stackArray[++top] = j; } public char pop() { return stackArray[top--]; } public char peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } }
package helloclean; public class BracketChecker { private String input; public BracketChecker(String in) { this.input = in; } public void check() { int stackSize = input.length(); StackX theStack = new StackX(stackSize); for(int j = 0; j < input.length(); j ++) { char ch = input.charAt(j); switch (ch) { case '{': case '[': case '(': theStack.push(ch); break; case '}': case ']': case ')': if(!theStack.isEmpty()) { char chx = theStack.pop(); if((ch == '}' && chx != '{') || (ch == ')' && chx != '(') || ch == ']' && chx != '[') { System.out.println("Error : " + ch + " at : " + j); } } else { System.out.println("Error : " + ch + " at : " + j); } break; default: break; } } if(!theStack.isEmpty()) { System.out.println("missing right delimiter!"); } } }
package helloclean; import java.util.Scanner; public class BracketApp { public static void main(String[] args) { String input; Scanner scanner = new Scanner(System.in); while (true) { System.out.println("input : eg: (ab[cd]e{fg})"); input = scanner.nextLine(); if(input.equals("q")) { System.out.println("退出!"); break; } BracketChecker bracketChecker = new BracketChecker(input); bracketChecker.check(); } } }
input : eg: (ab[cd]e{fg}) abc(de)[fg]{xxxxx missing right delimiter! input : eg: (ab[cd]e{fg}) abc[ef]ggg)dd Error : ) at : 10 input : eg: (ab[cd]e{fg}) abc(x)d input : eg: (ab[cd]e{fg})
到此,關于“通過棧校驗分隔符匹配的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。