Skip to content

Commit

Permalink
Ajout d'un convertisseur de variable "à la volée"
Browse files Browse the repository at this point in the history
  • Loading branch information
FaustVX committed Oct 14, 2013
1 parent 1e86dd0 commit 282405e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MathString.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void Main(string[] args)
string formule = line;
try
{
string result = m.Convert(formule);
string result = m.Convert(formule, @var => new MathString.VariableConvertor((float)new Random().NextDouble(), true));
Console.WriteLine("\"{0}\" = {1}", formule, result);
}
catch (Exception ex)
Expand Down
42 changes: 39 additions & 3 deletions MathString/MathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ public int GetHashCode(Variable obj)
return (obj.Name.GetHashCode() + obj.Value.GetHashCode()).GetHashCode();
}
}

public class VariableConvertor
{
#region Attributs
private readonly bool _success;
private readonly float _value;
#endregion

#region Constructeur
public VariableConvertor(float value, bool success)
{
_value = value;
_success = success;
}

#endregion

#region Proprietes
public float Value
{
get { return _value; }
}

public bool Success
{
get { return _success; }
}
#endregion
}
#endregion

#region Attributs
Expand Down Expand Up @@ -266,22 +295,22 @@ public static void AddGlobalVariable(string name, Function function)
Functions.Add(name, function);
}

public string Convert(string text, params Variable[] variables)
public string Convert(string text, Func<string, VariableConvertor> variableConvertor, params Variable[] variables)
{
text = text.Replace(" ", "");
int max = _mathFunc.Values.Max(t => t.Weigth);
var pair = _mathFunc.First(kvp => kvp.Value.Operator == "number");
for (Match ma = pair.Key.Match(text); ma.Success; ma = ma.NextMatch())
text = text.Replace(ma.Value, pair.Value.Action(ma.Value));

text = FindVariables(text, variables);
text = FindVariables(text, variables, variableConvertor);

text = FindFunctions(text);

return FindParenthesis(ref text, max, '(', ')') ? text : Calculate(text, max);
}

private string FindVariables(string text, ICollection<Variable> variables)
private string FindVariables(string text, ICollection<Variable> variables, Func<string, VariableConvertor> variableConvertor)
{
Regex varRegex = new Regex(VariableRegex);
IList<string> errors = new List<string>();
Expand All @@ -305,6 +334,13 @@ private string FindVariables(string text, ICollection<Variable> variables)
}
else
{
var varError = variableConvertor(ma.Groups["nom"].Value);
if (varError.Success)
{
text = text.Replace(ma.Value, varError.Value.ToString().Replace(',', '.'));
cont = false;
continue;
}
errors.Add(string.Format("{0}: c({1})", ma.Groups["nom"].Value, ma.Index));
cont = true;
}
Expand Down

0 comments on commit 282405e

Please sign in to comment.