代码之家  ›  专栏  ›  技术社区  ›  bugBurger

使用C重新排序编号的优化方法#

  •  3
  • bugBurger  · 技术社区  · 16 年前

    我在asp.net页面(C#)上显示了以下项目。

    欢迎光临
    2.有什么新鲜事

    2.2想法
    2.3其他

    2.3.2船只
    2.4车辆
    2.5水果

    现在用户可以删除任何子项(对于前用户,不是root用户可以删除项2.1礼物或项2.3.1新颖性),一旦用户删除项,我需要使用C#重新记忆结构。我正在寻找任何建议/想法/代码来完成这项任务。


    2.有什么新鲜事
    2.1礼物(移除)

    2.3其他


    2.4车辆
    2.5水果

    欢迎光临
    2.有什么新鲜事

    2.2其他
    2.2.1新颖性(移除)
    2.2.2船只
    2.3车辆
    2.4水果

    5 回复  |  直到 16 年前
        1
  •  5
  •   Lucero    16 年前

    对于用户来说,数字是纯粹的“光学”糖吗?如果是的话,我会在写输出时动态地给它们编号,而不会将编号存储在任何地方。

        2
  •  2
  •   Lucero    16 年前

        3
  •  2
  •   Daniel Brückner    16 年前

    这不是一个非常聪明的解决方案,但下面的代码将作为输入的行列表重新编号。它在分隔编号和其他编号的空间处拆分每一行,通过计算编号中的点数确定嵌套级别,并将此信息与最后一行的嵌套级别一起使用,以使用每个嵌套级别的当前编号更新列表。最后,存储在列表中的当前编号与文本合并并输出。

    List<Int32> numbering = new List<Int32>();
    
    Int32 lastNestingLevel = -1;
    
    foreach (String line in lines)
    {
        String[] parts = line.Split(new Char[] { ' ' }, 2);
    
        Int32 currentNestingLevel = parts[0].Count(c => c == '.');
    
        if (currentNestingLevel > lastNestingLevel)
        {
            // Start a new nesting level with number one.
            numbering.Add(1);
        }
        else if (currentNestingLevel == lastNestingLevel)
        {
             // Increment the number of the current nesting level.
            numbering[currentNestingLevel] += 1;        }
        else if (currentNestingLevel < lastNestingLevel)
        {
             // Remove the deepest nesting level...
            numbering.RemoveAt(numbering.Count - 1);
             // ...and increment the numbering of the current nesting level.
            numbering[currentNestingLevel] += 1;
        }
    
        lastNestingLevel = currentNestingLevel;
    
        String newNumbering = String.Join(".", numbering
            .Select(n => n.ToString())
            .ToArray());
    
        Console.WriteLine(newNumbering + " " + parts[1]);
    }
    

    对于以下输入

    List<String> lines = new List<String>()
    {
        "1 Welcome",
        "2 Whats New",
        //"2.1 Gifts",
        "2.2 Ideas",
        "2.3 Others",
        //"2.3.1 Novelty",
        "2.3.2 Boats",
        "2.4 Vehicals",
        "2.5 Fruits"
    };
    

    输出如下。

    1 Welcome
    2 Whats New
    2.1 Ideas
    2.2 Others
    2.2.1 Boats
    2.3 Vehicals
    2.4 Fruits
    

    使现代化

    Dictionary<Int32, Int32> numbering = new Dictionary<Int32, Int32>();
    
    Int32 lastNestingLevel = -1;
    
    foreach (String line in lines)
    {
        String[] parts = line.Split(new Char[] { ' ' }, 2);
    
        Int32 currentNestingLevel = parts[0].Count(c => c == '.');
    
        if (currentNestingLevel > lastNestingLevel)
        {
            numbering[currentNestingLevel] = 1;
        }
        else
        {
            numbering[currentNestingLevel] += 1;
        }
    
        lastNestingLevel = currentNestingLevel;
    
        String newNumbering = String.Join(".", numbering
            .Where(n => n.Key <= currentNestingLevel)
            .OrderBy(n => n.Key)
            .Select(n => n.Value.ToString())
            .ToArray());
    
        Console.WriteLine(newNumbering + " " + parts[1]);
    }
    

    此变体还修复了第一个版本中的错误。如果嵌套级别一次下降一个以上,则第一个变量将产生错误输出。

    2.3.2.1 Vehicals
    2.5 Fruits <= nesting level drops by two
    

        4
  •  1
  •   Giffyguy    16 年前

    我建议使用多维集合。因此,当您删除“2”集合(2.1-礼物)中的第一个项目时,“创意”将自动成为该集合中的第一个新项目,从那时起将被解读为2.1而不是2.2


    这是你必须注意的事情。。。


    您将有一个包含一级项目的列表“L1”。
    它看起来是这样的:

    List L1[2]
      List Welcome[0]
      List What's New[5]
        List Gifts[0]
        List Ideas[0]
        List Others[2]
          List Novelty[0]
          List Boats[0]
        List Vehicles[0]
        List Fruits[0]
    

    实现此结构的最简单方法之一是创建一个“Item”类,该类包含ItemName的字符串以及子项列表。这样一来,每件物品都有可能生孩子,或者没有孩子。

        5
  •  1
  •   CAbbott    16 年前

    我想出了一个课程来保存内容:

    public class Content
    {
        public string Title { get; set; }
        public IList<Content> SubContent
        {
            get;
            private set;
        }
    
        private Content()
        {
        }
    
        public Content(string title)
        {
            Title = title;
            SubContent = new List<Content>();
        }
    }
    

    然后使用递归函数对内容进行编号/显示:

        private static void Display(string marker, IList<Content> content)
        {
            int count = 0;
            foreach (Content c in content)
            {
                string label = marker + (marker.Length > 0 ? "." : "") + (++count);
                Console.WriteLine(label + " " + c.Title);
                if (c.SubContent.Count > 0)
                    Display(label, c.SubContent);
            }
        }