2017-04-13 86 views
3

大家好我有这样的代码,使得功能在数学但此功能的结果是错误的OO如何在数学函数获得真正的价值

X = 0

“-3 * X^2-16 * X + 2"

代码:

public static void main(String[] args) throws Exception { 

     ScriptEngineManager manager = new ScriptEngineManager(); 
     ScriptEngine engine = manager.getEngineByName("js"); 
     engine.put("X", 0); 

     Object operation = engine.eval("-3*X^2-16*X+2"); 
     //Object operation2 = engine.eval("(X+3)"); 

     System.out.println("Evaluado operacion 1: " + operation); 
     //System.out.println("Evaluado operacion 2: " + operation2); 

    } 

结果为2,但我得到4

Evaluado operacion 1:4

我有由

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package gustavo_santa; 

import javax.script.ScriptEngine; 
import javax.script.ScriptEngineManager; 
import javax.script.ScriptException; 
import javax.swing.JOptionPane; 

/** 
* 
* @author osmarvirux 
*/ 
public class SerieB { 
    //xi and x4 
    int xi; 
    int x4; 
    //f(x) function variables 
    String positive_negative; 
    int num_one; 
    int elevation_one; 
    String add_subtract_one; 
    int num_two; 
    int elevation_two; 
    String add_subtract_two; 
    int num_three; 
    //results 
    String xi_result; 
    String x4_result; 

    public SerieB(int xi, int x4, String positive_negative, int num_one, int elevation_one, String add_subtract_one, int num_two, int elevation_two, String add_subtract_two, int num_three) { 
     this.xi = xi; 
     this.x4 = x4; 
     this.positive_negative = positive_negative; 
     this.num_one = num_one; 
     this.elevation_one = elevation_one; 
     this.add_subtract_one = add_subtract_one; 
     this.num_two = num_two; 
     this.elevation_two = elevation_two; 
     this.add_subtract_two = add_subtract_two; 
     this.num_three = num_three; 
    } 

    public void Procedure_xi(){ 
     ScriptEngineManager manager = new ScriptEngineManager(); 
     ScriptEngine engine = manager.getEngineByName("js"); 
     if (positive_negative == "-"){ 

      try { 
      xi_result=(num_one*(Math.pow(xi, elevation_one)))+add_subtract_one+(num_two*(Math.pow(xi, elevation_two))) 
        +add_subtract_two+num_three; 
      Object result = engine.eval(xi_result); 
      System.out.println(xi_result+" = "+result); 
      } catch(ScriptException se) { 
       se.printStackTrace(); 
      }   
     }else{ 
      try { 
      xi_result=((-num_one*(Math.pow(xi, elevation_one)))+add_subtract_one+(num_two*(Math.pow(xi, elevation_two))) 
      +add_subtract_two+num_three); 
      Object result = engine.eval(xi_result); 
      System.out.println(xi_result+" = "+result); 
      } catch(ScriptException se) { 
       se.printStackTrace(); 
      }  

     } 
    } 
    public void Procedure_x4(){ 
     ScriptEngineManager manager = new ScriptEngineManager(); 
     ScriptEngine engine = manager.getEngineByName("js"); 
     if (positive_negative == "-"){ 

      try { 
      x4_result=(num_one*(Math.pow(x4, elevation_one)))+add_subtract_one+(num_two*(Math.pow(x4, elevation_two))) 
        +add_subtract_two+num_three; 
      Object result = engine.eval(x4_result); 
      System.out.println(x4_result+" = "+result); 
      } catch(ScriptException se) { 
       se.printStackTrace(); 
      }   
     }else{ 
      try { 
      x4_result=((-num_one*(Math.pow(x4, elevation_one)))+add_subtract_one+(num_two*(Math.pow(x4, elevation_two))) 
      +add_subtract_two+num_three); 
      Object result = engine.eval(x4_result); 
      System.out.println(x4_result+" = "+result); 
      } catch(ScriptException se) { 
       se.printStackTrace(); 
      }  

     } 
    } 
    public static void main(String[] args){ 
     //-3x^2-16x+2 
     SerieB obj = new SerieB(0, 1, "+", -3, 2, "-", 16, 1, "+", 2); 
     obj.Procedure_xi(); 
     obj.Procedure_x4(); 
    } 


} 

