day15

Day 15 —— Bracket Matching

1. Background

今天是学习java的第十五天,今天进行的是用栈实现字符串的括号匹配。

2. Description

2.1 String.length()

这个是java中string自带的方法。Returns the length of this string. The length is equal to the number of Unicode code units in the string.这是VScode对它的介绍,用途是返回字符串中字符的个数。

在这里插入图片描述

2.2 String.charAt()

这个也是String自带的方法。Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

在这里插入图片描述

3. Code

package datastructure;

public class CharStack {
    /**
     * The depth.
     */
    public static final int MAX_DEPTH = 10;

    /**
     * The actual depth.
     */
    int depth;

    /**
     * The data.
     */
    char[] data;

    /**
     * Construct an empty char stack.
     */
    public CharStack() {
        depth = 0;
        data = new char[MAX_DEPTH];
    } // Of the first constructor

    public String toString() {
        String resultString = "";

        for (int i = 0; i < depth; i++) {
            resultString += data[i];
        } // Of for i

        return resultString;
    } // Of toString

    /**
     *************
     * Push an element.
     * @param paraChar The given element.
     * @return Success or not.
     * ************
     */
    public boolean Push(char paraChar) {
        if (depth == MAX_DEPTH) {
            System.out.println("Stack full");
            return false;
        }

        data[depth] = paraChar;
        depth++;

        return true;
    } // Of Push

    /**
     *************
     * Pop an element.
     * @return The poped char.
     * ************
     */
    public char Pop() {
        if (depth == 0) {
            System.out.println("Nothing to pop");
            return '\0';
        } // Of if

        char resultChar = data[depth - 1];
        depth--;

        return resultChar;
    } // Of Pop

    /**
     * 用栈实现字符串中的括号匹配。
     * @param paraString The given String.
     * @return Success or not.
     */
    public static boolean bracketMatching(String paraString) {
        // Step 1.
        CharStack tempStack = new CharStack();
        char tempChar, tempPopChar;
        tempStack.Push('#');

        // Step 2.
        for (int i = 0; i < paraString.length(); i++) {
            tempChar = paraString.charAt(i);

            switch (tempChar) {
                case '(':
                case '[':
                case '{':
                    tempStack.Push(tempChar);
                    break;
                case ')':
                    tempPopChar = tempStack.Pop();
                    if (tempPopChar != '(') {
                        return false;
                    } // Of if
                    break;
                case ']':
                    tempPopChar = tempStack.Pop();
                    if (tempPopChar != '[') {
                        return false;
                    } // Of if
                    break;
                case '}':
                    tempPopChar = tempStack.Pop();
                    if (tempPopChar != '{') {
                        return false;
                    } // Of if
                    break;
                default:
                    // Do nothing
            } // Of switch
        } // Of for i

        tempPopChar = tempStack.Pop();
        if (tempPopChar != '#') {
            return false;
        } // Of if

        return true;
    } // Of bracketMatching

    /**
     * The entrance of program.
     * 
     * @param args Not used now.
     */
    public static void main(String[] args) {
        CharStack tempStack = new CharStack();

        for (char i = 'a'; i < 'm'; i++) {
            tempStack.Push(i);
            System.out.println("The stack is: " + tempStack);
        } // Of for i

        char tempChar;
        for (int i = 0; i < 12; i++) {
            tempChar = tempStack.Pop();
            System.out.println("Poped : " + tempChar);
            System.out.println("The stack is: " + tempStack);
        } // Of for i

        boolean tempMatch;
		String tempExpression = "[2 + (1 - 3)] * 4";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "( )  )";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "()()(())";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = "({}[])";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

		tempExpression = ")(";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

    } // Of main
} // Of CharStack

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值