The following is working for me but I want to include numbers in scientific
notation.
public double Evaluate( string expr )
{
const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)"
Regex reMulDiv = new Regex(Num + @"\s*([*/])\s*" + Num);
other stuff:
while ( reMulDiv.IsMatch( expr ) )
{
Regex restar = new Regex(@"\*");
string bstr = reMulDiv.Match(expr).Value.ToString();
bstr = restar.Replace(bstr, "\\*");
string a=reMulDiv.Match(expr).Groups.SyncRoot.ToString();
string astr = DoMulDiv(reMulDiv.Match(expr));
Regex rx = new Regex(bstr);
expr = rx.Replace(expr, astr);
}
other stuff
return (Convert.ToDouble(expr));
}
public string DoMulDiv( Match m )
{
int i = 0;
double n1 = Convert.ToDouble(m.Groups[1].Value);
double n2 = Convert.ToDouble(m.Groups[3].Value);
switch (m.Groups[2].Value.ToString())
{
case "/":
return ( n1 / n2 ).ToString();
case "*":
return ( n1 * n2 ).ToString();
default:
return "";
}
}
Trying to scale-up to include numbers in sci notation.
const string Num = @"((\-?\d+\.?\d*|\-?\.\d+)([E][-+]?[0-9]+)?)";
Trouble is n1, n2 ,n3 are nolonger the first#, the operator, and the last#.
The number of groups varies and the position of the numbers and operator is
not predictable. I think my problem is that I am not implementing the
function properly. But the rework (without a hack) is evading me.
please help