目前在做一个程序,需要实时输入复杂函数,需要程序能够理解函数,应该如何做?请各位高手不吝赐教,给出思路即可。

解决方案 »

  1.   

    可以使用支持嵌入Java程序的脚本语言,比如 jython, groovy, beanshell等等
    给你一个简单的例子吧, 这个程序需要beanshell的包, 下载地址是
    http://beanshell.org/bsh-2.0b2.jar
    /////////////////////////////////////////////////////////////////////////import bsh.EvalError;
    import bsh.Interpreter;/*
     * BeanShellTest.java
     *
     * Created on 2005年4月30日, 下午4:53
     */public class BeanShellTest extends javax.swing.JFrame {
    Interpreter interpreter = new Interpreter();

    /** Creates new form BeanShellTest */
    public BeanShellTest() {
    initComponents();
    setSize(550, 550);
    setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    yLabel = new javax.swing.JLabel();
    yTextField = new javax.swing.JTextField();
    xLabel = new javax.swing.JLabel();
    xTextField = new javax.swing.JTextField();
    scriptLabel = new javax.swing.JLabel();
    scrollPane = new javax.swing.JScrollPane();
    scriptArea = new javax.swing.JTextArea();
    calcButton = new javax.swing.JButton();
    resultField = new javax.swing.JTextField();

    getContentPane().setLayout(null);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    yLabel.setText("y");
    getContentPane().add(yLabel);
    yLabel.setBounds(170, 30, 6, 15);

    yTextField.setText("20");
    getContentPane().add(yTextField);
    yTextField.setBounds(190, 30, 100, 21);

    xLabel.setText("x");
    getContentPane().add(xLabel);
    xLabel.setBounds(40, 30, 20, 15);

    xTextField.setText("10");
    getContentPane().add(xTextField);
    xTextField.setBounds(60, 30, 90, 21);

    scriptLabel.setText("script");
    getContentPane().add(scriptLabel);
    scriptLabel.setBounds(40, 80, 36, 15);

    scrollPane.setViewportView(scriptArea);
    scriptArea.setText("result=x+y;");

    getContentPane().add(scrollPane);
    scrollPane.setBounds(40, 110, 450, 300);

    calcButton.setText("Calculate");
    calcButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    getContentPane().add(calcButton);
    calcButton.setBounds(40, 430, 100, 25);

    resultField.setEditable(false);
    getContentPane().add(resultField);
    resultField.setBounds(160, 430, 330, 21);

    pack();
    }
    // </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    interpreter.set("x", Integer.parseInt(xTextField.getText()));
    interpreter.set("y", Integer.parseInt(yTextField.getText())); interpreter.eval(scriptArea.getText()); Object res = interpreter.get("result");
    resultField.setText(res.toString()); } catch (EvalError e) {
    e.printStackTrace();
    }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new BeanShellTest().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton calcButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea scriptArea;
    private javax.swing.JTextField resultField;
    private javax.swing.JLabel scriptLabel;
    private javax.swing.JLabel xLabel;
    private javax.swing.JTextField xTextField;
    private javax.swing.JLabel yLabel;
    private javax.swing.JTextField yTextField;
    // End of variables declaration

    }
      

  2.   

    没试过。不知道这样的思路是否可行?
    写程序把实时输入的函数保存到一个.java的文件,然后(在程序中)调用JAVAC编译该文件产生一个类,
    然后用Class.forName装载该类,可以调用它的方法了。
      

  3.   

    To;gtlang78() and darkattack(居士),非常感谢两位和两位的想法,我会试试的,今天晚饭我请教了一位牛人,他竟然说自己做个词法和语法分析器,我倒~~~~~程序都不过2000来行:)