From 9103c2e9d5e4f866ac989162361d4ea1996a4574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9C=9D=E9=98=B3?= Date: Thu, 17 Oct 2024 14:36:21 +0800 Subject: [PATCH] czy --- src/czy/czy/BaseConverter.java | 59 ++++++++++++++++++++ src/czy/czy/BaseConverterApp.java | 89 +++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/czy/czy/BaseConverter.java create mode 100644 src/czy/czy/BaseConverterApp.java diff --git a/src/czy/czy/BaseConverter.java b/src/czy/czy/BaseConverter.java new file mode 100644 index 0000000..86f8c2c --- /dev/null +++ b/src/czy/czy/BaseConverter.java @@ -0,0 +1,59 @@ +package czy.czy; + +import java.math.BigInteger; +import java.util.Scanner; + +public class BaseConverter { + + /** + * 将任意进制的数转换为另一个任意进制的数。 + * + * @param numStr 原始数值的字符串表示 + * @param fromBase 原始进制 + * @param toBase 目标进制 + * @return 转换后的字符串表示 + */ + public static String convertBase(String numStr, int fromBase, int toBase) { + // 验证进制是否合法 + if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { + throw new IllegalArgumentException("Base must be between 2 and 16."); + } + + // 将原始进制的数转换为十进制 + BigInteger decimalValue = new BigInteger(numStr, fromBase); + + // 如果目标进制为10,则直接返回 + if (toBase == 10) { + return decimalValue.toString(); + } + + // 将十进制数转换为目标进制 + return decimalValue.toString(toBase).toUpperCase(); // 转换为大写以便更好识别十六进制字符 + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // 读取原始进制 + System.out.print("请输入原始进制 (2-16): "); + int fromBase = scanner.nextInt(); + + // 读取目标进制 + System.out.print("请输入目标进制 (2-16): "); + int toBase = scanner.nextInt(); + + // 读取原始数值 + System.out.print("请输入原始数值: "); + String numStr = scanner.next(); + + // 调用转换方法 + try { + String result = convertBase(numStr, fromBase, toBase); + System.out.println("转换后的结果为: " + result); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + + scanner.close(); + } +} diff --git a/src/czy/czy/BaseConverterApp.java b/src/czy/czy/BaseConverterApp.java new file mode 100644 index 0000000..d47433a --- /dev/null +++ b/src/czy/czy/BaseConverterApp.java @@ -0,0 +1,89 @@ +package czy.czy; + +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +import java.math.BigInteger; + +public class BaseConverterApp extends Application { + + private TextField fromBaseField; + private TextField toBaseField; + private TextField numStrField; + private Label resultLabel; + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("进制转换器"); + + GridPane grid = new GridPane(); + grid.setPadding(new Insets(10, 10, 10, 10)); + grid.setVgap(8); + grid.setHgap(10); + + Label fromBaseLabel = new Label("原始进制:"); + fromBaseField = new TextField(); + fromBaseField.setPromptText("2-16"); + GridPane.setConstraints(fromBaseLabel, 0, 0); + GridPane.setConstraints(fromBaseField, 1, 0); + + Label toBaseLabel = new Label("目标进制:"); + toBaseField = new TextField(); + toBaseField.setPromptText("2-16"); + GridPane.setConstraints(toBaseLabel, 0, 1); + GridPane.setConstraints(toBaseField, 1, 1); + + Label numStrLabel = new Label("原始数值:"); + numStrField = new TextField(); + GridPane.setConstraints(numStrLabel, 0, 2); + GridPane.setConstraints(numStrField, 1, 2); + + Button convertButton = new Button("转换"); + GridPane.setConstraints(convertButton, 1, 3); + + resultLabel = new Label(); + GridPane.setConstraints(resultLabel, 1, 4); + + grid.getChildren().addAll(fromBaseLabel, fromBaseField, toBaseLabel, toBaseField, numStrLabel, numStrField, convertButton, resultLabel); + + Scene scene = new Scene(grid, 300, 200); + primaryStage.setScene(scene); + primaryStage.show(); + + convertButton.setOnAction(event -> { + try { + int fromBase = Integer.parseInt(fromBaseField.getText()); + int toBase = Integer.parseInt(toBaseField.getText()); + String numStr = numStrField.getText(); + String result = conv(numStr, fromBase, toBase); + resultLabel.setText("转换后的结果为: " + result); + } catch (IllegalArgumentException e) { + resultLabel.setText(e.getMessage()); + } + }); + } + + public static void main(String[] args) { + launch(args); + } + + public static String conv(String numStr, int fromBase, int toBase) { + if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { + throw new IllegalArgumentException("Base must be between 2 and 16."); + } + + BigInteger decimalValue = new BigInteger(numStr, fromBase); + + if (toBase == 10) { + return decimalValue.toString(); + } + + return decimalValue.toString(toBase).toUpperCase(); + } +} \ No newline at end of file