美文网首页
php计算考生考试分数2023-09-25

php计算考生考试分数2023-09-25

作者: 阿然学编程 | 来源:发表于2023-09-24 16:59 被阅读0次
    /**
     * @param $questions    考试考题(二维数组)
     * @param $userAnswers  考生答题(二维数组)
     * @return int
     */
    function calculateScore(array $questions, array $userAnswers)
    {
        $totalScore = 0; // 初始化总得分为0
    
        // 索引问题数组以便快速查找(题目ID)
        $indexedQuestions = array_column($questions, null, 'id'); // 使用题目ID作为键,将问题数组重新索引
    
        foreach ($userAnswers as $question) {
            $questionId = $question['id']; // 获取当前问题的ID
            // 检查问题是否存在于问题数组中
            if (isset($indexedQuestions[$questionId])) {
                $questionData = $indexedQuestions[$questionId]; // 获取当前问题的数据
                // 检查用户的答案是否与问题答案匹配
                if (is_array($questionData['answers']) && is_array($question['answers'])) {
                    // 如果问题和用户的答案都是数组
                    if (empty(array_diff($questionData['answers'], $question['answers']))) {
                        // 如果用户的答案和问题答案没有差异
                        $totalScore += $questionData['score']; // 将当前问题的得分加到总得分中
                    }
                } else {
                    if ($questionData['answers'] == $question['answers']) {
                        // 如果问题和用户的答案都是字符串,并且相等
                        $totalScore += $questionData['score']; // 将当前问题的得分加到总得分中
                    }
                }
            }
        }
    
        return $totalScore; // 返回总得分
    }
    
    • 使用示例
        public function test1()
        {
            //问题答案分数
            $a = array(
                array(
                    "id" => 1,//题目id
                    "topic" => "选择题1",
                    "answers" => "a,b",
                    "score" => 10
                ),
                array(
                    "id" => 2,//题目id
                    "topic" => "选择题2",
                    "answers" => "b",
                    "score" => 10
                ),
                array(
                    "id" => 3,
                    "topic" => "选择题3",
                    "answers" => "a",
                    "score" => 10
                ),
                array(
                    "id" => 4,
                    "topic" => "选择题4",
                    "answers" => "c",
                    "score" => 10
                )
            );
            
            //用户答题
            $b = array(
                array(
                    "id" => 1,//题目id
                    "topic" => "选择题1",
                    "answers" => "a,b",
                ),
                array(
                    "id" => 2,//题目id
                    "topic" => "选择题2",
                    "answers" => "c",
                ),
                array(
                    "id" => 3,
                    "topic" => "选择题3",
                    "answers" => "a",
                ),
                array(
                    "id" => 4,
                    "topic" => "选择题4",
                    "answers" => "",
                )
            );
    
            $score = calculateScore($a, $b);
            echo "考试分数:" . $score;
        }
    
    • 示例2(answers数组)
        public function test2()
        {
            $a = array(
                array(
                    "id" => 1,
                    "topic" => "选择题1",
                    "answers" => array("a", "b"),
                    "score" => 10
                ),
                array(
                    "id" => 2,
                    "topic" => "选择题2",
                    "answers" => array("b"),
                    "score" => 10
                ),
                array(
                    "id" => 3,
                    "topic" => "选择题3",
                    "answers" => array("a"),
                    "score" => 10
                ),
                array(
                    "id" => 4,
                    "topic" => "选择题4",
                    "answers" => array("c"),
                    "score" => 10
                )
            );
    
            $b = array(
                array(
                    "id" => 1,
                    "topic" => "选择题1",
                    "answers" => array("a", "b"),
                ),
                array(
                    "id" => 2,
                    "topic" => "选择题2",
                    "answers" => array("c"),
                ),
                array(
                    "id" => 3,
                    "topic" => "选择题3",
                    "answers" => array("d"),
                ),
                array(
                    "id" => 4,
                    "topic" => "选择题4",
                    "answers" => array("c"),
                )
            );
    
    
            // 调用函数计算得分
            $score = calculateScore($a, $b);
            echo "得分:" . $score;
        }
    

    相关文章

      网友评论

          本文标题:php计算考生考试分数2023-09-25

          本文链接:https://www.haomeiwen.com/subject/kizfbdtx.html