0
点赞
收藏
分享

微信扫一扫

日撸 Java 三百行学习笔记day15

微笑沉默 2022-03-11 阅读 81

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

在数据处理中,常常要判断数据中的括号,应用栈来处理及其方便。
当用户输入一个字符串时,遇到左括号时,将其入栈,如’(’ ‘[”’{,而遇到右括号时,如’)”}”]’时,与栈顶的括号与当前匹配,如果匹配成功,则将栈中的括号出栈,表示当前括号成对。

 如匹配失败,又分为两种情况,栈已经空和栈不空,若栈已空,则现在右括号比左括号多()},栈不空则很明显,当前括号与栈中括号不匹配(}.

 当所有的匹配完成,也有两种状态,栈空或不空,栈空,当前字符串中所有字符匹配成功,栈不空,则还有左括号待匹配,说明左括号多。

public static boolean bracketMatching(String paraString) {
			// Step 1. Initialize the stack through pushing a '#' at the bottom.
			CharStack tempStack = new CharStack();
			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


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

			for (char ch = 'a'; ch < 'm'; ch++) {
				tempStack.push(ch);
				System.out.println("The current 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 current 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
}

其中对于用#初始化栈不太理解,对最后出栈再判断是否为#,这个前面不匹配直接return false,如果都对了就可以直接return true了,感觉不用再管那个#了。具体代码如下。

 

 

举报

相关推荐

0 条评论