⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokenoperator.java

📁 一个可以读算术表达式
💻 JAVA
字号:
package calculator;

/*
 * Created on 2004-11-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author tom1
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class TokenOperator extends Token {
	private static String OPERATORS = "+-/*^()"; //定义有效的操作符

	private static int[] PRECEDENCE = { 1, 1, 2, 2, 3, 0, 9 }; //定义优先级

	private String token;

	public TokenOperator(String _token) throws TokenException {
		if (OPERATORS.indexOf(_token) >= 0) //判断参数是否为有效的操作符
			token = _token;
		else
			throw new TokenException("invalid operator: " + _token);
	}

	public boolean isOpenParen() {
		return token.equals("(");
	}

	public boolean isCloseParen() {
		return token.equals(")");
	}

	public int order() { //返回操作符的优先级
		return PRECEDENCE[OPERATORS.indexOf(token)];
	}

	public static boolean isValidOp(char op) { //判断参数是否为有效的操作符
		return (OPERATORS.indexOf(op) >= 0);
	}

	public String toString() {
		return "Operator: " + token;
	}

	public TokenNumeric evaluate(double num1, double num2)
			throws TokenException { //根据不同的操作符执行相应的操作
		if (token.equals("+"))
			return new TokenNumeric(num2 + num1);
		else if (token.equals("-"))
			return new TokenNumeric(num2 - num1);
		else if (token.equals("*"))
			return new TokenNumeric(num2 * num1);
		else if (token.equals("/"))
			return new TokenNumeric(num2 / num1);
		else if (token.equals("^"))
			return new TokenNumeric(Math.pow(num2, num1));
		else
			throw new TokenException("invalid token, can not evaluate");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -