I am making a program in C# that takes an equation from the user input and evaluates it. At the moment I have a way of it calculating the user input and also taking values from stored variables such as x. The equation the will enter will be something like 2.0 * 78 * 4X / 3
.
At the moment I can replace X
with the value associated with it. However I need a way of searching the string and finding X
, then if X
has a number before it, multiplying X * X
that number of times. In the example above, that would be 4 times.
Would it be best to use the IndexOf
method or split the string into sub strings?
All I want is to be able to find weather X
has a number before it; if not then just output the value of X
, else if X
has a number before it multiply X*X
said times.
This is the code I have for finding X
to the data passed,. Ideally I would like to contain any code withing the following:
if (name == "x" || name == "X")
{
args.Result = x; // Returns value of x
}
This is all rest of the code
var e = new Expression(input);
// Set up a custom delegate so NCalc will ask you for a parameter's value
// when it first comes across a variable
e.EvaluateParameter += delegate(string name, ParameterArgs args)
{
if (name == "x" || name == "X")
{
args.Result = x; // Returns value of x
}
else if (name == "y" || name == "Y")
{
//....
}
else if (name == "z" || name == "Z")
{
//....
}
// Or if the names match up you might be able to something like:
// args.Result = dataRow[name];
};
//var result =
return e.Evaluate();