|  |  |  | @ -1,87 +0,0 @@ | 
			
		
	
		
			
				
					|  |  |  |  | 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(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } |