2012-07-27 17 views
0

我在一個小的應用程序上工作,我想獲得一個數學函數,並且x(a,b)的範圍並顯示它的圖形。使用Jtextfield的值作爲java代碼

在某些時候,我調用一個方法來執行x的函數。 我堆在一點,我得到的功能從文本字段,並用它作爲Java代碼

假設(例如F(X)= 2 * X + 1):

class Myclass extends JFrame{ 
    blah blah ... 
    JLabel lblFx =new JLebel("f(x=)"); 
    JTextfield Fx = new JTextField(); 

    //and lets say that this method calculate the f(x). 
    //get as argument the x 
    double calculateFx(double x){ 
     return 2*x+1; // !!!!BUT HERE I WANT TO GET THIS 2*x+1 FROM TextField!!!!! 
    } 

} 

任何想法?

回答

0

我發現這個真棒Expr LIB!

「該軟件包解析並經浮點數計算的數學表達式,例如2 + 2或COS(X /(2 * PI))* COS(Y /(2 * PI))。

的設計優先級是易於使用,有用的錯誤消息,易於集成到小應用程序,以及良好的性能:快速評估和短的下載時間。功能和靈活性並不是高優先級,但代碼很簡單,它不應該很難改變你的口味。「

在我的例子

class Myclass extends JFrame{ 
    blah blah ... 
    JLabel lblFx =new JLebel("f(x=)"); 
    JTextfield Fx = new JTextField(); 
    String formula = Fx.getText();//2*x+1 OR cos(x) OR lot of other functions 

    //and lets say that this method calculate the f(x). 
    //get as argument the x 
    double calculateFx(double x){ 
     // The Expr Object that will representing the mathematical expression 
     Expr expr = null; 

     try { 
      // parsing function formula to mathematical expression 
      expr = Parser.parse(formula); 
     } catch (SyntaxException e) { 
      System.err.println(e.explain()); 
      return; 
     } 

     // Define(make) what will be the variable in the formula, here x 
     Variable vx = Variable.make("x"); 
     // Set the given value(x) in variable 
     vx.setValue(x); 

     //return (mathematical) expression's value. See more for Expr lib how work... 
     return expr.value();// 
    } 

} 

終於某處時,在項目中的呼叫calculateFx(x),對於一個給定的x,你會得到F(x)的值!

3

你在找什麼在某些語言中被稱爲「eval」;但是,Java沒有直接的東西。

Java確實有一個scripting interface called "javax.script",它支持用其他語言(如JavaScript,Ruby,Python等)編寫的代碼的「eval」,也支持訪問Java編譯器(以及其他工具)的tools package called "javax.tools"。對於高級程序員來說,另一種方法是使用解析器生成器來解析文本字段中的字符串並以某種方式解釋它。

不幸的是,這些都是初學者的高級概念。考慮用另一種直接支持「eval」的語言來編寫程序。

+0

你確定嗎?我解釋他的最後一句「從TextField獲取函數(例如f(x)= 2x + 1)並將其用作java代碼」來暗示我的答案。 – maerics 2012-07-27 14:54:15

+0

是的,你說得對。刪除我的答案,對不起。 – 2012-07-27 14:55:35

5

您可以使用ScriptEngine。看到下面的例子,你可以適應使用你的JTextField的內容。

注意:"2x+1"不是一個有效的表達式,您需要包含所有運算符,因此在這種情況下:"2*x+1"

public static void main(String[] args) throws ScriptException { 
    ScriptEngineManager factory = new ScriptEngineManager(); 
    ScriptEngine engine = factory.getEngineByName("JavaScript"); 

    String formula = "2 * x + 1"; //contained in your jtextfield 

    for (double x = 0; x < 10; x++) { 
     String adjustedFormula = formula.replace("x", Double.toString(x)); 
     double result = (Double) engine.eval(adjustedFormula); 
     System.out.println("x = " + x + " ==> " + formula + " = " + result); 
    } 
} 

輸出:

x = 0.0 ==> 2 * x + 1 = 1.0 
x = 1.0 ==> 2 * x + 1 = 3.0 
x = 2.0 ==> 2 * x + 1 = 5.0 
x = 3.0 ==> 2 * x + 1 = 7.0 
x = 4.0 ==> 2 * x + 1 = 9.0 
x = 5.0 ==> 2 * x + 1 = 11.0 
x = 6.0 ==> 2 * x + 1 = 13.0 
x = 7.0 ==> 2 * x + 1 = 15.0 
x = 8.0 ==> 2 * x + 1 = 17.0 
x = 9.0 ==> 2 * x + 1 = 19.0 
+1

+1用於腳本引擎的示例使用。 – maerics 2012-07-27 14:57:43

+0

好吧,但如果在textField上輸入cos(x)? – Universe 2012-07-27 15:41:22

+0

@Universe如果你想處理更復雜的表達式,你需要解析字符串,並用它們的Javascript代替數學函數。在這種情況下,您需要用'Math.cos'替換'cos'。一種方法是將其存儲在地圖中,其中key = cos和value = Math.cos。遍歷該映射並替換字符串中的每個函數。 – assylias 2012-07-27 15:46:24