<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>评测结果</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 20px; }
  </style>
</head>
<body>
  <h1>评测结果</h1>
  <div id="result"></div>
  
  <script>
    let questions = [];
    fetch('questions.json')
      .then(response => response.json())
      .then(data => {
        questions = data;
        calculateResult();
      })
      .catch(error => console.error("加载题目失败：", error));
    
    function calculateResult(){
      const savedAnswers = JSON.parse(localStorage.getItem('answers') || '{}');
      let total = questions.length;
      let correctCount = 0;
      let resultDetails = '';
      
      questions.forEach((q, idx) => {
        // 从 correctAnswer 中提取正确选项字母，如 "(C)" 得到 C
        const correctMatch = q.correctAnswer.match(/^\((.)\)/);
        const correctLetter = correctMatch ? correctMatch[1] : '';
        const userAnswer = savedAnswers[idx] || '';
        const isCorrect = (userAnswer === correctLetter);
        if(isCorrect) correctCount++;
        resultDetails += '题目 ' + (idx + 1) + ': ' + (isCorrect ? '正确' : '错误') + '<br>';
      });
      const accuracy = ((correctCount / total) * 100).toFixed(2);
      document.getElementById('result').innerHTML = `
          <p>总题数: ${total}</p>
          <p>正确题数: ${correctCount}</p>
          <p>准确率: ${accuracy}%</p>
          <hr>
          <h3>详细结果：</h3>
          ${resultDetails}
      `;
    }
  </script>
</body>
</html>
