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.

133 lines
4.3 KiB

4 months ago
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
5 months ago
import java.util.Scanner;
public class BaseConverter {
// 字符数组用于表示大于9的数字
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);
}
4 months ago
/**
*
*/
public static void main(String[] args) {
4 months ago
EventQueue.invokeLater(() -> {
new BaseConverterGUI();
});
}
// GUI 类
public static class BaseConverterGUI extends JFrame {
private JTextField inputField;
private JTextField sourceBaseField;
private JTextField targetBaseField;
private JButton convertButton;
private JLabel resultLabel;
4 months ago
public BaseConverterGUI() {
setTitle("进制转换器");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // 居中显示
setLayout(new GridLayout(5, 2));
4 months ago
// 输入框
inputField = new JTextField();
add(new JLabel("输入数据(例如 A1:"));
add(inputField);
4 months ago
// 源进制输入框
sourceBaseField = new JTextField();
add(new JLabel("源进制数(例如 16:"));
add(sourceBaseField);
4 months ago
// 目标进制输入框
targetBaseField = new JTextField();
add(new JLabel("目标进制数(例如 2:"));
add(targetBaseField);
4 months ago
// 转换按钮
convertButton = new JButton("转换");
add(new JLabel());
add(convertButton);
4 months ago
// 结果标签
resultLabel = new JLabel("转换结果:");
add(new JLabel());
add(resultLabel);
// 添加监听器到按钮
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String input = inputField.getText().toUpperCase();
int sourceBase = Integer.parseInt(sourceBaseField.getText());
int targetBase = Integer.parseInt(targetBaseField.getText());
String result = convertBase(input, sourceBase, targetBase);
resultLabel.setText("转换结果:" + result);
} catch (Exception ex) {
JOptionPane.showMessageDialog(BaseConverterGUI.this, "输入错误,请检查后重试!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
setVisible(true);
}
}
4 months ago
5 months ago
}