日撸代码300行学习笔记 Day 15

1.实现栈的应用-括号匹配

检查一个字符串的括号是否匹配。所谓匹配,是指每个左括号有相应的一个右括号与之对应,且左括号不可以出现在右括号右边。 可以修改测试字符串, 检查不同情况下的运行。除了关注的括号,,其它字符不起任何作用。一旦发现不匹配,就直接返回。

2代码

package demo;

public class CharStack {
	// 设定栈的深度
	public static final int MAX_DEPTH = 10;
	// 实际深度
	int depth;

	char[] data;

	// 创建一个空栈
	public CharStack() {
		depth = 0;
		data = new char[MAX_DEPTH];
	}// Of the first constructor

	// Overrides the method claimed in Object, the superclass of any class.

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

		return resultString;
	}// Of toString

	// 入栈函数
	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.
		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

	// main
	public static void main(String args[]) {
		CharStack tempStack = new CharStack();
		
		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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,感谢您对eclipse插件开发学习的关注!下面是一些学习笔记配套代码的例子。 1. 创建一个最简单的插件: ```java public class HelloWorldPlugin implements IStartup { @Override public void earlyStartup() { System.out.println("Hello, Eclipse Plugin Development!"); } } ``` 2. 在Eclipse菜单中添加一个新的命令: ```java public class MyCommandHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "My Command", "Hello, Eclipse Plugin Development!"); return null; } } ``` 3. 在编辑器右键菜单中添加一个新的动作: ```java public class MyEditorAction extends Action { public MyEditorAction() { setText("My Action"); } @Override public void run() { IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if (editor instanceof ITextEditor) { ITextEditor textEditor = (ITextEditor) editor; IDocumentProvider documentProvider = textEditor.getDocumentProvider(); IDocument document = documentProvider.getDocument(textEditor.getEditorInput()); selectCurrentWord(textEditor, document); } } private void selectCurrentWord(ITextEditor editor, IDocument document) { ISelectionProvider selectionProvider = editor.getSelectionProvider(); ITextSelection selection = (ITextSelection) selectionProvider.getSelection(); int offset = selection.getOffset(); try { int lineStartOffset = document.getLineOffset(selection.getStartLine()); int lineEndOffset = lineStartOffset + document.getLineLength(selection.getStartLine()); String lineText = document.get(lineStartOffset, lineEndOffset - lineStartOffset); int wordStartOffset = lineText.lastIndexOf(' ', offset - lineStartOffset) + 1; int wordEndOffset = lineText.indexOf(' ', offset - lineStartOffset); if (wordEndOffset < wordStartOffset) { // The word is the last or only word on the line wordEndOffset = lineEndOffset - lineStartOffset; } else { wordEndOffset += lineStartOffset; } selectionProvider.setSelection(new TextSelection(wordStartOffset, wordEndOffset - wordStartOffset)); } catch (BadLocationException e) { e.printStackTrace(); } } } ``` 此处仅提供了一些简单的示例代码,以帮助您开始使用eclipse插件开发。有关更详细的学习开发指导,请参阅Eclipse官方文档和教程。祝您在插件开发学习过程中取得成功!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值