Java科学计算器,支持三角函数、开根号等多种方式,支持优先级运算

这篇博客介绍了如何使用Java编程实现一个功能丰富的科学计算器,支持三角函数如sin、cos、tan,以及开根号运算和复杂的优先级运算。通过该计算器,用户可以进行高级数学计算,提升计算效率。
摘要由CSDN通过智能技术生成


import java.util.LinkedList;
import java.util.List;

public class Calc {

	final public static String[] OPT_ADD = { "+", "add", "plus", "jia" };
	final public static String[] OPT_SUB = { "-", "sub", "jian", "cut" };
	final public static String[] OPT_MUT = { "x", "X", "*", "multi", "cross" };
	final public static String[] OPT_DIV = { "/", "÷", "fiv", "chu" };
	final public static String[] OPT_LBK = { "(", "(" };
	final public static String[] OPT_RBK = { ")", ")" };
	final public static String[] OPT_SQRT = { "sqrt", "√" };
	final public static String[] OPT_SIN = { "sin" };
	final public static String[] OPT_COS = { "cos" };
	final public static String[] OPT_TAN = { "tan" };
	final public static String[] OPT_LOG = { "1og" };
	final public static String[] OPT_PI = { "pi", "pai" };
	final public static String[] OPT_E = { "e" };
	final public static String[] OPT_EXP = { "^", "times" };

	public static void main(String[] args) {
		/*List<Node> list = nodelize("9.7 -5multi2+(-6 *(5.0 sub   1)) + sqrt 4 + (6+5/5)");
		for (Node n : list) {
			System.out.print(n);
			System.out.print(" ");
		}
		System.out.println();
		List<Node> post = toPostfix(list);
		for (Node n : post) {
			System.out.print(n);
			System.out.print(" ");
		}
		System.out.println();
		try {
			double a = doCalc(post);
			System.out.println(a);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		try {
			System.out.println(":" + Calc.run("1+1 add  1 sub 3 multi 2 + (10086 - 10010)"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 9.7-5*2+(6*(5-1))
	 * 
	 * @param in
	 * @return
	 */
	public static List<Node> nodelize(String in) {
		List<Node> ls = new LinkedList<Node>();
		// in = "0" + in;
		in += "#";
		in = in.toLowerCase();
		// byte[] buffer = (in + "#").getBytes();
		int pointer = 0;
		// int last_pointer = 0;
		Type t = Type.Num;
		String tmp = "";
		while (pointer < in.length()) {
			if (in.charAt(pointer) >= '0' && in.charAt(pointer) <= '9' || in.charAt(pointer) == '.') {
				if (t == Type.Num) {
					tmp += in.charA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值