Changeset 16340 for branches/2968_infix_string_formatter
- Timestamp:
- 12/06/18 16:04:56 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2968_infix_string_formatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionStringFormatter.cs
r16339 r16340 15 15 public sealed class InfixExpressionStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 16 16 private readonly List<KeyValuePair<string, double>> constants; 17 private staticint constantCounter = 0;17 private int constantCounter = 0; 18 18 19 19 [StorableConstructor] … … 22 22 : base(original, cloner) { 23 23 constants = new List<KeyValuePair<string, double>>(original.constants); 24 constantCounter = original.constantCounter; 24 25 } 25 26 public InfixExpressionStringFormatter() : base() { … … 34 35 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 35 36 StringBuilder strBuilder = new StringBuilder(); 37 constants.Clear(); 38 constantCounter = 0; 36 39 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder); 37 40 strBuilder.Append($"{Environment.NewLine}{Environment.NewLine}"); 41 42 int maxDigits = GetDigits(constantCounter); 43 int padding = constants.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length); 38 44 foreach (var constant in constants) { 39 strBuilder.Append($"{constant.Key} = {constant.Value}{Environment.NewLine}"); 45 int digits = GetDigits(Int32.Parse(constant.Key.Substring(2))); 46 strBuilder.Append($"{constant.Key}{new String(' ', maxDigits - digits)} = " + 47 string.Format($"{{0,{padding}:F12}}", constant.Value, CultureInfo.InvariantCulture) + Environment.NewLine); 40 48 } 41 49 return strBuilder.ToString(); … … 169 177 } 170 178 179 private int GetDigits(int x) { 180 if (x < 10) 181 return 1; 182 else 183 return (int)Math.Floor(Math.Log10(x) + 1); 184 } 185 171 186 private string GetToken(ISymbol symbol) { 172 187 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).SingleOrDefault();
Note: See TracChangeset
for help on using the changeset viewer.