From d2589957147a3118f5eb31692f12669e830e557e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9C=9D=E9=98=B3?= Date: Thu, 10 Oct 2024 15:31:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/elt.iml | 9 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 9 +++ .idea/vcs.xml | 6 ++ text1/src/src/BaseConverter.java | 59 +++++++++++++++++++ text1/src/src/BaseConverterApp.java | 89 +++++++++++++++++++++++++++++ text1/src/src/Main.java | 17 ++++++ text1/text1.iml | 11 ++++ 9 files changed, 214 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/elt.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 text1/src/src/BaseConverter.java create mode 100644 text1/src/src/BaseConverterApp.java create mode 100644 text1/src/src/Main.java create mode 100644 text1/text1.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/elt.iml b/.idea/elt.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/elt.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..66f29f6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9399180 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/text1/src/src/BaseConverter.java b/text1/src/src/BaseConverter.java new file mode 100644 index 0000000..2f5c53a --- /dev/null +++ b/text1/src/src/BaseConverter.java @@ -0,0 +1,59 @@ +package src; + +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/text1/src/src/BaseConverterApp.java b/text1/src/src/BaseConverterApp.java new file mode 100644 index 0000000..127d2aa --- /dev/null +++ b/text1/src/src/BaseConverterApp.java @@ -0,0 +1,89 @@ +package src; + +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 diff --git a/text1/src/src/Main.java b/text1/src/src/Main.java new file mode 100644 index 0000000..bb65604 --- /dev/null +++ b/text1/src/src/Main.java @@ -0,0 +1,17 @@ +package src; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/text1/text1.iml b/text1/text1.iml new file mode 100644 index 0000000..37cc804 --- /dev/null +++ b/text1/text1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file