Compare commits

...

No commits in common. 'nnd' and 'master' have entirely different histories.
nnd ... master

82
1.c

@ -1,82 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
// 函数:将单个字符转换为对应的数值
int charToVal(char c) {
if (c >= '0' && c <= '9') return c - '0';
else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
else return -1; // 非法字符
}
// 函数:将数值转换为对应的字符
char valToChar(int val) {
if (val >= 0 && val <= 9) return '0' + val;
else return 'A' + val - 10;
}
// 函数将字符串形式的数从fromBase进制转换为十进制
int convertToDecimal(char *num, int fromBase) {
int len = strlen(num);
int power = 1; // 表示当前位的权重
int numDecimal = 0; // 存放结果的十进制数
for (int i = len - 1; i >= 0; i--) {
int val = charToVal(num[i]);
if (val < 0 || val >= fromBase) {
printf("输入错误: 你所输入的数 %c 不是 %d进制\n", num[i], fromBase);
return -1; // 如果有非法字符,则返回 -1
}
numDecimal += val * power;
power *= fromBase; // 计算权重
}
return numDecimal;
}
// 函数将十进制数转换为toBase进制的字符串形式
void convertFromDecimal(int numDecimal, int toBase, char *result) {
int index = 0; // 指示result数组中的当前位置
if (numDecimal == 0) {
result[index++] = '0'; // 特例处理零
}
while (numDecimal > 0) {
int remainder = numDecimal % toBase;
result[index++] = valToChar(remainder);
numDecimal /= toBase;
}
result[index] = '\0'; // 添加字符串结束符
// 反转字符串
for (int i = 0; i < index / 2; i++) {
char temp = result[i];
result[i] = result[index - i - 1];
result[index - i - 1] = temp;
}
}
int main() {
char num[20];
int fromBase, toBase;
char result[20];
printf("请输入数字: ");
scanf("%s", num);
printf("请输入当前数字的进制(2 to 16): ");
scanf("%d", &fromBase);
printf("请输入您预期得到数字的进制(2 to 16): ");
scanf("%d", &toBase);
// 转换为十进制
int numDecimal = convertToDecimal(num, fromBase);
if (numDecimal < 0) {
return 1; // 如果转换出错则直接返回
}
// 转换为目标进制
convertFromDecimal(numDecimal, toBase, result);
printf("转化后的数字: %s\n", result);
return 0;
}

BIN
1.exe

Binary file not shown.

@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="zh">
<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;
margin: 20px;
padding: 20px;
background-color: #f4f4f4;
border-radius: 8px;
}
input, select {
margin-bottom: 10px;
padding: 8px;
width: 200px;
}
button {
padding: 10px;
}
.result {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>进制转换工具</h1>
<div>
<label for="inputNumber">输入数字 (X):</label>
<input type="text" id="inputNumber" placeholder="例如1A3" />
<label for="fromBase">选择原进制:</label>
<select id="fromBase">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select>
<label for="toBase">选择目标进制:</label>
<select id="toBase">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select>
<button onclick="convert()">转换</button>
</div>
<div class="result" id="result"></div>
<script>
function convert() {
const inputNumber = document.getElementById("inputNumber").value;
const fromBase = parseInt(document.getElementById("fromBase").value);
const toBase = parseInt(document.getElementById("toBase").value);
try {
// 将输入的原进制数转换为10进制
const decimalValue = parseInt(inputNumber, fromBase);
// 将10进制数转换为目标进制
const convertedValue = decimalValue.toString(toBase).toUpperCase();
// 显示结果
document.getElementById("result").innerText = `转换结果 (Y): ${convertedValue}`;
} catch (error) {
document.getElementById("result").innerText = "转换失败,请确保输入正确。";
}
}
</script>
</body>
</html>

@ -0,0 +1,19 @@
<script>
function convert() {
const inputNumber = document.getElementById("inputNumber").value;
const fromBase = parseInt(document.getElementById("fromBase").value);
const toBase = parseInt(document.getElementById("toBase").value);
try {
// 将输入的原进制数转换为10进制
const decimalValue = parseInt(inputNumber, fromBase);
// 将10进制数转换为目标进制
const convertedValue = decimalValue.toString(toBase).toUpperCase();
// 显示结果
document.getElementById("result").innerText = `转换结果 (Y): ${convertedValue}`;
} catch (error) {
document.getElementById("result").innerText = "转换失败,请确保输入正确。";
}
}
</script>
Loading…
Cancel
Save