结果与此代码其他代码是2,但我想使用

ScriptEngineManager经理= new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();

因为是一个库,我认为更准确,我不想使用我的代码,因为有很多行,我不知道如果是100%的效率。有人可以帮助我吗?或者给我一个推荐来解决这个数学函数?非常感谢

+0

问题是'^'的意思是按位排他,而不是取幂。但我不知道如何解决这个问题。 –

+0

考虑在您使用JavaScript脚本引擎后向此问题添加JavaScript标记。 –

+0

我已添加JavaScript标签bro –

回答

1

你得到的结果是正确的。

这个混淆源于你假设你是电源操作员(^)实际上是JavaScript中的bitwise XOR操作符(你正在使用JavaScript脚本引擎)。

因此,评估0^2产生2,而评估Math.pow(0, 2)产生0,因此,差异。

为了得到你所期望的结果,表达必须阅读:

-3*Math.pow(X,2)-16*X+2 

你可以预先处理表达的Math.pow()调用来替代指数的操作:

let X = 0; 
 
let expression = "-3*X^2-16*X+2" 
 
let processed = expression.replace(/(\w+)\^(\w+)/g, 'Math.pow($1,$2)'); 
 

 
console.log(processed); // prints "-3*Math.pow(X,2)-16*X+2" 
 
console.log(eval(processed)); // prints "2"

使用脚本引擎,可能看起来像:

ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine engine = manager.getEngineByName("js"); 
engine.put("X", 0); 
engine.put("expression", "-3*X^2-16*X+2"); 
engine.put("processed", engine.eval("expression.replace(/(\\w+)\\^(\\w+)/g, 'Math.pow($1,$2)')")); 

System.out.println(engine.eval("eval(processed)")); // 2.0 

或者,如果你喜欢做Java中的正则表达式替换:

String expression = "-3*X^2-16*X+2"; 
String processed = expression.replaceAll("(\\w+)\\^(\\w+)", "Math.pow($1,$2)"); 

ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine engine = manager.getEngineByName("js"); 
engine.put("X", 0); 

System.out.println(engine.eval(processed)); // 2.0 
+0

'Math.pow(0,2)'意味着0的权力2应该产生0不是1 –

+0

但我想获得一个字符串的信息如何将文本?因为用户不能在字符串兄弟中写入“-3 * Math.pow(X,2)-16 * X + 2”:( –

+0

@MatthewCiaramitaro Thx。Typo。 –

0

我觉得发生了什么事是你的程序评估

(-3*0)^2 - 16*0 + 2 = 0^2 +2 = 2+2 =4 

,因为在计算机科学中的操作^意味着按位异或, 这基本上意味着,如果该位都为1或0,然后改变他们为0,否则1和2的10而0是0表示这样2^0 = 2

尝试用*X 或者你更换^2可以使用Math.pow(X,n),如果你需要幂一些电力n,但平方最好是刚写出来为X*X

下面是过时由于编辑到的问题,但仍是功能:

在你写的第二部分问题

Object operation = engine.eval("-3*(X^2)-16*X +2"); 
String processed = operation.replace(/(\w+)\^(\w+)/g, 'Math.pow($1,$2)'); 

根据另一个用户的回答。你应该写下

String expr = "-3*(X^2)-16*X +2"; 
String processed = expr.replaceAll("(\\w+)(\\^)(\\w+)", 'Math.pow($1,$2')); 
Object operation = engine.eval(processed); 
+0

这不会编译。 –

+0

这是一个正则表达式吗? –

+0

该正则表达式替换不起作用。 –