lzy1227 1 month ago
parent 810140f177
commit 86b0dfdc58

@ -2,83 +2,68 @@ import java.util.Scanner;
public class computer { public class computer {
// 将任意进制数转换为十进制数 // 将任意R进制数X转换为十进制数
public static int toDecimal(String number, int base) { public static int baseToDecimal(String x, int r) {
int decimal = 0; int decimalValue = 0;
int power = 0; int length = x.length();
// 从字符串的末尾开始遍历 for (int i = 0; i < length; i++) {
for (int i = number.length() - 1; i >= 0; i--) { char digit = x.charAt(length - 1 - i); // 从右往左处理
char digit = number.charAt(i);
int value; int value;
// 处理0-9的数字字符 if (Character.isDigit(digit)) {
if (digit >= '0' && digit <= '9') { value = digit - '0'; // 数字字符
value = digit - '0';
}
// 处理A-F或a-f的十六进制字符
else if (digit >= 'A' && digit <= 'F') {
value = digit - 'A' + 10;
} else if (digit >= 'a' && digit <= 'f') {
value = digit - 'a' + 10;
} else { } else {
throw new IllegalArgumentException("Invalid character in number: " + digit); value = Character.toUpperCase(digit) - 'A' + 10; // 字母字符
} }
decimalValue += value * Math.pow(r, i);
// 根据当前字符的权重(即它在数中的位置)累加其值
decimal += value * Math.pow(base, power);
power++;
} }
return decimalValue;
return decimal;
} }
public static String fromDecimal(int decimal, int base) { // 将十进制数转换为R进制数
if (base < 2 || base > 16) { public static String decimalToBase(int num, int r) {
throw new IllegalArgumentException("Base must be between 2 and 16"); if (num == 0) {
return "0";
} }
StringBuilder number = new StringBuilder(); StringBuilder result = new StringBuilder();
// 当十进制数大于0时持续转换 while (num > 0) {
while (decimal > 0) { int remainder = num % r;
int remainder = decimal % base;
char digit;
// 处理0-9
if (remainder < 10) { if (remainder < 10) {
digit = (char) ('0' + remainder); result.append(remainder); // 数字字符
} else {
result.append((char) ('A' + (remainder - 10))); // 字母字符
} }
// 处理10-15A-F num /= r;
else {
digit = (char) ('A' + (remainder - 10));
} }
number.insert(0, digit); // 在字符串的开头插入新的字符 return result.reverse().toString(); // 反转结果
decimal /= base; // 更新十进制数,去掉已转换的部分
} }
// 如果转换后的字符串为空即输入的十进制数为0则返回"0" // 将任意R进制数X从进制R_from转换到进制R_to
return number.length() == 0 ? "0" : number.toString(); public static String convertBase(String x, int rFrom, int rTo) {
int decimalValue = baseToDecimal(x, rFrom); // 转换为十进制
return decimalToBase(decimalValue, rTo); // 转换为目标进制
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
// 输入原始进制数和目标进制 System.out.print("请输入要转换的进制数例如1A");
System.out.print("输入原始数字的基数2-16: "); String x = scanner.nextLine();
int originalBase = scanner.nextInt();
System.out.print("输入原始数字输入要转换的基数2-16: "); System.out.print("请输入原始进制2-16");
String originalNumber = scanner.next(); int rFrom = scanner.nextInt();
System.out.print("输入要转换的基数2-16: "); System.out.print("请输入目标进制2-16");
int targetBase = scanner.nextInt(); int rTo = scanner.nextInt();
// 转换并输出结果 // 调用转换函数
int decimal = toDecimal(originalNumber, originalBase); String result = convertBase(x, rFrom, rTo);
String convertedNumber = fromDecimal(decimal, targetBase);
System.out.println("转换后的号码是: " + convertedNumber); System.out.printf("%s 从 %d 进制转换到 %d 进制的结果是: %s\n", x, rFrom, rTo, result);
scanner.close(); scanner.close();
} }

Loading…
Cancel
Save