字符串格式化为C#中的沙子时钟

时间:2017-05-21 17:48:31

标签: c# string recursion

说我有一个看起来像这样的字符串:

*****
***
*
***
*****

我试图以这样的方式对其进行格式化:

*****
 ***
  *
 ***
*****

我正在使用递归来构建这个字符串,这个长度是5但它可以接收任何奇数作为高度,代码是:

public static StringBuilder GenerateSandClockWithInput(StringBuilder stringBuilder, uint height)
{
    if (height == 1)
    {
        stringBuilder.AppendLine("*");
        return stringBuilder;
    }

    string aestricsLine = NumberToAestrics(height);
    stringBuilder.AppendLine(aestricsLine);
    GenerateSandClockWithInput(stringBuilder, height - 2);
    stringBuilder.AppendLine(aestricsLine);
    return stringBuilder;
}

public static string NumberToAestrics(uint numberOfAestrics)
{
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < numberOfAestrics; i++)
    {
        stringBuilder.Append("*");
    }
    return stringBuilder.ToString();
}

3 个答案:

答案 0 :(得分:1)

您可以尝试这样做,如果这不是必需的,您不必返回该字符串构建器。它是引用类型,在此代码中您将始终使用相同的类型:

class Program
{
    public static void Main()
    {
        var clockBuilder = new StringBuilder();
        GenerateSandClock( clockBuilder, 13 );
        Console.Write(clockBuilder.ToString() );
        Console.ReadKey();
    }

    private static void GenerateSandClock(StringBuilder stringBuilder, uint height, uint indentionLevel = 0)
    {
        var line = NumberToSpaces( indentionLevel ++ ) + NumberToAestrics( height );
        stringBuilder.AppendLine(line);

        if (height == 1)
        {
            return;
        }

        GenerateSandClock(stringBuilder, height - 2, indentionLevel);
        stringBuilder.AppendLine(line);
    }

    private static string NumberToSpaces( uint indentionLevel)
    {
        return new string( ' ', (int)indentionLevel);
    }

    private static string NumberToAestrics(uint numberOfAestrics)
    {
        return new string( '*', (int)numberOfAestrics );
    }
}

答案 1 :(得分:1)

尝试followi

        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder();
            int height = 5;
            stringBuilder =  GenerateSandClockWithInput(stringBuilder, 0, height);
        }
        public static StringBuilder GenerateSandClockWithInput(StringBuilder stringBuilder, int row, int height)
        {
            if (row == height) return stringBuilder;
            if (row < height / 2)
            {
                stringBuilder.AppendLine(new string(' ', row) + new string('*', height - (2 * row)));
            }
            else
            {
                stringBuilder.AppendLine(new string(' ', height - row - 1) + new string('*', (2* row) - height + 2));
            }
            GenerateSandClockWithInput(stringBuilder, row + 1, height);
            return stringBuilder;
        }

ng:

答案 2 :(得分:0)

计算每行之前应添加的空白数,并使用new string(char, count)构造函数在每行之前添加空字符串。

您可以使用String.SplitLinqString.Join来完成此操作。这是代码。

制作原始字符串:

static string Make(int height)
{
    var lines = Enumerable.Range(-height, 2 * height + 1)
                          .Where(i => Math.Abs(i) % 2 == 1)
                          .Select(i => new string('*', Math.Abs(i)));

    return string.Join(Environment.NewLine, lines);
}

对齐中心

static string AlignCenter(string input)
{
    var lines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

    var maxLength = (int)(lines.Max(i => i.Length));

    var result = lines.Select(i => $"{new string(' ', (maxLength - i.Length + 1) / 2) }{i}").ToList();

    return string.Join(Environment.NewLine, result);
}

<强>用法

Console.WriteLine(Make(5));

Console.WriteLine(AlignCenter(Make(5)));