【问题标题】:C# sorting file by distance between elementsC#按元素之间的距离对文件进行排序
【发布时间】:2017-04-21 08:04:09
【问题描述】:

我的文本文件看起来像这样

LINE 325,474 195,251 589,821 375,711 Nan Nan Nan
LINE 617,303 578,402 771,724 392,711 Nan Nan Nan
LINE 424,931 472,48 481,203 617,633 Nan Nan Nan

其中第一列基本上是元素名称,第二列是起始X坐标,第三列是起始Y坐标,第四和第五列是结束X和Y坐标,其他不重要。 我需要按每条线之间的距离对它们进行排序。我的代码如下所示:

        string[] Text = File.ReadAllLines(OpenFile.Filename);
        string[,] Word = new string[Text.Length, 8];
        double CurXpos = 0; // for smallest distance set new points (starting from 0,0)
        double CurYpos = 0;
        string[] word = new string[8];
        for (long i = 0; i < Text.Length; i++) // read text 
        {                
            string line = Text[i];               

                for (byte j = 0; j < 8; j++)
                {
                    word = line.Split(' ');
                    Word[i, j] = word[j]; //store text to 2D array
                }
        }
        StreamWriter FileSorted = new StreamWriter(desired destination);
        for (long i = 0; i < Text.Length; i++) // search for minimal distance
        {
            double X1 = Math.Round(double.Parse(Word[i, 1], System.Globalization.CultureInfo.InvariantCulture), 3); // X start possition to double
            double Y1 = Math.Round(double.Parse(Word[i, 2], System.Globalization.CultureInfo.InvariantCulture), 3); //Y , etc.
            double X2 = Math.Round(double.Parse(Word[i, 3], System.Globalization.CultureInfo.InvariantCulture), 3);
            double Y2 = Math.Round(double.Parse(Word[i, 4], System.Globalization.CultureInfo.InvariantCulture), 3);
            double[] XPos = new double[Text.Length]; // array of smallest distances for each line
            double[] YPos = new double[Text.Length];
            double MinDis1 = Math.Sqrt(Math.Pow(X1 - CurXpos, 2) + Math.Pow(Y1 - CurXpos, 2)); //calculation of the smallest distances
            double MinDis2 = Math.Sqrt(Math.Pow(X2 - CurXpos, 2) + Math.Pow(Y2 - CurYpos, 2)); //calculate if end points are closer                
            long PosMin = 0; //position of line with minimum
            double[] AbsMinDis = new double[Text.Length]; // line containing distance data of each line
            if (MinDis1 < MinDis2) // if distance of starting coordinate is smaller than ending, save 
            {
                AbsMinDis[i] = MinDis1;
                XPos[i] = X1;
                YPos[i] = Y1;

            }
            else if (MinDis2 < MinDis1) // if distance of ending points is smaller, swap starting end endinng points and save line possition
            {
                AbsMinDis[i] = MinDis2;
                XPos[i] = X2;
                YPos[i] = Y2;
                Word[i, 1] = X2.ToString();
                Word[i, 2] = Y2.ToString();
                Word[i, 3] = X1.ToString();
                Word[i, 4] = Y1.ToString();                    
            }
            for (long j = 0; j < Text.Length; j++) //sorting file 
            {
                if (AbsMinDis[i] < AbsMinDis[PosMin])
                {                       
                    CurXpos = XPos[i];
                    CurYpos = YPos[i];
                    string swap = Word[PosMin, j];
                    Word[PosMin, j] = Word[i, j];
                    Word[i, j] = swap;
                    PosMin = i;
                }             

现在我不知道我是否有任何错误,或者我不知道如何编写它,因为它看起来像,它对文件没有任何作用 写法是这样的:

            FileSorted.Write(Word[i, 0]);
            for (byte k = 1; k < 8; k++)
            {
                FileSorted.Write(" {0}", Word[i, k]);
            }
            FileSorted.WriteLine();
        }
        FileSorted.Close();

感谢您的时间和帮助。

【问题讨论】:

  • 变量名以小写字母开头会更容易阅读。
  • 你是什么意思?抱歉,我是 C# 新手
  • 那么代码执行的结果是什么?有什么错误吗?空的文件?根本没有文件?
  • @DanielFrühauf - 完全取决于个人喜好和背景。对他的问题丝毫没有帮助。
  • 重写文件,或错误排序@KernelMode

标签: c# arrays file sorting distance


【解决方案1】:

i 的其他迭代(第一次除外)中写入SortedFile 失败,因为您在迭代结束时关闭了文件:

FileSorted.Close();

如果您在完成i 的所有迭代后移动FileSorted.Close(),那么您将看到SortedFile 中的所有行。

行重复是因为在写入注释“//排序文件”的文件时,您正在写入行i 次而不是一次。

here 可以看到大小写约定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2015-08-06
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多