首页 > 解决方案 > 我想做一个物理计算器(运动学和弹丸),但我不知道如何构造它。我应该怎么办?

问题描述

我正在尝试制作一个物理计算器,您可以在其中输入您正在寻找的变量,然后提供您知道的所有其他变量。(即,您正在寻找 t = 5 秒时的速度,并且您有 vnaught 和加速度)计算器将内置大多数运动学和(也许)射弹方程,例如 V = at 或 Vf^2 = Vo^2 + 2a(X - Xo)。但是,我不知道我应该如何构建它。

一位朋友建议尝试二叉表达式树,但我不确定这是否是解决此问题的正确方法。另外,我怎样才能实现所有的物理方程?通过 JSON?

public class Node {

    // Nodes used in the algebraic expression trees
    public Node(char symbol, ArrayList<ExpressionTree> equations) {
        this.symbol = symbol;
        this.equations = equations;
        this.left = this.right = null;
    }
    // Nodes have a set of equations associated with them to calculate their value from their children 
    public double calculateValue() {
        for (ExpressionTree eq : equations) {
            try { 
                this.value = eq.calculateInOrder(eq.root);
                System.out.println(this.symbol + "'s value is: " + this.value);
            } catch(Exception e) {
                System.out.println("Missing variables!");
            }
        }
        return this.value;
    }
}

public class ExpressionTree {
    // Constructs a tree and returns a single node that has children nodes who also have children nodes... etc.
    public Node constructTree(char postfix[]) {
        Stack<Node> stack = new Stack<Node>();
        Node n, n1, n2;

        for (int i = 0; i < postfix.length; i++) { 
            if (!isOperator(postfix[i])) { 
                n = new Node(postfix[i]); 
                stack.push(n); // If operand, simply push into stack
            } else // If operator:
            { 
                n = new Node(postfix[i]); 

                // Pop two top nodes (these will be the two operands for the operation)
                n1 = stack.pop();
                n2 = stack.pop(); 

                // Make them children of this node
                n.right = n1; 
                n.left = n2; 

                // Add the node back to the stack. This node keeps its children in the stack.
                stack.push(n); 
            } 

        } 

        n = stack.pop(); // STACK IS NOW EMPTY. N IS THE LAST VALUE FROM STACK, A SINGLE ROOT NODE WITH CHILDREN (with more children).

        this.root = n; // Saves root for this tree instance
        return n; 
    }
    // Goes through each operation (infix)
    public double calculateInOrder(Node n) {
        if (n.left == null && n.right == null) {
            return n.value;
        }

        double leftsum = calculateInOrder(n.left);
        double rightsum = calculateInOrder(n.right);

        if (n.symbol == '^') {
            return Math.pow(leftsum, rightsum);
        } else if (n.symbol == '*') {
            return leftsum * rightsum;
        } else if (n.symbol == '/') {
            return leftsum / rightsum;
        } else if (n.symbol == '+') {
            return leftsum + rightsum;
        } else if (n.symbol == '-') {
            return leftsum - rightsum;
        } else {
            throw new IllegalArgumentException("Cannot calculate without two operands and an operator!");
        }
    }
}

我正在使用对物理方程建模的二进制表达式树,并且如果所有其他值都已知,我已经实现了计算树根值的代码。我不确定如何制作共享特定节点的树(例如加速在多个树中),这对于解决需要多次计算的物理问题很重要。我应该废弃整个表达式树吗?我该如何处理?

很抱歉发布这么多代码,我知道这是一个非常模糊的问题。我只是在寻找正确方向的推动力,以便我可以弄清楚如何构建它。我知道互联网上还有其他物理计算器,但我不知道它们是如何编码的,我想自己写这个来提高我的编码技能。谢谢!

标签: java

解决方案


推荐阅读