all i want code when user
write any equations like this "(x^2+x)/(x*x)"
in text box 1
and write the value "x" in text box 2 , he has the value of this equation in message box when he press ok.
please, help me to get this code
and thank u .
and my problem , How can i write power expression in this code:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Evaluate(textBox1.Text.Replace("x",textBox2.Text)).ToString());
}
public static double Evaluate(string expression)
{
return (double)new System.Xml.XPath.XPathDocument
(new StringReader("
(string.Format("number({0})", new
System.Text.RegularExpressions.Regex(@"([\+\-])")
.Replace(expression, " ${1} ")
.Replace("/", " div ")
AlanPosted Sep 26, 2007, 10:26 AM
Unfortunately, XPath doesn't have either an operator or built-in function which can cope with 'power' calculations. In this particular case, you could add the following to your expression:
.Replace("x^2", "x*x")
However, if the variable can be called something other than 'x' or the power can be an arbitrary number, then clearly you need a different solution to this.
You might be better using VBScript's Eval() function in the Microsoft Scripting Control as this can cope with the '^' operator directly.