0
点赞
收藏
分享

微信扫一扫

第十五天:栈的应用(括号匹配)

桑二小姐 2022-04-17 阅读 34
java

括号匹配需要注意的问题:

左括号必须与右括号是相同的类型,比如“【】”‘“()”{}“”等,其他类型字符不考虑。

左括号必须与正确的顺序闭合。

一般思路:在这里我们使用来实现。遍历字符串时判断:用switch来判断,如果是左括号,那么我们将其入栈;如果为右括号,我们就把栈顶元素出栈,栈顶元素和这个右括号元素相匹配,若匹配就break跳出,继续匹配下一个,若不匹配,返回false。

括号匹配主要函数代码:

public static boolean bracketMatching(String paraString) {
		// Step 1. Initialize the stack through pushing a '#' at the bottom.
		day15 tempStack = new day15();
		tempStack.push('#');
		char tempChar, tempPopedChar;

		// Step 2. Process the string. For a string, length() is a method
		// instead of a member variable.
		for (int i = 0; i < paraString.length(); i++) {
			tempChar = paraString.charAt(i);

			switch (tempChar) {
			case '(':
			case '[':
			case '{':
				tempStack.push(tempChar);
				break;
			case ')':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '(') {
					return false;
				} // Of if
				break;
			case ']':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '[') {
					return false;
				} // Of if
				break;
			case '}':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '{') {
					return false;
				} // Of if
				break;
			default:
				// Do nothing.
			}// Of switch
		} // Of for

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

		return true;
	}// Of bracketMatching

测试数据完整代码:

package day_15;

import day_15.day15;

public class day15 {
public static final int MAX_DEPTH = 10;

	
	int depth;

	char[] data;

	public day15() {
		depth = 0;
		data = new char[MAX_DEPTH];
	}

	public String toString() {
		String resultString = "";
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		} 

		return resultString;
	}

	//入栈
	public boolean push(char paraChar) {
		if (depth == MAX_DEPTH) {
			System.out.println("Stack full.");
			return false;
		} // Of if

		data[depth] = paraChar;
		depth++;

		return true;
	}// Of push

	//出栈
	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
	
	public static boolean bracketMatching(String paraString) {
		// Step 1. Initialize the stack through pushing a '#' at the bottom.
		day15 tempStack = new day15();
		tempStack.push('#');
		char tempChar, tempPopedChar;

		// Step 2. Process the string. For a string, length() is a method
		// instead of a member variable.
		for (int i = 0; i < paraString.length(); i++) {
			tempChar = paraString.charAt(i);

			switch (tempChar) {
			case '(':
			case '[':
			case '{':
				tempStack.push(tempChar);
				break;
			case ')':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '(') {
					return false;
				} // Of if
				break;
			case ']':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '[') {
					return false;
				} // Of if
				break;
			case '}':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '{') {
					return false;
				} // Of if
				break;
			default:
				// Do nothing.
			}// Of switch
		} // Of for

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

		return true;
	}// Of bracketMatching


	
	public static void main(String args[]) {
		day15 tempStack = new day15();
		

		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);

		

	}

}

结果是:

 

举报

相关推荐

HCIP 第十五天

HCIP第十五天

第十五天6号

学习javaweb第十五天

打卡学习第十五天

Java web第十五天

学习python的第十五天

0 条评论