24 game/CSharp: Difference between revisions

m
removed some duplication caused by copy/paste, edited intro text, homogenized open bracket placement
mNo edit summary
m (removed some duplication caused by copy/paste, edited intro text, homogenized open bracket placement)
Line 4:
You could, for example, use the CodeDOM to dynamically compile an object that contains the expression string.
 
WhileOr, while not necessarily a good coding practice, but certainly a short and simple route, would beyou tocould use System.Xml.XPath.XPathNavigator.Evaluate(string xpath) as shown here:
<lang csharp>
public class XPathEval : I24MathParser {
Line 33:
Here is a more verbose, native solution - a lightweight math expression parser for evaluating 24 Game user input:
<lang csharp>
/// <summary>
/// Lightweight math parser - C# does not have an Evaluate function
/// </summary>
public class MathParser : I24MathParser {
/// <summary>
/// Lightweight math parser - C# does not have an Evaluate function
Line 77 ⟶ 73:
 
 
float Solve(string equation) {
//carry out order of operations
// bracketed subexpressions - for any operator
Line 96 ⟶ 92:
Match match = subExpression.Match(equation);
 
while (match.Success) {
if (match.Groups[1].Length > 0) {
{
if (match.Groups[1].Length > 0)
{
//recursively solve for subexpressions -- match group 1 excludes outer brackets
subResult = Solve(match.Groups[1].Value);
}
else {
{
//no more nested expressions - get final result for this subExpression
subResult = ParseEquation(match.Value);
Anonymous user