Problem Statement:

To Evaluate a complex expression in dotnet and show the result to end users. The expression could be something like this X2 +Y3+Log10 (z) .

Solution:

There are different ways to solve the problem stated above. Am planning to show how to use
Scripting Engines like Jscript/VBScript inside C#.

We can make use of Windows Script Control to achieve this. Windows Script control can be downloaded from MSDN site.

It is shipped as an Activex control which lets you make use of JavaScript power inside c#.

Once you install the exe add "msscript.ocx" as reference to your application. It will add Interop.MSScriptControl.dll to the solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSScriptControl;

namespace EvalInCSharpConsole
{
class Program
{
static void Main(string[] args)
{
ScriptControlClass ObjScripEngine = new ScriptControlClass();
ObjScripEngine.Language = "JScript";
string theFormula = "X^2 + Y^3 + Log(Z)";

// Rewriting the formula to work with Java script. Log(Z) ==> Math.Log(Z) and X^2 ==> Math.pow(X,2)

theFormula = " Math.pow(X,2) + Math.pow(Y,3) + Math.log(Z)";

// Its better you rewrite the formula so that you will have a
// variable representer. Out here it is []. It will be easy for you to replace the variable with values.

theFormula = " Math.pow([X],2) + Math.pow([Y],3) + Math.log([Z])";

// Assigning values to the fromula string

//Assign X
theFormula = theFormula.Replace("[X]", "2");

//Assign Y
theFormula = theFormula.Replace("[Y]", "3");

//Assign Z
theFormula = theFormula.Replace("[Z]", "10");

// Eval is the funcation in Javascipt which can evaluate the expressions.

Console.WriteLine(ObjScripEngine.Eval(theFormula).ToString());

Console.Read();
}
}
}
Likewise you can achieve any functionality that you can do in JavaScript. Also windows script control can be used to achieve any VB Script functionality by changing the Language property to VBScript.