This commit is contained in:
gogongxt committed 2026-11-02 16:01:16 +08:00
1 parent 2ce1c9056a
commit 9a726dbd9f
13 files changed
+8547 -5835

No files matched your search

+411 -180
View File
@@ -3,231 +3,462 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>程序员进制计算器</title>
<title>程序员计算器 · 功能工具箱</title>
<link rel="icon" href="assets/favicon.svg" type="image/svg+xml" />
<link rel="stylesheet" href="assets/styles.css" />
<script src="assets/common.js"></script>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.input-section {
margin-bottom: 20px;
.input-toolbar {
display: flex;
align-items: flex-end;
gap: 10px;
}
.input-toolbar .field {
flex: 1;
}
.input-toolbar input {
height: 44px;
font-family: var(--font-mono);
font-size: 1rem;
letter-spacing: 0.02em;
}
.input-toolbar .btn {
height: 44px;
padding-inline: 20px;
}
.base-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
margin-bottom: 16px;
}
input[type="text"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box; /* 确保 padding 不会增加元素宽度 */
}
.radio-group {
display: flex;
gap: 10px;
margin-top: 10px;
margin-bottom: 20px;
}
.radio-group label {
display: block;
margin-bottom: 5px;
}
button {
background-color: #4caf50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
#results {
margin-top: 20px;
border-top: 1px solid #ccc;
padding-top: 10px;
.base-row .field-label {
font-size: 0.8rem;
font-weight: 600;
color: var(--text-secondary);
}
.history-list {
list-style-type: none;
padding: 0;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.history-list li {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 5px;
.history-chip {
display: inline-flex;
align-items: center;
gap: 8px;
max-width: 100%;
padding: 6px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: var(--surface-2);
color: var(--text-secondary);
font-size: 0.76rem;
cursor: pointer;
font-size: 14px;
transition: all var(--rate) ease;
}
.history-section h2 {
font-size: 18px;
color: #333;
margin-bottom: 15px;
.history-chip:hover {
border-color: var(--accent);
color: var(--accent);
background: var(--accent-soft);
}
.history-list li:hover {
background-color: #f0f0f0;
.history-chip .chip-base {
font-size: 0.62rem;
font-weight: 700;
letter-spacing: 0.06em;
color: var(--text-muted);
}
.history-chip code {
font-family: var(--font-mono);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.result-row code {
font-size: 0.92rem;
letter-spacing: 0.02em;
}
.result-row .row-label {
width: 42px;
}
@media (max-width: 560px) {
.input-toolbar {
flex-direction: column;
align-items: stretch;
}
.input-toolbar .btn {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>程序员进制计算器</h1>
<label for="number-input">请输入数字:</label>
<input type="text" id="number-input" placeholder="例如: 100" required />
<button onclick="convertNumber()">转换</button>
<div class="radio-group">
<label
><input type="radio" name="base" value="10" checked /> 十进制</label
>
<label><input type="radio" name="base" value="2" /> 二进制</label>
<label><input type="radio" name="base" value="8" /> 八进制</label>
<label><input type="radio" name="base" value="16" /> 十六进制</label>
</div>
<div id="results">
<h2>转换结果</h2>
<div id="result-section">
<!-- 计算结果将显示在这里 -->
<div class="tool-page tool-page--narrow">
<header class="tool-header">
<div class="tool-icon">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M4 9h16M4 15h16M10 3 8 21M16 3l-2 18" />
</svg>
</div>
</div>
<div class="history-section">
<h2>历史记录</h2>
<ul class="history-list" id="history-list">
<!-- 历史记录将显示在这里 -->
</ul>
</div>
<div>
<h1>程序员计算器</h1>
<p>二进制、八进制、十进制、十六进制即时互转,支持任意精度整数。</p>
</div>
<div class="header-actions">
<button
class="btn btn--icon btn--ghost"
data-theme-toggle
title="切换主题"
aria-label="切换主题"
>
<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" />
</svg>
<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="4" />
<path
d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"
/>
</svg>
</button>
</div>
</header>
<script>
const historyList = [];
<section class="card">
<div class="base-row">
<span class="field-label">输入进制</span>
<div class="segmented" role="radiogroup" aria-label="输入进制">
<label class="seg">
<input type="radio" name="base" value="10" checked />
<span>十进制</span>
</label>
<label class="seg">
<input type="radio" name="base" value="2" />
<span>二进制</span>
</label>
<label class="seg">
<input type="radio" name="base" value="8" />
<span>八进制</span>
</label>
<label class="seg">
<input type="radio" name="base" value="16" />
<span>十六进制</span>
</label>
</div>
</div>
function convertNumber() {
const numberInput = document.getElementById("number-input").value;
const base = document.querySelector(
'input[name="base"]:checked',
).value;
<div class="input-toolbar">
<div class="field">
<label for="numberInput">输入数字</label>
<input
type="text"
id="numberInput"
value="100"
placeholder="例如:100,或 0x1f"
autocomplete="off"
spellcheck="false"
/>
</div>
<button class="btn btn--primary" id="convertBtn">转换</button>
</div>
let decimal;
if (base === "10") {
decimal = parseInt(numberInput, 10);
} else if (base === "2") {
decimal = parseInt(numberInput, 2);
} else if (base === "8") {
decimal = parseInt(numberInput, 8);
} else if (base === "16") {
decimal = parseInt(numberInput, 16);
<div class="alert alert--error hidden" id="errorBox" style="margin-top: 12px">
<svg viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="9" />
<path d="M12 8v5M12 16.5v.01" />
</svg>
<span id="errorText"></span>
</div>
</section>
<section class="card" aria-live="polite">
<div class="card-head">
<h2>转换结果</h2>
<span class="spacer"></span>
<span class="badge badge--accent">任意精度</span>
</div>
<div class="result-list">
<div class="result-row">
<span class="row-label">DEC</span>
<code id="resultDecimal">100</code>
<button
class="copy-btn"
data-copy-target="#resultDecimal"
data-copy-message="十进制已复制"
aria-label="复制十进制"
>
复制
</button>
</div>
<div class="result-row">
<span class="row-label">BIN</span>
<code id="resultBinary">0110 0100</code>
<button
class="copy-btn"
data-copy-target="#resultBinary"
data-copy-message="二进制已复制"
aria-label="复制二进制"
>
复制
</button>
</div>
<div class="result-row">
<span class="row-label">OCT</span>
<code id="resultOctal">144</code>
<button
class="copy-btn"
data-copy-target="#resultOctal"
data-copy-message="八进制已复制"
aria-label="复制八进制"
>
复制
</button>
</div>
<div class="result-row">
<span class="row-label">HEX</span>
<code id="resultHex">64</code>
<button
class="copy-btn"
data-copy-target="#resultHex"
data-copy-message="十六进制已复制"
aria-label="复制十六进制"
>
复制
</button>
</div>
</div>
<p class="hint" style="margin-top: 12px">
二进制结果每 4 位以空格分组,方便阅读;点击“复制”即可粘贴到代码中。
</p>
</section>
<section class="card">
<div class="card-head">
<h2>历史记录</h2>
<span class="spacer"></span>
<button class="btn btn--ghost btn--sm" id="clearHistoryBtn">清空</button>
</div>
<div class="history-list" id="historyList"></div>
<div class="empty-state" id="historyEmpty" style="padding: 18px 10px">
<span>按 Enter 或点击“转换”后会记录到这里</span>
</div>
</section>
</div>
<script>
(function () {
"use strict";
var BASE_NAMES = { 2: "BIN", 8: "OCT", 10: "DEC", 16: "HEX" };
var history = [];
var MAX_HISTORY = 20;
var input = document.getElementById("numberInput");
var errorBox = document.getElementById("errorBox");
var errorText = document.getElementById("errorText");
var historyList = document.getElementById("historyList");
var historyEmpty = document.getElementById("historyEmpty");
function currentBase() {
return parseInt(
document.querySelector('input[name="base"]:checked').value,
10,
);
}
function cleanInput(raw, base) {
var value = String(raw).trim().replace(/[\s_]/g, "");
if (base === 16) value = value.replace(/^0x/i, "");
if (base === 2) value = value.replace(/^0b/i, "");
if (base === 8) value = value.replace(/^0o/i, "");
return value;
}
function validPattern(base) {
return {
2: /^-?[01]+$/,
8: /^-?[0-7]+$/,
10: /^-?[0-9]+$/,
16: /^-?[0-9a-f]+$/i,
}[base];
}
function parseValue(raw, base) {
var text = cleanInput(raw, base);
if (!text || !validPattern(base).test(text)) {
return null;
}
if (isNaN(decimal)) {
alert("请输入有效的数字!");
return;
var negative = text.charAt(0) === "-";
var digits = negative ? text.slice(1) : text;
var bigBase = BigInt(base);
var value = 0n;
for (var i = 0; i < digits.length; i += 1) {
var digit = parseInt(digits.charAt(i), base);
if (isNaN(digit)) return null;
value = value * bigBase + BigInt(digit);
}
const binary = formatBinary(decimal.toString(2)); // 格式化二进制
const hexadecimal = decimal.toString(16).toUpperCase();
const octal = decimal.toString(8);
return negative ? -value : value;
}
const resultSection = document.getElementById("result-section");
function groupBinary(binary) {
var negative = binary.charAt(0) === "-";
var digits = negative ? binary.slice(1) : binary;
var grouped = digits.replace(/\B(?=(.{4})+$)/g, " ");
return (negative ? "-" : "") + grouped;
}
resultSection.innerHTML = `
<p>十进制: <span id="decimal"></span></p>
<p>二进制: <span id="binary"></span></p>
<p>十六进制: <span id="hexadecimal"></span></p>
<p>八进制: <span id="octal"></span></p>
`;
function setError(message) {
errorText.textContent = message;
errorBox.classList.remove("hidden");
}
document.getElementById("decimal").textContent = decimal;
document.getElementById("binary").textContent = binary;
document.getElementById("hexadecimal").textContent = hexadecimal;
document.getElementById("octal").textContent = octal;
function clearError() {
errorBox.classList.add("hidden");
}
const historyItem = {
input: numberInput,
function setResults(values) {
document.getElementById("resultDecimal").textContent =
values ? values.decimal : "—";
document.getElementById("resultBinary").textContent =
values ? values.binary : "—";
document.getElementById("resultOctal").textContent =
values ? values.octal : "—";
document.getElementById("resultHex").textContent =
values ? values.hex : "—";
}
// 返回解析结果,失败时返回 null
function convert() {
var base = currentBase();
var raw = input.value;
var value = parseValue(raw, base);
if (value === null) {
setError(
"请输入有效的" +
({ 2: "二进制", 8: "八进制", 10: "十进制", 16: "十六进制" }[
base
] || "") +
"数字。",
);
setResults(null);
return null;
}
clearError();
var result = {
input: cleanInput(raw, base),
base: base,
decimal: decimal,
binary: binary,
hexadecimal: hexadecimal,
octal: octal,
decimal: value.toString(10),
binary: groupBinary(value.toString(2)),
octal: value.toString(8),
hex: value.toString(16).toUpperCase(),
};
historyList.unshift(historyItem); // 将最新记录添加到最前面
updateHistoryList();
setResults(result);
return result;
}
function formatBinary(binary) {
// 从右往左每隔4位加一个空格
return binary
.split("") // 将字符串转为数组
.reverse() // 反转数组,从右往左处理
function renderHistory() {
historyList.innerHTML = "";
historyEmpty.style.display = history.length ? "none" : "block";
.join("") // 重新拼接为字符串
.replace(/(\d{4})/g, "\$1 ") // 每隔4位加一个空格
.split("") // 再次转为数组
.reverse() // 反转回原始顺序
history.forEach(function (item) {
var chip = document.createElement("button");
chip.className = "history-chip";
chip.type = "button";
chip.title = "点击重新转换";
chip.innerHTML =
'<span class="chip-base">' +
BASE_NAMES[item.base] +
"</span><code></code>";
.join("") // 重新拼接为字符串
.trim(); // 去除首尾多余的空格
}
chip.querySelector("code").textContent = item.input;
function updateHistoryList() {
const historyListElement = document.getElementById("history-list");
historyListElement.innerHTML = "";
chip.addEventListener("click", function () {
input.value = item.input;
document.querySelector(
'input[name="base"][value="' + item.base + '"]',
).checked = true;
convert();
input.focus();
});
historyList.forEach((item, index) => {
const listItem = document.createElement("li");
let displayText = "";
if (item.base === "10") {
displayText = `十进制: ${item.input}`;
} else if (item.base === "2") {
displayText = `二进制: ${item.input}`;
} else if (item.base === "8") {
displayText = `八进制: ${item.input}`;
} else if (item.base === "16") {
displayText = `十六进制: ${item.input}`;
}
listItem.textContent = displayText;
listItem.addEventListener("click", () => showHistoryResult(index));
historyListElement.appendChild(listItem);
historyList.appendChild(chip);
});
}
function showHistoryResult(index) {
const item = historyList[index];
const resultSection = document.getElementById("result-section");
resultSection.innerHTML = `
<p>十进制: <span id="decimal"></span></p>
<p>二进制: <span id="binary"></span></p>
<p>十六进制: <span id="hexadecimal"></span></p>
<p>八进制: <span id="octal"></span></p>
`;
function commit() {
var result = convert();
if (!result) return;
document.getElementById("decimal").textContent = item.decimal;
document.getElementById("binary").textContent = item.binary;
document.getElementById("hexadecimal").textContent = item.hexadecimal;
document.getElementById("octal").textContent = item.octal;
// 相同记录不重复
var duplicate = history.some(function (item) {
return (
item.input === result.input && item.base === result.base
);
});
if (!duplicate) {
history.unshift(result);
history = history.slice(0, MAX_HISTORY);
renderHistory();
}
}
</script>
</div>
var liveConvert = Toolbox.debounce(convert, 160);
input.addEventListener("input", function () {
liveConvert();
});
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
commit();
}
});
document
.querySelectorAll('input[name="base"]')
.forEach(function (radio) {
radio.addEventListener("change", convert);
});
document.getElementById("convertBtn").addEventListener("click", commit);
document
.getElementById("clearHistoryBtn")
.addEventListener("click", function () {
if (!history.length) return;
history = [];
renderHistory();
Toolbox.toast("历史记录已清空", "success");
});
// 初始化
convert();
renderHistory();
})();
</script>
</body>
</html>