3

現在、通常のループを使用して、番号のリストが正しいかどうかを確認しています。

私は現在LINQを学んでおり、LINQで実装して数字のシーケンスが正しい順序であるかどうかを確認する方法を知りたいです。

たとえば、次のシーケンス番号のリストがあります。

1.0
1.1
1.2
1.4
2.0

1.3 がないため、プログラムは行 1.4 にエラーとしてフラグを立てる必要があります。

LINQを使用してそれを達成するにはどうすればよいですか?

ご助力いただきありがとうございます。:)

目次のようなものです:

1.1 の後に 1.3 は無効です。1 の後に 2 は有効です。1.4 の後に 2 が有効です。

これが私が使用しているコードで、まだ多くの失効があると思います:

using (System.IO.StreamReader reader = new System.IO.StreamReader("D:\\test.txt"))
{
    double prevNumber = 0;

    while (reader.Peek() >= 0)
    {
        double curNumber = double.Parse(reader.ReadLine());
        double x = Math.Round(curNumber - prevNumber, 1);

        if (x == 0.1)
        {
            prevNumber = curNumber;
        }

        else
        {
            int prev = (int)Math.Floor(prevNumber);
            int cur = (int)Math.Floor(curNumber);

            if ((cur - prev) == 1)
            {
                prevNumber = curNumber;
            }
            else
            {
                //error found
            }
        }
    }
}
4

2 に答える 2

2

このメソッドはファイル名を受け取り、正しくないバージョンの行番号の配列を返します。あなたの例では、それは を返します{ 4 }

x.y処理したいのはそれだけのように見えるため、フォームの数値のみを処理します。

static int[] IncorrectLines(string filename)
{
    // Parse the file into an array of ints, 10* each version number.
    var ints =  File.ReadLines(filename)
        .Select(s => (int)(10 * decimal.Parse(s))).ToArray();
    // Pair each number up with the previous one.
    var pairs = ints
        .Zip(ints.Skip(1), (p, c) => new { Current = c, Previous = p });
    // Include the line numbers
    var withLineNos = pairs
        .Select((pair, index) => new { Pair = pair, LineNo = index + 2 });
    // Only keep incorrect lines
    var incorrect = withLineNos.Where(o => ! (         // not one of either:
            o.Pair.Current - 1 == o.Pair.Previous ||   // simple increment
            (o.Pair.Current % 10 == 0 &&               // major increment
             (o.Pair.Current / 10) - 1 == o.Pair.Previous / 10)
        ));
    return incorrect.Select(o => o.LineNo).ToArray();
}

本音をいうと?ループの方がいいと思います。

于 2012-12-06T14:50:56.087 に答える
1

したがって、私が正しく理解していれば、(小数点以下 1 桁の精度で) double の並べ替えられたリストをループ処理し、整数に小数点以下の桁数が存在する場合、それらの差が 0.1 を超えないかどうかを判断する必要があります。 .

リストがソートされていると仮定します。

List<double> contents = new List<double>() {1.0, 1.1, 1.2, 1.4, 2.0};

そのリストで IsValid を呼び出します。

bool IsValid(List<double> contents) {
  //Get the distinct whole numbers
  var wholeNumbers = contents.Select(t => Math.Floor(t)).Distinct();
  foreach (var num in wholeNumbers) {

    //Get the "subcontents" for this whole number (chapter)
    var subContents = contents.Where(t => Math.Floor(t) == num).ToList();
    for (int i = 0; i < subContents.Count() - 1; i++) {

      //If the subcontents are different by something other than 0.1, it's invalid
      if (subContents.Count() > 1 && Math.Round(subContents[i + 1] - subContents[i], 1) != 0.1) {
        return false;
      }
    }
  }
  return true;
}

(サブカテゴリが 1.14、1.24、1.34 などの場合でも、それは有効であると見なされることに注意してください。)

于 2012-12-06T14:28:45.343 に答える