const display = document.getElementById(
'display'
);
const equation = document.getElementById(
'equation'
);
const buttons = document.querySelectorAll(
'.btn'
);
const historyTable = document.getElementById(
'historyTable'
).querySelector(
'tbody'
);
const clearBtn = document.getElementById(
'clearBtn'
);
const downloadBtn = document.getElementById(
'downloadBtn'
);
const calculationType = document.getElementById(
'calculationType'
);
const dimensionInputs = document.querySelectorAll(
'.dimension-input'
);
const keyMap = {
'0'
:
'0'
,
'1'
:
'1'
,
'2'
:
'2'
,
'3'
:
'3'
,
'4'
:
'4'
,
'5'
:
'5'
,
'6'
:
'6'
,
'7'
:
'7'
,
'8'
:
'8'
,
'9'
:
'9'
,
'.'
:
'.'
,
'+'
:
'+'
,
'-'
:
'-'
,
'*'
:
'*'
,
'/'
:
'/'
,
'Enter'
:
'='
,
'='
:
'='
,
'Backspace'
:
'⌫'
,
'Escape'
:
'C'
,
'%'
:
'%'
};
document.addEventListener(
'keydown'
, (e) => {
if
(e.target.tagName ===
'INPUT'
&& e.target.type ===
'text'
) {
return
;
}
const key = keyMap[e.key];
if
(key) {
const button = document.querySelector(`.btn[value=
"${key}"
]`);
if
(button) {
button.click();
}
}
});
let
currentInput =
'0'
;
let
equationStr =
''
;
let
result =
null
;
function
updateDisplay() {
display.textContent = currentInput;
equation.textContent = equationStr;
}
function
handleNumber(number) {
if
(currentInput ===
'0'
&& number !==
'.'
) {
currentInput = number;
}
else
{
currentInput += number;
}
updateDisplay();
}
function
handleOperator(operator) {
if
(operator ===
'C'
) {
currentInput =
'0'
;
equationStr =
''
;
result =
null
;
}
else
if
(operator ===
'⌫'
) {
currentInput = currentInput.slice(0, -1) ||
'0'
;
}
else
if
(operator ===
'+/-'
) {
currentInput = (parseFloat(currentInput) * -1).toString();
}
else
if
(operator ===
'%'
) {
currentInput = (parseFloat(currentInput) / 100).toString();
}
else
{
equationStr += currentInput + operator;
currentInput =
'0'
;
}
updateDisplay();
}
function
calculate() {
try
{
equationStr += currentInput;
result = eval(equationStr);
currentInput = result.toString();
history.push({
equation: equationStr,
result: result
});
updateHistoryTable();
equationStr =
''
;
updateDisplay();
}
catch
(e) {
currentInput =
'Error'
;
updateDisplay();
}
}
let
history = JSON.parse(localStorage.getItem(
'calcHistory'
) ||
'[]'
);
updateHistoryTable();
function
updateHistoryTable() {
localStorage.setItem(
'calcHistory'
, JSON.stringify(history));
historyTable.innerHTML =
''
;
history.forEach((item, index) => {
const row = document.createElement(
'tr'
);
row.innerHTML = `
<td>${index + 1}</td>
<td>${item.equation}</td>
<td>${item.result}</td>
`;
historyTable.appendChild(row);
});
}
function
clearHistory() {
history = [];
localStorage.removeItem(
'calcHistory'
);
updateHistoryTable();
}
function
downloadHistory() {
const savedHistory = JSON.parse(localStorage.getItem(
'calcHistory'
) ||
'[]'
);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(savedHistory.map((item, index) => ({
'序号'
: index + 1,
'计算式'
: item.equation,
'结果'
: item.result
})));
XLSX.utils.book_append_sheet(wb, ws,
'计算历史'
);
XLSX.writeFile(wb,
'计算历史.xlsx'
);
}
buttons.forEach(button => {
button.addEventListener(
'click'
, () => {
const value = button.value;
if
(button.classList.contains(
'number'
)) {
handleNumber(value);
}
else
if
(button.classList.contains(
'operator'
)) {
if
(value ===
'='
) {
calculate();
}
else
{
handleOperator(value);
}
}
});
});
clearBtn.addEventListener(
'click'
, clearHistory);
downloadBtn.addEventListener(
'click'
, downloadHistory);
updateDisplay();
calculationType.addEventListener(
'change'
,
function
() {
const inputGroups = document.querySelectorAll(
'.input-group'
);
for
(
let
i = 1; i < inputGroups.length; i++) {
inputGroups[i].style.display =
'none'
;
}
switch
(
this
.value) {
case
'area'
:
document.getElementById(
'lengthGroup'
).style.display =
'flex'
;
document.getElementById(
'widthGroup'
).style.display =
'flex'
;
break
;
case
'cylinder_area'
:
document.getElementById(
'radiusGroup'
).style.display =
'flex'
;
document.getElementById(
'heightGroup'
).style.display =
'flex'
;
break
;
case
'cone_area'
:
document.getElementById(
'radiusGroup'
).style.display =
'flex'
;
document.getElementById(
'generatrixGroup'
).style.display =
'flex'
;
break
;
case
'trapezoid_area'
:
document.getElementById(
'upperBaseGroup'
).style.display =
'flex'
;
document.getElementById(
'lowerBaseGroup'
).style.display =
'flex'
;
document.getElementById(
'heightGroup'
).style.display =
'flex'
;
break
;
case
'rectangle_perimeter'
:
document.getElementById(
'lengthGroup'
).style.display =
'flex'
;
document.getElementById(
'widthGroup'
).style.display =
'flex'
;
break
;
case
'circle_perimeter'
:
document.getElementById(
'radiusGroup'
).style.display =
'flex'
;
break
;
case
'cube_volume'
:
document.getElementById(
'lengthGroup'
).style.display =
'flex'
;
document.getElementById(
'widthGroup'
).style.display =
'flex'
;
document.getElementById(
'heightGroup'
).style.display =
'flex'
;
break
;
case
'cylinder_volume'
:
document.getElementById(
'radiusGroup'
).style.display =
'flex'
;
document.getElementById(
'heightGroup'
).style.display =
'flex'
;
break
;
case
'triangle_area'
:
document.getElementById(
'baseGroup'
).style.display =
'flex'
;
document.getElementById(
'height1Group'
).style.display =
'flex'
;
break
;
}
});
const inputGroups = document.querySelectorAll(
'.input-group'
);
for
(
let
i = 1; i < inputGroups.length; i++) {
inputGroups[i].style.display =
'none'
;
}