2017-04-21 55 views
1

我有文本文件,它看起來像這樣C#由距離元件之間排序文件

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 

當第一列基本上name元素,第二開始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(); 

謝謝你的時間和幫助。

+0

如果你在開頭用小寫字母寫變量名,它會更容易閱讀。 –

+0

你是怎麼意思的?對不起,我是C#的新手, –

+0

那麼代碼執行的結果是什麼?有什麼錯誤?空的文件?根本沒有文件? – KernelMode

回答

0

有一個失敗的i其他迭代寫SortedFile(除第一),因爲你在迭代結束時關閉文件:

FileSorted.Close(); 

如果移動FileSorted.Close()您與完成後,所有的迭代i,那麼您將看到SortedFile中的所有行。

行重複是因爲當寫入評論「//排序文件」的文件時,您正在寫入行i而不是一次。

here你可以看到大小寫約定。