Search code examples
c#windowswindows-phone-8.1equationequation-solving

How to convert a string c# equation to an answer


I am building a WP8 app it is solving numerical analysis problems. For one step i need to parse entered function but I dont have any knowledge about parsing and I tried something else

I have a stackpanel with mathematical components (Sin(x),Cos(x),Tan(x),+,-,*...)

when user click any component

For example first Sin(x) then + and then Cos(x)

string yazılıdenklem =""; int xdegeri=Convert.ToInt32(Xtxt.Text);

private void Sin_Tapped(object sender, TappedRoutedEventArgs e)
    {

        yazılıdenklem=yazılıdenklem+"Math.Sin("+xdegeri.ToString()+")";

    }

 private void artı_Tapped(object sender, TappedRoutedEventArgs e)
    {

        yazılıdenklem = yazılıdenklem + "+";
    }
 private void Cos_Tapped(object sender, TappedRoutedEventArgs e)
    {

        yazılıdenklem = yazılıdenklem + "Math.Cos(" + xdegeri.ToString() + ")";
    }

End of choosing I having a string equation like : "Math.Sin(3)+Math.Cos(3)" can i transform this equation to any numerical form ? Can I get any result from this string eq. ? Thank you


Solution

  • bcParser.NET is a safe eval() function to evaluate math formulas. bcParser.NET is a Math Parser Library for the .NET Platform. bcParser.NET parses and evaluates mathematical expressions given as strings at runtime.

    MathParser.SetExpression("sin(3)+cos(3)");
    double value = MathParser.getValueAsDouble();