You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
public class BaseConverter {
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* 将给定进制的字符串转换为十进制整数
*
* @param str 给定进制的字符串表示
* @param base 给定进制的基数
* @return 十进制整数
*/
public static int toDecimal(String str, int base) {
int result = 0;
int power = 0;
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
int digit = Character.isDigit(c) ? c - '0' : c - 'A' + 10;
result += digit * Math.pow(base, power++);
}
return result;
}
/**
* 将十进制整数转换为目标进制的字符串
*
* @param decimal 十进制整数
* @param targetBase 目标进制的基数
* @return 目标进制的字符串表示
*/
public static String fromDecimal(int decimal, int targetBase) {
if (decimal == 0) {
return "0";
}
StringBuilder result = new StringBuilder();
while (decimal > 0) {
result.insert(0, DIGITS[decimal % targetBase]);
decimal /= targetBase;
}
return result.toString();
}
/**
* 将任意进制的数转换为另一个任意进制的数
*
* @param str 给定进制的字符串表示
* @param sourceBase 给定进制的基数
* @param targetBase 目标进制的基数
* @return 目标进制的字符串表示
*/
public static String convertBase(String str, int sourceBase, int targetBase) {
int decimalValue = toDecimal(str, sourceBase);
return fromDecimal(decimalValue, targetBase);
}
/**
* 主方法,用于测试
*/
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new BaseConverterGUI();
});
}
// GUI 类
public static class BaseConverterGUI extends JFrame {
private JTextField inputField;
private JComboBox<Integer> sourceBaseBox;
private JComboBox<Integer> targetBaseBox;
private JButton convertButton;
private JLabel resultLabel;
public BaseConverterGUI() {
setTitle("进制转换器");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // 居中显示
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// 输入框
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 5, 5, 5);
add(new JLabel("输入数据(例如 A1:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
inputField = new JTextField();
add(inputField, gbc);
// 源进制下拉菜单
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("源进制数(例如 16:"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
List<Integer> bases = Arrays.asList(2, 4, 8, 10, 16, 32);
sourceBaseBox = new JComboBox<>(bases.toArray(new Integer[0]));
add(sourceBaseBox, gbc);
// 目标进制下拉菜单
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel("目标进制数(例如 2:"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
targetBaseBox = new JComboBox<>(bases.toArray(new Integer[0]));
add(targetBaseBox, gbc);
// 转换按钮
gbc.gridx = 1;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.CENTER;
convertButton = new JButton("转换");
add(convertButton, gbc);
// 结果标签
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.LINE_START;
resultLabel = new JLabel("转换结果:");
add(resultLabel, gbc);
// 添加监听器到按钮
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String input = inputField.getText().toUpperCase();
int sourceBase = (Integer) sourceBaseBox.getSelectedItem();
int targetBase = (Integer) targetBaseBox.getSelectedItem();
String result = convertBase(input, sourceBase, targetBase);
resultLabel.setText("转换结果:" + result);
} catch (Exception ex) {
JOptionPane.showMessageDialog(BaseConverterGUI.this, "输入错误,请检查后重试!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
pack(); // 自动调整大小以适应所有组件
setVisible(true);
}
}
}