Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16340


Ignore:
Timestamp:
12/06/18 16:04:56 (6 years ago)
Author:
chaider
Message:

#2968 Changed formating of constant list

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2968_infix_string_formatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionStringFormatter.cs

    r16339 r16340  
    1515  public sealed class InfixExpressionStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    1616    private readonly List<KeyValuePair<string, double>> constants;
    17     private static int constantCounter = 0;
     17    private int constantCounter = 0;
    1818
    1919    [StorableConstructor]
     
    2222      : base(original, cloner) {
    2323      constants = new List<KeyValuePair<string, double>>(original.constants);
     24      constantCounter = original.constantCounter;
    2425    }
    2526    public InfixExpressionStringFormatter() : base() {
     
    3435    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
    3536      StringBuilder strBuilder = new StringBuilder();
     37      constants.Clear();
     38      constantCounter = 0;
    3639      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
    3740      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);
    3844      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);
    4048      }
    4149      return strBuilder.ToString();
     
    169177    }
    170178
     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
    171186    private string GetToken(ISymbol symbol) {
    172187      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).SingleOrDefault();
Note: See TracChangeset for help on using the changeset viewer.