|
|
@ -1,22 +1,24 @@
|
|
|
|
import java.util.Scanner;
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
|
|
|
|
public class uml10_10 {
|
|
|
|
public class uml10_10 {
|
|
|
|
|
|
|
|
|
|
|
|
// 将任意进制字符串转换为十进制整数
|
|
|
|
// 将任意进制字符串转换为十进制整数
|
|
|
|
public static int toDecimal(String number, int base) {
|
|
|
|
public static int toDecimal(String number, int base) {
|
|
|
|
int decimal = 0;
|
|
|
|
int decimal = 0;
|
|
|
|
int power = 0;
|
|
|
|
int power = 0;
|
|
|
|
|
|
|
|
// 从字符串的末尾开始处理
|
|
|
|
// 从字符串的末尾开始处理
|
|
|
|
|
|
|
|
for (int i = number.length() - 1; i >= 0; i--) {
|
|
|
|
for (int i = number.length() - 1; i >= 0; i--) {
|
|
|
|
char digit = number.charAt(i);
|
|
|
|
char digit = number.charAt(i);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是数字字符(0-9)
|
|
|
|
// 如果是数字字符(0-9)
|
|
|
|
if (Character.isDigit(digit)) {
|
|
|
|
if (Character.isDigit(digit)) {
|
|
|
|
int value = Character.getNumericValue(digit);
|
|
|
|
int value = Character.getNumericValue(digit);
|
|
|
|
decimal += value * Math.pow(base, power);
|
|
|
|
decimal += value * Math.pow(base, power);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 如果是字母字符(A-F 或 a-f),表示10-15
|
|
|
|
// 如果是字母字符(A-F 或 a-f),表示10-15
|
|
|
|
else if (Character.isLetter(digit)) {
|
|
|
|
else if (Character.isLetter(digit)) {
|
|
|
|
char upperDigit = Character.toUpperCase(digit);
|
|
|
|
char upperDigit = Character.toUpperCase(digit);
|
|
|
|
int value = upperDigit - 'A' + 10;
|
|
|
|
int value = upperDigit - 'A' + 10;
|
|
|
@ -31,14 +33,14 @@ public class uml10_10 {
|
|
|
|
return decimal;
|
|
|
|
return decimal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将十进制整数转换为任意进制字符串
|
|
|
|
// 将十进制整数转换为任意进制字符串
|
|
|
|
public static String fromDecimal(int number, int base) {
|
|
|
|
public static String fromDecimal(int number, int base) {
|
|
|
|
if (number == 0) {
|
|
|
|
if (number == 0) {
|
|
|
|
return "0";
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制
|
|
|
|
char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制
|
|
|
|
|
|
|
|
|
|
|
|
boolean isNegative = number < 0;
|
|
|
|
boolean isNegative = number < 0;
|
|
|
|
if (isNegative) {
|
|
|
|
if (isNegative) {
|
|
|
@ -59,34 +61,66 @@ public class uml10_10 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
// 创建窗口
|
|
|
|
|
|
|
|
JFrame frame = new JFrame("进制转换器");
|
|
|
|
System.out.print("请输入源进制(2-16):");
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
int sourceBase = scanner.nextInt();
|
|
|
|
frame.setSize(400, 300);
|
|
|
|
scanner.nextLine(); // 消耗掉换行符
|
|
|
|
frame.setLayout(new GridLayout(5, 2)); // 网格布局
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入目标进制(2-16):");
|
|
|
|
// 创建组件
|
|
|
|
int targetBase = scanner.nextInt();
|
|
|
|
JLabel sourceBaseLabel = new JLabel("源进制(2-16):");
|
|
|
|
scanner.nextLine(); // 消耗掉换行符
|
|
|
|
JTextField sourceBaseField = new JTextField();
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入要转换的" + sourceBase + "进制数字:");
|
|
|
|
JLabel targetBaseLabel = new JLabel("目标进制(2-16):");
|
|
|
|
String number = scanner.nextLine().trim(); // 读取实际的数字输入
|
|
|
|
JTextField targetBaseField = new JTextField();
|
|
|
|
|
|
|
|
|
|
|
|
// 验证源进制数字是否有效
|
|
|
|
JLabel numberLabel = new JLabel("要转换的数字:");
|
|
|
|
for (char c : number.toCharArray()) {
|
|
|
|
JTextField numberField = new JTextField();
|
|
|
|
if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid character in source base number: " + c);
|
|
|
|
JButton convertButton = new JButton("转换");
|
|
|
|
|
|
|
|
JLabel resultLabel = new JLabel("转换结果:");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加组件到窗口
|
|
|
|
|
|
|
|
frame.add(sourceBaseLabel);
|
|
|
|
|
|
|
|
frame.add(sourceBaseField);
|
|
|
|
|
|
|
|
frame.add(targetBaseLabel);
|
|
|
|
|
|
|
|
frame.add(targetBaseField);
|
|
|
|
|
|
|
|
frame.add(numberLabel);
|
|
|
|
|
|
|
|
frame.add(numberField);
|
|
|
|
|
|
|
|
frame.add(convertButton);
|
|
|
|
|
|
|
|
frame.add(resultLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加按钮事件处理
|
|
|
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText().trim());
|
|
|
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText().trim());
|
|
|
|
|
|
|
|
String number = numberField.getText().trim();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 验证源进制数字是否有效
|
|
|
|
|
|
|
|
for (char c : number.toCharArray()) {
|
|
|
|
|
|
|
|
if (!Character.isDigit(c) &&
|
|
|
|
|
|
|
|
!(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid character in source base number: " + c);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将源进制数字转换为十进制
|
|
|
|
|
|
|
|
int decimalNumber = toDecimal(number, sourceBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将十进制数字转换为目标进制
|
|
|
|
|
|
|
|
String convertedNumber = fromDecimal(decimalNumber, targetBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 显示结果
|
|
|
|
|
|
|
|
resultLabel.setText("转换后的" + targetBase + "进制数字是:" + convertedNumber);
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误: " + ex.getMessage(), "输入错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 显示窗口
|
|
|
|
// 将源进制数字转换为十进制
|
|
|
|
frame.setVisible(true);
|
|
|
|
int decimalNumber = toDecimal(number, sourceBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将十进制数字转换为目标进制
|
|
|
|
|
|
|
|
String convertedNumber = fromDecimal(decimalNumber, targetBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scanner.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|