Iは、第二、第三は、Y座標出発第四及び第五されるX座標起動され、最初の列は、基本的エレメントの名前でこの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
のように見えるテキストファイルを有します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#に新しいです –
だからコード実行の結果は何ですか?任意のエラー?空のファイルですか?ファイルはまったくありませんか? – KernelMode