From 53d6d7dca47297eb4d17aec9ed373e9ea653864a Mon Sep 17 00:00:00 2001 From: ppnhs9f3g <3524393152@qq.com> Date: Fri, 11 Oct 2024 16:46:21 +0800 Subject: [PATCH] ADD file via upload --- index.js | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..4fa0298 --- /dev/null +++ b/index.js @@ -0,0 +1,210 @@ +let originalData = []; +let updatedData = []; +let workbook; +let sheetName; +let skippedStudents = []; +// 导入学生名单 +document.getElementById('excelFileInput').addEventListener('change', function (e) { + var file = e.target.files[0]; + if (!file) { + return; + } + var reader = new FileReader(); + reader.onload = function (e) { + var data = new Uint8Array(e.target.result); + workbook = XLSX.read(data, { type: 'array' }); + sheetName = workbook.SheetNames[0]; + var worksheet = workbook.Sheets[sheetName]; + originalData = XLSX.utils.sheet_to_json(worksheet); + updatedData = JSON.parse(JSON.stringify(originalData)); + var displayDiv = document.getElementById('excelDataDisplay'); + // 添加表头 + displayDiv.innerHTML = '
学号姓名积分
'; + updatedData.forEach(function (row) { + var rowDiv = document.createElement('div'); + Object.keys(row).forEach(key => { + var cellDiv = document.createElement('span'); + cellDiv.textContent = row[key] + ' '; + rowDiv.appendChild(cellDiv); + }); + displayDiv.appendChild(rowDiv); + }); + }; + reader.readAsArrayBuffer(file); +}); + +// 点名按钮 +document.getElementById('callButton').addEventListener('click', function () { + // 去除上一次的高亮 + var displayDiv = document.getElementById('excelDataDisplay'); + Array.from(displayDiv.children).forEach(child => child.style.backgroundColor = ''); + + // 过滤掉因上次回答问题得分超过2.5而跳过的学生 + let eligibleStudents = updatedData.filter(student => !skippedStudents.includes(student)); + + // 计算总积分 + let totalPoints = eligibleStudents.reduce((acc, student) => { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + return acc + (pointsKey ? parseInt(student[pointsKey]) : 0); + }, 0); + + // 确保总积分不为零 + if (totalPoints === 0) { + // 如果总积分为零,可以随机选择一个学生或者采取其他处理方式 + let randomIndex = Math.floor(Math.random() * eligibleStudents.length); + let selectedStudent = eligibleStudents[randomIndex]; + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + var selectedRow = rows.find(row => row.querySelector('span').textContent.trim() === selectedStudent[Object.keys(selectedStudent)[0]]); + selectedRow.style.backgroundColor = 'lightblue'; + // 获取选中行的学号和姓名并显示在主页面 + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + var selectedStudentDiv = document.getElementById('selectedStudentInfo'); + selectedStudentDiv.innerHTML = `

学号:${studentId},姓名:${studentName}

`; + return; + } + + // 计算每个学生被选中的概率(总积分越高,被点到的概率越低) + let probabilities = eligibleStudents.map(student => { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + let studentPoints = pointsKey ? parseInt(student[pointsKey]) : 0; + return 1 / (studentPoints + 1) / totalPoints; + }); + + // 使用 Math.random 和 Date.now 作为种子 + let randomValue = Math.random() + Date.now() % 1000 / 1000; + let cumulativeProbability = 0; + let selectedStudent; + for (let i = 0; i < eligibleStudents.length; i++) { + cumulativeProbability += probabilities[i]; + if (randomValue < cumulativeProbability) { + selectedStudent = eligibleStudents[i]; + break; + } + } + + if (selectedStudent) { + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + var selectedRow = rows.find(row => row.querySelector('span').textContent.trim() === selectedStudent[Object.keys(selectedStudent)[0]]); + selectedRow.style.backgroundColor = 'lightblue'; + // 获取选中行的学号和姓名并显示在主页面 + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + var selectedStudentDiv = document.getElementById('selectedStudentInfo'); + selectedStudentDiv.innerHTML = `

学号:${studentId},姓名:${studentName}

`; + } +}); + +// 到达教室按钮 +document.getElementById('arriveClassroom').addEventListener('click', function () { + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + if (rows.length > 0) { + var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); + if (selectedRow) { + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + // 查找对应的学生并增加积分 + var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); + if (student) { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + if (pointsKey) { + student[pointsKey] = parseFloat(student[pointsKey]) + 1; + // 更新显示 + var rowSpans = selectedRow.querySelectorAll('span'); + rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; + } + } + } + } +}); + +// 准确重复所提问的问题 +document.getElementById('repeatedQuestionYes').addEventListener('click', function () { + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + if (rows.length > 0) { + var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); + if (selectedRow) { + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + // 查找对应的学生并增加积分 + var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); + if (student) { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + if (pointsKey) { + student[pointsKey] = parseFloat(student[pointsKey]) + 0.5; + // 更新显示 + var rowSpans = selectedRow.querySelectorAll('span'); + rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; + } + } + } + } +}); + +// 不能准确重复所提问的问题 +document.getElementById('repeatedQuestionNo').addEventListener('click', function () { + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + if (rows.length > 0) { + var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); + if (selectedRow) { + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + // 查找对应的学生并减少积分 + var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); + if (student) { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + if (pointsKey) { + student[pointsKey] = parseFloat(student[pointsKey]) - 1; + // 更新显示 + var rowSpans = selectedRow.querySelectorAll('span'); + rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; + } + } + } + } +}); + +// 增加自定义分数 +document.getElementById('addScoreButton').addEventListener('click', function () { + var inputScore = parseFloat(document.getElementById('addScoreInput').value); + if (inputScore >= 0.5 && inputScore <= 3 && !isNaN(inputScore)) { + var displayDiv = document.getElementById('excelDataDisplay'); + var rows = Array.from(displayDiv.children).slice(1); + if (rows.length > 0) { + var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); + if (selectedRow) { + var studentInfo = selectedRow.querySelectorAll('span'); + var studentId = studentInfo[0].textContent.trim(); + var studentName = studentInfo[1].textContent.trim(); + // 查找对应的学生并增加积分 + var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); + if (student) { + let pointsKey = Object.keys(student).find(key => key.includes('积分')); + if (pointsKey) { + student[pointsKey] = parseFloat(student[pointsKey]) + inputScore; + // 更新显示 + var rowSpans = selectedRow.querySelectorAll('span'); + rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; + + // 如果得分超过 2.5 分,将该学生加入跳过列表 + if (student[pointsKey] > 2.5) { + skippedStudents.push(student); + } + } + } + } + } + } else { + alert('输入的分数应在 0.5 到 3 之间且为有效数字。'); + } +}); \ No newline at end of file