Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of matching delimiters by stack check

Shulou Source: shulou.com Published: 2022-06-02 10:00:44 09月20日 Update

This article mainly introduces "the method of matching delimiters through stack verification". In daily operation, I believe that many people have doubts about the method of matching delimiters through stack verification. The editor has consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "the method of matching delimiters through stack verification"! Next, please follow the editor to study!

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 ("quit!"); break;} BracketChecker bracketChecker = new BracketChecker (input) BracketChecker.check () } input: eg: (AB [CD] e {fg}) abc (de) [fg] {xxxxxmissing right delimiterator input: eg: (AB [CD] e {fg}) abc [ef] ggg) ddError:) at: 10input: eg: (AB [CD] e {fg}) abc (x) dinput: (AB [CD] e {fg}) this ends the study of "matching by stack delimiters" I hope I can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Methods delimiters learning more help practical next articles theories knowledge articles websites materials follow questions easy to use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Shulou Technology OPPO Reno Redmi