显示两个数组之间常见和非常见元素的总数?

时间:2012-08-27 07:20:51

标签: c# arrays linq intersect except

我有两个数组:array testAnswer保存“考试答案”,数组inputAnswers保存“学生答案”。

当我运行我的代码时,它会显示两个数组的所有常见元素(正确的答案)和不常见的元素(错误的答案)。但是,我希望能够显示正确/错误答案的总数,而不是实际显示正确/不正确的答案。

到目前为止我的代码:

private void button1_Click(object sender, EventArgs e)
{
    //Array holding answers to test
    string[] testAnswer = new string[20] { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
    string a = String.Join(", ", testAnswer);

    //Reads text file line by line. Stores in array, each line of the file is an element in the array
    string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");

    string b = String.Join(", ", inputAnswer);

    //Increments through array elements in both arrays and checks for matching elements. Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] == inputAnswer[i])
            listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
    }

    //Increments through array elements in both arrays and checks for uncommon elements. Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] != inputAnswer[i])
            listBox2.Items.Add(inputAnswer[i]);
    }
}

3 个答案:

答案 0 :(得分:4)

以下是使用LINQ获取结果的方法:

var results = 
    testAnswer
        .Zip(inputAnswer, (t, i) => new { t, i })
        .Aggregate(new { Correct = 0, Incorrect = 0 },
            (a, ti) => new
            {
                Correct = a.Correct + (ti.t == ti.i ? 1 : 0), 
                Incorrect = a.Incorrect + (ti.t != ti.i ? 1 : 0)
            });

它会产生一个带有这种结果的匿名变量:

anonymous variable result

另一种方法是:

var query = 
    testAnswer
        .Zip(inputAnswer, (t, i) => t == i)
        .ToLookup(x => x);

var results = new
{
    Correct = query[true].Count(),
    Incorrect = query[false].Count()
};

答案 1 :(得分:0)

以下代码将在最后提供2个整数,这些整数将保留答案:

private void button1_Click(object sender, EventArgs e)
{
    string[] testAnswer = new string[20] { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
    string a = String.Join(", ", testAnswer);



    //Reads text file line by line. Stores in array, each line of the file is an element in the array
    string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");

    string b = String.Join(", ", inputAnswer);


    //Increments through array elements in both arrays and checks for matching elements. 
    //Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] == inputAnswer[i])
            listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
        else 
            listBox2.Items.Add(inputAnswer[i]);
        }

    int correctAns = listbox1.Items.Count;
    int wringAns = listbox2.Items.Count;
}

答案 2 :(得分:-1)

常见答案计数为Enumerable.Intersect结果项目计数,非常见 - Enumerable.Except结果项目计数。

更新:只要在评论中提到它会产生错误的答案,证明它不会:

  var testAnswers = new[] { 1, 2, 3 };
  var inputAnswers = new[] { 3, 2, 1 };
  var commonAnswers = testAnswers
            .Select((x, index) => Tuple.Create(x, index))
            .Intersect(inputAnswers.Select((y, index) => Tuple.Create(y, index)));