first commit
This commit is contained in:
237
number.html
Normal file
237
number.html
Normal file
@@ -0,0 +1,237 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>程序员进制计算器</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f4f4f4;
|
||||
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.input-section {
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
input[type="text"] {
|
||||
padding: 8px;
|
||||
font-size: 16px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.radio-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-top: 10px;
|
||||
margin-bottom: 20px; /* 增加间距 */
|
||||
}
|
||||
|
||||
.radio-group label {
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
font-size: 16px;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
.result-section {
|
||||
margin-bottom: 20px;
|
||||
background-color: white;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
width: 300px;
|
||||
text-align: left; /* 左对齐 */
|
||||
}
|
||||
|
||||
.result-section p {
|
||||
margin: 10px 0;
|
||||
font-size: 18px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.history-section {
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
.history-section h2 {
|
||||
font-size: 18px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.history-list {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.history-list li {
|
||||
padding: 10px;
|
||||
|
||||
background-color: white;
|
||||
|
||||
border: 1px solid #ccc;
|
||||
|
||||
border-radius: 4px;
|
||||
margin-bottom: 5px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.history-list li:hover {
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>程序员进制计算器</h1>
|
||||
<div class="input-section">
|
||||
<input type="text" id="number-input" placeholder="请输入数字" />
|
||||
<button onclick="convertNumber()">转换</button>
|
||||
</div>
|
||||
<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 class="result-section" id="result-section">
|
||||
<!-- 计算结果将显示在这里 -->
|
||||
</div>
|
||||
<div class="history-section">
|
||||
<h2>历史记录</h2>
|
||||
<ul class="history-list" id="history-list">
|
||||
<!-- 历史记录将显示在这里 -->
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const historyList = [];
|
||||
|
||||
function convertNumber() {
|
||||
const numberInput = document.getElementById("number-input").value;
|
||||
const base = document.querySelector('input[name="base"]:checked').value;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (isNaN(decimal)) {
|
||||
alert("请输入有效的数字!");
|
||||
return;
|
||||
}
|
||||
|
||||
const binary = formatBinary(decimal.toString(2)); // 格式化二进制
|
||||
const hexadecimal = decimal.toString(16).toUpperCase();
|
||||
const octal = decimal.toString(8);
|
||||
|
||||
const resultSection = document.getElementById("result-section");
|
||||
|
||||
resultSection.innerHTML = `
|
||||
<p>十进制: <strong>${decimal}</strong></p>
|
||||
<p>二进制: <strong>${binary}</strong></p>
|
||||
<p>十六进制: <strong>${hexadecimal}</strong></p>
|
||||
<p>八进制: <strong>${octal}</strong></p>
|
||||
`;
|
||||
|
||||
const historyItem = {
|
||||
input: numberInput,
|
||||
base: base,
|
||||
decimal: decimal,
|
||||
binary: binary,
|
||||
hexadecimal: hexadecimal,
|
||||
octal: octal,
|
||||
};
|
||||
|
||||
historyList.unshift(historyItem); // 将最新记录添加到最前面
|
||||
updateHistoryList();
|
||||
}
|
||||
|
||||
function formatBinary(binary) {
|
||||
// 从右往左每隔4位加一个空格
|
||||
return binary
|
||||
.split("") // 将字符串转为数组
|
||||
.reverse() // 反转数组,从右往左处理
|
||||
|
||||
.join("") // 重新拼接为字符串
|
||||
.replace(/(\d{4})/g, "\$1 ") // 每隔4位加一个空格
|
||||
.split("") // 再次转为数组
|
||||
.reverse() // 反转回原始顺序
|
||||
|
||||
.join("") // 重新拼接为字符串
|
||||
.trim(); // 去除首尾多余的空格
|
||||
}
|
||||
|
||||
function updateHistoryList() {
|
||||
const historyListElement = document.getElementById("history-list");
|
||||
historyListElement.innerHTML = "";
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
function showHistoryResult(index) {
|
||||
const item = historyList[index];
|
||||
const resultSection = document.getElementById("result-section");
|
||||
resultSection.innerHTML = `
|
||||
<p>十进制: <strong>${item.decimal}</strong></p>
|
||||
<p>二进制: <strong>${item.binary}</strong></p>
|
||||
|
||||
<p>十六进制: <strong>${item.hexadecimal}</strong></p>
|
||||
|
||||
<p>八进制: <strong>${item.octal}</strong></p>
|
||||
`;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user