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

📄 evaluation.java

📁 Calculator:可以计算各种基础表达式的计算器,比如3x+5x^2(x=3)
💻 JAVA
字号:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Calculator;

/**
 *
 * @author asus
 */
/*
 ******************PUBLIC MOTHOD*********************
public double getVar(String s)				 -> get the value of the variable
store in the hashmap
public double evaluate(Vector listExpr)		 -> recursion mothod use partEvaluate
to evaluate the vector expression
public void print()							 -> print out the result
 ******************PRIVATE MOTHOD********************
private double partEvaluate(Vector listExpr) -> evaluate the mininum part
 *****************************************************
 */
import java.util.Vector;

public class Evaluation {

    private ArgumentSet argSet;
    private ListExpression exp;
    private ListExpression exp1;
    @SuppressWarnings("unchecked")
    private Vector preExp = new Vector();
    private Vector preExp1 = new Vector();
    private Vector var;

    public Evaluation(String list) {
        exp = new ListExpression(list);
        preExp = exp.list2Preorder();
        exp1 = new ListExpression(list);
        preExp1 = exp1.list2Preorder();
        argSet = new ArgumentSet();
        var =  new Vector();
    }

    public ListExpression getExp() {
        return exp1;
    }

    public Vector getPreExp() {
        return preExp;
    }

    //public method to get (ArgumentSet)arg
    public ArgumentSet getArgSet() {
        return this.argSet;
    }
    /*if the string is a number, return its double result
     * if the string is a var,catch the Exception
     * and return the value in the hash map*/

    public double getVar(String s) {
        double result = 0.0;
        try {
            result = Double.parseDouble(s);
        } catch (NumberFormatException e) {
            result = argSet.getArg(s);
            if (!var.contains(s)) {
                var.add(s);
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    private double partEvaluate(Vector listExpression) throws Exception {
        Vector listExpr = new Vector();
        listExpr.addAll(listExpression);
        double result = 0;
        String operator = "";
        for (int i = listExpr.size() - 1; i >= 0; i--) {
            operator = (String) listExpr.get(i);
            if (Operation.isBiOperator(operator)) {
                if (operator.equals("+")) {
                    result = (getVar((String) listExpr.get(i + 1))) + (getVar((String) listExpr.get(i + 2)));
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.add(i, "" + result);
                } else if (operator.equals("-")) {
                    result = (getVar((String) listExpr.get(i + 1))) - (getVar((String) listExpr.get(i + 2)));
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.add(i, "" + result);
                } else if (operator.equals("*")) {
                    result = (getVar((String) listExpr.get(i + 1))) * (getVar((String) listExpr.get(i + 2)));
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.add(i, "" + result);
                } else if (operator.equals("/")) {
                    result = (getVar((String) listExpr.get(i + 1))) / (getVar((String) listExpr.get(i + 2)));
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.add(i, "" + result);
                }
            } else if (Operation.isUnOperator(operator)) {
                if (operator.equals("sin")) {
                    result = Math.sin(Math.toRadians((getVar((String) listExpr.get(i + 1)))));
                    listExpr.remove(i);
                    listExpr.remove(i);
                    listExpr.add(i, "" + result);
                }
            } else {
                result = getVar((String) listExpr.get(i));
            }
            
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    public double evaluate(Vector listExpression) throws Exception {
        Vector listExpr = new Vector();
        listExpr.addAll(listExpression);
        double result = 0.0;
        int count = 0;
        for (int i = 0; i < listExpr.size(); i++) {
            if (listExpr.get(i) instanceof Vector) {
                count++;
            }
        }
        if (count == 0) {
            result = partEvaluate(listExpr);
        } else {

            for (int i = 0; i < listExpr.size(); i++) {
                if (listExpr.get(i) instanceof Vector) {
                    listExpr.set(i, String.valueOf(evaluate((Vector) listExpr.get(i))));
                }
            }
            result = partEvaluate(listExpr);
        }
        preExp.clear();
        preExp.addAll(0, preExp1) ;//copyInto(Object[] anArray)
        return result;
        
    }

    public double Evaluate() throws Exception{
        double result = evaluate(preExp);
        //preExp = preExp1;
        return result;
    }

    public Vector showVar() throws Exception{
        return var;
    }

    public void print() throws Exception {
        System.out.print("PreOrderList:");
        System.out.println(preExp);
        //System.out.println(this.showVar());
        System.out.print("Value:");
        System.out.printf("%11.1f\n", Evaluate());
        //System.out.println(this.showVar());
        System.out.println(preExp1);
    }
}

⌨️ 快捷键说明

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