K: Bracket separator matching problem

Related introduction:

  bracket separator matching refers to the problem of judging whether the brackets in the input string expression match or not. For example, 1 + (12 + 2) * (1 + 2) is an expression with bracket separator matching, and (12 + 1) * 4 + (12 / 2) is an expression with bracket separator mismatching

  to determine whether the bracket separators of an expression match, the idea is as follows: read the relevant characters in the expression in turn; if it is a number or an operator, continue to read the next character; if it is a left bracket, press it into the stack; if it is a right bracket, pop up the top element of the stack to see whether it is the left bracket type matched by the right bracket; if not, the table Expression is not matched by parentheses. Otherwise, repeat the above steps until each character in the expression is traversed. When traversing is completed, if the element in the stack is not empty, the parentheses do not match. Otherwise, the parentheses match.

The relevant codes are as follows:

package queueandstack;

import java.util.Scanner;

/**
 * This class is used to implement the operation of bracket matching problem
 * The question of whether the brackets used to determine the input match
 * @author Apprentice
 *
 */

public class Match
{
    public void matchesSymbol() 
    {
        boolean match=false;
        Stack<String> stack=new Stack<String>();
        System.out.print("Please enter a series of brackets:");
        String inputString=null;
        while(true)
        {
            inputString=getInputString();
            if(inputString==null||deleteBlank(inputString).equals(""))
            {
                System.out.print("Please enter a series of brackets:");
                continue;
            }
            else
            {
                break;
            }
        }
        //Remove empty characters
        inputString=deleteBlank(inputString);
        //When parentheses appear in pairs
        if(inputString.length()%2==0)
        {
            match=true;
            for(int i=0;i<inputString.length();i++)
            {
                //When the bracket is left, it is pushed into the stack, otherwise it will pop up from the stack
                if(String.valueOf(inputString.charAt(i)).matches("[((\\[{[]"))
                {
                    stack.push(String.valueOf(inputString.charAt(i)));
                }
                else
                {
                    if(!stack.isEmpty())
                    {
                        String value=stack.pop();
                    }
                    else
                    {
                        match=false;
                        break;
                    }
                }
            }
        }
        if(!match||!stack.isEmpty())
        {
            System.out.println("Bracket mismatch");
        }
        else
        {
            System.out.println("Bracket match succeeded");
        }
    }
    //This method is used to get the input string
    private  String getInputString()
    {
        Scanner in =new Scanner(System.in);
        String str=null;
        if(in.hasNextLine())
        {
            str=in.nextLine();
        }
        return str;
    }
    //This method is used to remove the null character from the input string
    private String deleteBlank(String inputString)
    {
        inputString=inputString.replaceAll(" ", "");
        return inputString;
    }
}

Go back to the catalogue

Tags: Java

Posted on Sun, 03 May 2020 06:50:54 -0700 by saeed42