|
|
|
|
package java6257.lesson06;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
|
|
// 符号转化为数字
|
|
|
|
|
public static int getNum(char c) {
|
|
|
|
|
if (Character.isDigit(c)) {
|
|
|
|
|
return c - '0';
|
|
|
|
|
}
|
|
|
|
|
if ('a' <= c && c <= 'z') {
|
|
|
|
|
return c - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
if ('A' <= c && c <= 'Z') {
|
|
|
|
|
return c - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 数字转化为符号
|
|
|
|
|
public static char getChar(int num) {
|
|
|
|
|
if (num >= 0 && num <= 9) {
|
|
|
|
|
return (char) (num + '0');
|
|
|
|
|
} else {
|
|
|
|
|
return (char) (num - 10 + 'A');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将字符串根据进制转化为数字
|
|
|
|
|
public static long toNum(String number, int base) {
|
|
|
|
|
int n = number.length();
|
|
|
|
|
long num = 0;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
char ch = number.charAt(i);
|
|
|
|
|
num *= base;
|
|
|
|
|
num += getNum(ch);
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将数字根据进制转化为字符串
|
|
|
|
|
public static String toString(long num, int base) {
|
|
|
|
|
if (num == 0) return "0";
|
|
|
|
|
|
|
|
|
|
StringBuilder ans = new StringBuilder();
|
|
|
|
|
while (num > 0) {
|
|
|
|
|
ans.append(getChar((int) (num % base)));
|
|
|
|
|
num /= base;
|
|
|
|
|
}
|
|
|
|
|
ans.reverse();
|
|
|
|
|
return ans.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行进制转换
|
|
|
|
|
public static String convertBase(String num, int stBase, int enBase) {
|
|
|
|
|
return toString(toNum(num, stBase), enBase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
// 创建窗口
|
|
|
|
|
JFrame frame = new JFrame("进制转换器");
|
|
|
|
|
frame.setSize(400, 300);
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
frame.setLayout(null);
|
|
|
|
|
|
|
|
|
|
// 源进制输入框
|
|
|
|
|
JLabel stBaseLabel = new JLabel("源进制(2-16):");
|
|
|
|
|
stBaseLabel.setBounds(50, 20, 120, 25);
|
|
|
|
|
frame.add(stBaseLabel);
|
|
|
|
|
|
|
|
|
|
JTextField stBaseField = new JTextField();
|
|
|
|
|
stBaseField.setBounds(200, 20, 100, 25);
|
|
|
|
|
frame.add(stBaseField);
|
|
|
|
|
|
|
|
|
|
// 目标进制输入框
|
|
|
|
|
JLabel enBaseLabel = new JLabel("目标进制(2-16):");
|
|
|
|
|
enBaseLabel.setBounds(50, 60, 120, 25);
|
|
|
|
|
frame.add(enBaseLabel);
|
|
|
|
|
|
|
|
|
|
JTextField enBaseField = new JTextField();
|
|
|
|
|
enBaseField.setBounds(200, 60, 100, 25);
|
|
|
|
|
frame.add(enBaseField);
|
|
|
|
|
|
|
|
|
|
// 源数字输入框
|
|
|
|
|
JLabel numberLabel = new JLabel("源数字:");
|
|
|
|
|
numberLabel.setBounds(50, 100, 120, 25);
|
|
|
|
|
frame.add(numberLabel);
|
|
|
|
|
|
|
|
|
|
JTextField numberField = new JTextField();
|
|
|
|
|
numberField.setBounds(200, 100, 100, 25);
|
|
|
|
|
frame.add(numberField);
|
|
|
|
|
|
|
|
|
|
// 目标数字显示框
|
|
|
|
|
JLabel resultLabel = new JLabel("转换后的数字:");
|
|
|
|
|
resultLabel.setBounds(50, 140, 120, 25);
|
|
|
|
|
frame.add(resultLabel);
|
|
|
|
|
|
|
|
|
|
JTextField resultField = new JTextField();
|
|
|
|
|
resultField.setBounds(200, 140, 100, 25);
|
|
|
|
|
resultField.setEditable(false); // 只读
|
|
|
|
|
frame.add(resultField);
|
|
|
|
|
|
|
|
|
|
// 转换按钮
|
|
|
|
|
JButton convertButton = new JButton("转换");
|
|
|
|
|
convertButton.setBounds(150, 180, 100, 30);
|
|
|
|
|
frame.add(convertButton);
|
|
|
|
|
|
|
|
|
|
// 按钮监听事件
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取输入的源进制、目标进制和数字
|
|
|
|
|
int stBase = Integer.parseInt(stBaseField.getText());
|
|
|
|
|
int enBase = Integer.parseInt(enBaseField.getText());
|
|
|
|
|
String number = numberField.getText();
|
|
|
|
|
|
|
|
|
|
// 检查进制范围
|
|
|
|
|
if (stBase < 2 || stBase > 16 || enBase < 2 || enBase > 16) {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "进制必须在 2 到 16 之间", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 进行转换
|
|
|
|
|
String result = convertBase(number, stBase, enBase);
|
|
|
|
|
resultField.setText(result);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "请输入正确的数字格式", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 显示窗口
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|