如何正确处理数组

时间:2014-05-12 10:15:17

标签: c# arrays wpf string

如果您想查看此问题的背景,请参阅我的问题的第1部分:

Detecting brackets in input string

如果标题不匹配,请原谅我,因为我也很困惑如何恰当地命名它来描绘我的问题。如果有人知道更合适的标题,请随时编辑。

所以,给出下面的代码(我自己的代码):

    private const int PARTICLE_EACH_CHAR = 4;

    /*ProcessBarLines : string s only contains numbers, b, [,  and ]*/
    private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
    {
        List<int> bracket = new List<int>();
        List<int> other = new List<int>();
        int currentCloseNumber = 0;
        int currentOpenNumber = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '[')
            {
                bracket.Add(i);
                currentOpenNumber++;
                if (i - 1 > 0 && s[i - 1] != '[')
                {
                    currentOpenNumber = 1;
                }
            }
            else if (s[i] == ']')
            {
                bracket.Add(i);
                currentCloseNumber++;

                if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
                {
                    int min = bracket.Count - (currentCloseNumber * 2);
                    int max = bracket[bracket.Count - 1];

                    List<int> proc = new List<int>();

                    int firstIndex = -1;
                    int lastIndex = -1;

                    for (int ii = 0; ii < other.Count; ii++)
                    {
                        if (other[ii] > min && other[ii] < max)
                        {
                            proc.Add(other[ii]);

                            if (firstIndex == -1)
                            {
                                firstIndex = ii;
                                lastIndex = ii;
                            }
                            else
                            {
                                lastIndex = ii;
                            }
                        }
                    }

                    double leftPixel = firstIndex * widthEachChar;
                    double rightPixel = (lastIndex * widthEachChar) + widthEachChar;

                    DrawLine(canvas, currentCloseNumber, leftPixel,
                        rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);

                    lastLineAboveNotation += currentCloseNumber - 1;

                    currentOpenNumber -= currentCloseNumber;
                    currentCloseNumber = 0;
                }
            }
            else
            {
                other.Add(i);
            }
        }

        return lastLineAboveNotation + 1;
    }

以下是测试用例:

Picture 1

Picture 2

Picture 3

图1&amp; 2是正确的答案,图3是错误的答案。图3应该有一条线,就像从数字2反转一样,但显然,(如果你仔细观察)线条是在右侧绘制的,但它应该在左侧是正确的(在0之上)

我想,问题是,我很确定“min”。因为它没有给出正确的起始值。

对此有何想法?随意澄清任何事情。它用于编写数字乐谱。

顺便说一下,DrawLine()只是想在数字上方画出一条线,这不是问题。

1 个答案:

答案 0 :(得分:0)

最后!我找到了!

    private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
    {
        List<int> bracket = new List<int>();
        List<int> other = new List<int>();
        int currentCloseNumber = 0;
        int currentOpenNumber = 0;
        int space = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '[')
            {
                bracket.Add(i);
                currentOpenNumber++;
                if (i - 1 > 0 && s[i - 1] != '[')
                {
                    currentOpenNumber = 1;
                }
            }
            else if (s[i] == ']')
            {
                bracket.Add(i);
                currentCloseNumber++;

                if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
                {

                    int min = bracket[Math.Max(bracket.Count - ((currentCloseNumber * 2) + space), 0)];
                    int max = bracket[bracket.Count - 1];

                    space = max - min - 1;

                    List<int> proc = new List<int>();

                    int firstIndex = -1;
                    int lastIndex = -1;

                    for (int ii = 0; ii < other.Count; ii++)
                    {
                        if (other[ii] > min && other[ii] < max)
                        {
                            proc.Add(other[ii]);
                            other[ii] = -1;
                            if (firstIndex == -1)
                            {
                                firstIndex = ii;
                                lastIndex = ii;
                            }
                            else
                            {
                                lastIndex = ii;
                            }
                        }
                    }

                    double leftPixel = firstIndex * widthEachChar;
                    double rightPixel = (lastIndex * widthEachChar) + widthEachChar;

                    DrawLine(canvas, currentCloseNumber, leftPixel,
                        rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);

                    lastLineAboveNotation += 1;

                    currentOpenNumber -= currentCloseNumber;
                    currentCloseNumber = 0;
                }
            }
            else
            {
                other.Add(i);
            }
        }

        return lastLineAboveNotation + 1;
    }

如果有人提供更有效的代码,请告诉我们!