From bf70d0f03202de1f48d5c5da9965f9beadac1263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E8=92=B8?= <3505869163@qq.com> Date: Thu, 10 Oct 2024 15:14:31 +0800 Subject: [PATCH] V1 --- .idea/.gitignore | 3 + .idea/computer1.iml | 11 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ computer.java | 87 +++++++++++++++++++ out/production/computer1/.idea/.gitignore | 3 + out/production/computer1/.idea/computer1.iml | 11 +++ out/production/computer1/.idea/misc.xml | 6 ++ out/production/computer1/.idea/modules.xml | 8 ++ out/production/computer1/.idea/vcs.xml | 6 ++ out/production/computer1/HHJ.class | Bin 0 -> 234 bytes out/production/computer1/README.md | 2 + out/production/computer1/computer.class | Bin 0 -> 2533 bytes 14 files changed, 157 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/computer1.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 computer.java create mode 100644 out/production/computer1/.idea/.gitignore create mode 100644 out/production/computer1/.idea/computer1.iml create mode 100644 out/production/computer1/.idea/misc.xml create mode 100644 out/production/computer1/.idea/modules.xml create mode 100644 out/production/computer1/.idea/vcs.xml create mode 100644 out/production/computer1/HHJ.class create mode 100644 out/production/computer1/README.md create mode 100644 out/production/computer1/computer.class diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/computer1.iml b/.idea/computer1.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/computer1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e1f830b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b4476f8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/computer.java b/computer.java new file mode 100644 index 0000000..c6dfb8f --- /dev/null +++ b/computer.java @@ -0,0 +1,87 @@ +import java.util.Scanner; + +public class computer { + + // 将任意进制数转换为十进制数 + public static int toDecimal(String number, int base) { + int decimal = 0; + int power = 0; + + // 从字符串的末尾开始遍历 + for (int i = number.length() - 1; i >= 0; i--) { + char digit = number.charAt(i); + int value; + + // 处理0-9的数字字符 + if (digit >= '0' && digit <= '9') { + 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 { + throw new IllegalArgumentException("Invalid character in number: " + digit); + } + + // 根据当前字符的权重(即它在数中的位置)累加其值 + decimal += value * Math.pow(base, power); + power++; + } + + return decimal; + } + + // 将十进制数转换为任意进制数 + public static String fromDecimal(int decimal, int base) { + if (base < 2 || base > 16) { + throw new IllegalArgumentException("Base must be between 2 and 16"); + } + + StringBuilder number = new StringBuilder(); + + // 当十进制数大于0时,持续转换 + while (decimal > 0) { + int remainder = decimal % base; + char digit; + + // 处理0-9 + if (remainder < 10) { + digit = (char) ('0' + remainder); + } + // 处理10-15(A-F) + else { + digit = (char) ('A' + (remainder - 10)); + } + + number.insert(0, digit); // 在字符串的开头插入新的字符 + decimal /= base; // 更新十进制数,去掉已转换的部分 + } + + // 如果转换后的字符串为空(即输入的十进制数为0),则返回"0" + return number.length() == 0 ? "0" : number.toString(); + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // 输入原始进制数和目标进制 + System.out.print("输入原始数字的基数(2-16): "); + int originalBase = scanner.nextInt(); + + System.out.print("输入原始数字输入要转换的基数(2-16): "); + String originalNumber = scanner.next(); + + System.out.print("输入要转换的基数(2-16): "); + int targetBase = scanner.nextInt(); + + // 转换并输出结果 + int decimal = toDecimal(originalNumber, originalBase); + String convertedNumber = fromDecimal(decimal, targetBase); + + System.out.println("转换后的号码是: " + convertedNumber); + + scanner.close(); + } +} \ No newline at end of file diff --git a/out/production/computer1/.idea/.gitignore b/out/production/computer1/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/out/production/computer1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/computer1/.idea/computer1.iml b/out/production/computer1/.idea/computer1.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/computer1/.idea/computer1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/computer1/.idea/misc.xml b/out/production/computer1/.idea/misc.xml new file mode 100644 index 0000000..e1f830b --- /dev/null +++ b/out/production/computer1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/computer1/.idea/modules.xml b/out/production/computer1/.idea/modules.xml new file mode 100644 index 0000000..b4476f8 --- /dev/null +++ b/out/production/computer1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/computer1/.idea/vcs.xml b/out/production/computer1/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/computer1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/computer1/HHJ.class b/out/production/computer1/HHJ.class new file mode 100644 index 0000000000000000000000000000000000000000..f229e7f7835ffc1f4bda5ea374635b6a6bc5628c GIT binary patch literal 234 zcmXYr%?`m(5QWcFf2dON26kBJ#*WxXM5JLs>~HIa+f%FWb79PMuiMfrLLFI+#!yg;@IVdjMuyvz-~A82+5#0wz{!b!RsCj zVV@VJ*e|Fz+&JLHn>Z*WhuqM_bJ&fD7Xd`Yp}TQdSj2?phyp{wQ3?-t?TaK6sjQ`^TP1j` zWRD&(5?WlsqF~=qZA5E~Yi6u*z)BlttTkL0mf#%G;#qo?1RXVEhDExYUxOK$`M4tF zoXbpbSj+ID5}l6WOeK%&X$eIGmKHhIucfAPD?FS|CT3+44%f}+&y&^@nqfwTi^@3$ zU0EYeRV!Ph;25I|TT$2kIi{_7__TD4fGn6t&73n4%``=z1@`T%WyBi?Y$1_TlW8Jh zYVodoiN({b-3P*K(YtQh+LB1p9MRL39(|@BNM_R!z1I-!Dw^)dT7jX8rC7!SRK$@` zVInDE=?u8xcwCQZ@y>KCo6t>b(}{?lvWz4PEuS&6+cyP>gxavl9vaa=r^KhTege|3Bnq&CrzF zEZk5iyX0tdwAbfQAV)*8U;Y;7>iu#Kj*!#u^fg_aeZSrd5mYKpwgS=g%@MMz!=eD%b*U@>B~HnmTEEu-bVA zo@$@(2NVgI%8CRiQ(*O1F=kbDs=e0dy^11VQ|+NILh?ANPD-MJX>f>+74NO8j<=3K z{j(v5l8bPc`NpA;_TFF!8^dN|1|m^N)cl-;TGqOR)vjPIt69Z*rq+QLY(^`#VI#Ja z6S%b#t!DZV$jG!_iC6M3d6a1ySiG^kU92Tw4p}b8n6*g13T`*NsnvlGv z<`$~O;QHlTsA+LgJ3Vr>9Jxvt4+4_I3-q*jW@Btzx`|6KyS|xi3UlSQveG`**U!p! z5|v%(=D$$~um>6j^SErWGbKJ9%(xJH@hUo5M+NV8+0sjIOwAhJ(S7x6wG7g Rq-HTTQ7bmKmmUhR`CoCjU-|$5 literal 0 HcmV?d00001