-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPACAL.html
122 lines (100 loc) · 4.03 KB
/
GPACAL.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html>
<head>
<title>Data Parsing Example</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
}
</style>
<script>
function parseData() {
var rawData = document.getElementById("dataInput").value;
// 解析数据并提取Credit和Mark
var dataRows = rawData.split("\n"); // 假设每行数据以换行符分隔
var credits = [];
var marks = [];
var results = []; // 添加结果数组
for (var i = 0; i < dataRows.length; i++) {
var rowData = dataRows[i].split("\t"); // 假设数据列以制表符分隔
var credit = parseFloat(rowData[4]); // Credit列索引为4
var mark = parseFloat(rowData[5]); // Mark列索引为5
// 检查是否为有效数字,如果不是则将其设置为0
if (isNaN(credit)) {
credit = 0;
}
if (isNaN(mark)) {
mark = 0;
}
credits.push(credit);
marks.push(mark);
results.push((credit * mark).toFixed(2)); // 计算并存储result的值
}
// 创建列表格
var table = document.createElement("table");
var tableHead = document.createElement("thead");
var tableHeadRow = document.createElement("tr");
var tableBody = document.createElement("tbody");
var creditHeadCell = document.createElement("th");
creditHeadCell.textContent = "Credit";
tableHeadRow.appendChild(creditHeadCell);
var markHeadCell = document.createElement("th");
markHeadCell.textContent = "Mark";
tableHeadRow.appendChild(markHeadCell);
var resultHeadCell = document.createElement("th");
resultHeadCell.textContent = "Result";
tableHeadRow.appendChild(resultHeadCell);
for (var i = 0; i < credits.length; i++) {
var creditCell = document.createElement("td");
creditCell.textContent = credits[i];
var markCell = document.createElement("td");
markCell.textContent = marks[i];
var resultCell = document.createElement("td");
resultCell.textContent = results[i]; // 使用结果数组中的值
var row = document.createElement("tr");
row.appendChild(creditCell);
row.appendChild(markCell);
row.appendChild(resultCell);
tableBody.appendChild(row);
}
tableHead.appendChild(tableHeadRow);
table.appendChild(tableHead);
table.appendChild(tableBody);
// 计算结果
var resultSum = results.reduce(function (acc, curr) { // 使用results数组计算结果和
return isNaN(curr) ? acc : acc + parseFloat(curr);
}, 0);
var creditSum = credits.reduce(function (acc, curr) {
return isNaN(curr) ? acc : acc + curr;
}, 0);
var resultAvg = creditSum !== 0 ? (resultSum / creditSum).toFixed(2) : 0;
var resultSumContainer = document.createElement("div");
resultSumContainer.textContent = "Sum of Results: " + resultSum.toFixed(2);
var creditSumContainer = document.createElement("div");
creditSumContainer.textContent = "Sum of Credits: " + creditSum.toFixed(2);
// 添加结果显示
var resultContainer = document.getElementById("resultContainer");
resultContainer.innerHTML = ""; // 清空容器
resultContainer.appendChild(table);
var resultText = document.createElement("p");
resultText.textContent = "Average Result: " + resultAvg;
resultContainer.appendChild(resultText);
resultContainer.appendChild(resultSumContainer);
resultContainer.appendChild(creditSumContainer);
}
</script>
</head>
<body>
<img src="example.png" alt="Example Image" style="display: block; margin: 0 auto;">
<textarea id="dataInput" rows="10" cols="50"></textarea>
<br>
<button onclick="parseData()">计算加权均分</button>
<br>
<p>请按照如图所示完整复制分数放入框框里用于计算。遇问题交流微信chengzixuan1029。制作用时20分钟,凑合用。</p>
<div id="resultContainer"></div>
</body>
</html>