Compare commits
No commits in common. 'main' and 'aca88be3e668ef27922b159ec1deae02cbfe7926' have entirely different histories.
main
...
aca88be3e6
@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>进制转换器</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>进制转换器</h1>
|
||||
<label for="inputNumber">输入数字:</label>
|
||||
<input type="text" id="inputNumber" required>
|
||||
|
||||
<label for="fromBase">初始进制 (2-16):</label>
|
||||
<input type="number" id="fromBase" min="2" max="16" required>
|
||||
|
||||
<label for="toBase">目标进制 (2-16):</label>
|
||||
<input type="number" id="toBase" min="2" max="16" required>
|
||||
|
||||
<button onclick="convertBase()">转换</button>
|
||||
<p id="result"></p >
|
||||
</div>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,4 @@
|
||||
212203252
|
||||
222222222
|
||||
dh333333333
|
||||
dh444444444
|
@ -0,0 +1,28 @@
|
||||
function convertBase() {
|
||||
const inputNumber = document.getElementById('inputNumber').value.trim();
|
||||
const fromBase = parseInt(document.getElementById('fromBase').value);
|
||||
const toBase = parseInt(document.getElementById('toBase').value);
|
||||
|
||||
// 验证输入数字是否符合fromBase的规则
|
||||
if (!isValidNumber(inputNumber, fromBase)) {
|
||||
alert('输入的数字无效!请检查其进制格式。');
|
||||
return;
|
||||
}
|
||||
|
||||
// 将输入的数字从fromBase转换为十进制
|
||||
const decimalValue = parseInt(inputNumber, fromBase);
|
||||
|
||||
// 将十进制数转换为toBase
|
||||
const result = decimalValue.toString(toBase).toUpperCase();
|
||||
document.getElementById('result').textContent = `转换结果: ${result}`;
|
||||
}
|
||||
|
||||
function isValidNumber(num, base) {
|
||||
const validChars = '0123456789ABCDEF'.slice(0, base);
|
||||
for (let char of num.toUpperCase()) {
|
||||
if (!validChars.includes(char)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
Loading…
Reference in new issue