将字符串“sin(pi * cos(pi * x * y))”转换为double

时间:2013-11-15 19:08:10

标签: java parsing

(我是初学者)

双x = 0.5; 双y = 0.3;

    String[]normal = {"x","y","cos","sin","avg"};
    String[]complex = {"cos","sin","avg"};
    char coordinate = (char) (new Random().nextInt(2) + 'x');

    String result = "";


    if(levels == 1){
         String value1 = (normal[new Random().nextInt(normal.length)]);

         if (value1.equals("sin") ||value1.equals("cos")){
             result = value1 + "( pi *" + coordinate + ")";
            }
         else if(value1.equals("avg")){
             result = value1 + "(" + coordinate + "," + coordinate + ")" ;
         }
         else{
             result = value1 ;
            }
    }else{
            String value = (complex[new Random().nextInt(complex.length)]);
            if((value.equals("sin") ||value.equals("cos"))&&levels!=0 ){
                result = value + "( pi *" + createFunction(levels - 1) + ")";

            }
             else if(value.equals("avg")&& levels !=0){
                 result = value +"(" + createFunction (levels - (levels-1)) + "," + createFunction (levels - (levels-1)) + ")" ;

            }
            else if(value.equals("avg")&& levels ==2){
                result = value + "(" + createFunction (levels - 1) + "," + coordinate + ")" ;

            }
            else{
             result = value ;
            }


        }


        return result; 

    }

双重功能= .......................;

结果将是" sin(pi * cos(pi * x * y))"在字符串

如何计算此字符串并保持双重功能

5 个答案:

答案 0 :(得分:1)

您正在询问如何解析包含任意表达式的字符串,然后对其进行求值以获得浮点结果。

这非常困难,需要表达式解析器,将字符串转换为表达式树,以及表达式树计算器,以实际计算结果。

答案 1 :(得分:0)

初始化double并直接将值写入该变量是明智的。

Double answer = ....;

当您需要原始值时,只需使用变量answer。当您需要它作为字符串时,只需使用:

String answer_string = String.valueOf(answer);

或者,例如:

System.out.println(String.valueOf(answer));

答案 2 :(得分:0)

Math.sin和Math.cos方法将接受double值,并返回double。只需编写一个以x和y为参数的方法来返回公式:

double myAlgorithm( double x, double y){
return Math.sin(Math.PI*Math.cos(Math.PI*x*y))
}

将x和y作为int传递,因为它将隐式地传递给double

double myAlgorithm( int x, int y){
return Math.sin(Math.PI*Math.cos(Math.PI*x*y))
}

而且,如果你想传递字符串而不是类型:

double myAlgorithm( String x, String y){
return Math.sin(Math.PI*Math.cos(Math.PI*(Double.parseDouble(x).doubleValue())*(Double.parseDouble(y).doubleValue())))
}

答案 3 :(得分:0)

您可以使用Groovy脚本执行此操作。诀窍是将您的输入评估为类似Java的表达式:

public final class Test{
    private static GroovyShell createMathShell() {
        GroovyShell shell = new GroovyShell();

        shell.evaluate("" +
            "cos = {double x -> Math.cos(x)}\n" +   // predefine functions as lambda
            "sin = {double x -> Math.sin(x)}\n" +   // expressions
            "pi = Math.PI\n"                        // define pi
        );
        return shell;
    }

    public static void main(String[] args) {
        GroovyShell shell = createMathShell();

        // set values
        shell.setVariable("x", 0);
        shell.setVariable("y", 1);

        // evaluate
        double result = (Double) shell.evaluate("sin(pi*cos(pi*x*y))"); 

        System.out.println(result);
    }
}

执行此代码将打印:

1.2246467991473532E-16

答案 4 :(得分:-1)

这应该这样做:

Double.valueOf(string);