From ecc5d99038cdf03c0b40e04367c0bd3d0a53a900 Mon Sep 17 00:00:00 2001 From: Cys Date: Thu, 17 Oct 2024 16:56:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=88=E6=98=B1=E6=9D=89=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=9F=E8=83=BD2=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 6 + .idea/shelf/___.xml | 4 + .../shelved.patch | 102 +++++++++ .idea/vcs.xml | 6 + .idea/workspace.xml | 196 ++++++++++++++++++ Git.java | 105 ++++++---- .../cys_cyh_UML/.idea/workspace.xml | 84 ++++++++ uml10_10.java | 105 ++++++---- 8 files changed, 536 insertions(+), 72 deletions(-) create mode 100644 .idea/misc.xml create mode 100644 .idea/shelf/___.xml create mode 100644 .idea/shelf/在变基之前未提交的更改_[更改]/shelved.patch create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 out/production/cys_cyh_UML/.idea/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/shelf/___.xml b/.idea/shelf/___.xml new file mode 100644 index 0000000..5fea805 --- /dev/null +++ b/.idea/shelf/___.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/.idea/shelf/在变基之前未提交的更改_[更改]/shelved.patch b/.idea/shelf/在变基之前未提交的更改_[更改]/shelved.patch new file mode 100644 index 0000000..d205a89 --- /dev/null +++ b/.idea/shelf/在变基之前未提交的更改_[更改]/shelved.patch @@ -0,0 +1,102 @@ +Index: Git.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/Git.java b/Git.java +new file mode 100644 +--- /dev/null (date 1728549940950) ++++ b/Git.java (date 1728549940950) +@@ -0,0 +1,92 @@ ++import java.util.Scanner; ++ ++public class Git { ++ ++ // 将任意进制字符串转换为十进制整数 ++ public static int toDecimal(String number, int base) { ++ int decimal = 0; ++ int power = 0; ++ ++ // 从字符串的末尾开始处理 ++ for (int i = number.length() - 1; i >= 0; i--) { ++ char digit = number.charAt(i); ++ ++ // 如果是数字字符(0-9) ++ if (Character.isDigit(digit)) { ++ int value = Character.getNumericValue(digit); ++ decimal += value * Math.pow(base, power); ++ } ++ // 如果是字母字符(A-F 或 a-f),表示10-15 ++ else if (Character.isLetter(digit)) { ++ char upperDigit = Character.toUpperCase(digit); ++ int value = upperDigit - 'A' + 10; ++ decimal += value * Math.pow(base, power); ++ } else { ++ throw new IllegalArgumentException("Invalid character in number: " + digit); ++ } ++ ++ power++; ++ } ++ ++ return decimal; ++ } ++ ++ // 将十进制整数转换为任意进制字符串 ++ public static String fromDecimal(int number, int base) { ++ if (number == 0) { ++ return "0"; ++ } ++ ++ StringBuilder result = new StringBuilder(); ++ char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 ++ ++ boolean isNegative = number < 0; ++ if (isNegative) { ++ number = -number; ++ } ++ ++ while (number > 0) { ++ int remainder = number % base; ++ result.insert(0, digits[remainder]); ++ number /= base; ++ } ++ ++ if (isNegative) { ++ result.insert(0, '-'); ++ } ++ ++ return result.toString(); ++ } ++ ++ public static void main(String[] args) { ++ Scanner scanner = new Scanner(System.in); ++ ++ System.out.print("请输入源进制(2-16):"); ++ int sourceBase = scanner.nextInt(); ++ scanner.nextLine(); // 消耗掉换行符 ++ ++ System.out.print("请输入目标进制(2-16):"); ++ int targetBase = scanner.nextInt(); ++ scanner.nextLine(); // 消耗掉换行符 ++ ++ System.out.print("请输入要转换的" + sourceBase + "进制数字:"); ++ String number = scanner.nextLine().trim(); // 读取实际的数字输入 ++ ++ // 验证源进制数字是否有效 ++ for (char c : number.toCharArray()) { ++ if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) { ++ throw new IllegalArgumentException("Invalid character in source base number: " + c); ++ } ++ } ++ ++ // 将源进制数字转换为十进制 ++ int decimalNumber = toDecimal(number, sourceBase); ++ ++ // 将十进制数字转换为目标进制 ++ String convertedNumber = fromDecimal(decimalNumber, targetBase); ++ ++ System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber); ++ ++ scanner.close(); ++ } ++} diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..3481ef5 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,196 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 0 +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1728549189282 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Git.java b/Git.java index ce54df3..11367d2 100644 --- a/Git.java +++ b/Git.java @@ -1,22 +1,24 @@ -import java.util.Scanner; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class Git { - // 将任意进制字符串转换为十进制整数 + // 将任意进制字符串转换为十进制整数 public static int toDecimal(String number, int base) { int decimal = 0; int power = 0; - - // 从字符串的末尾开始处理 + // 从字符串的末尾开始处理 for (int i = number.length() - 1; i >= 0; i--) { char digit = number.charAt(i); - // 如果是数字字符(0-9) + // 如果是数字字符(0-9) if (Character.isDigit(digit)) { int value = Character.getNumericValue(digit); decimal += value * Math.pow(base, power); } - // 如果是字母字符(A-F 或 a-f),表示10-15 + // 如果是字母字符(A-F 或 a-f),表示10-15 else if (Character.isLetter(digit)) { char upperDigit = Character.toUpperCase(digit); int value = upperDigit - 'A' + 10; @@ -31,14 +33,14 @@ public class Git { return decimal; } - // 将十进制整数转换为任意进制字符串 + // 将十进制整数转换为任意进制字符串 public static String fromDecimal(int number, int base) { if (number == 0) { return "0"; } StringBuilder result = new StringBuilder(); - char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 + char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 boolean isNegative = number < 0; if (isNegative) { @@ -59,34 +61,67 @@ public class Git { } public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.print("请输入源进制(2-16):"); - int sourceBase = scanner.nextInt(); - scanner.nextLine(); // 消耗掉换行符 - - System.out.print("请输入目标进制(2-16):"); - int targetBase = scanner.nextInt(); - scanner.nextLine(); // 消耗掉换行符 - - System.out.print("请输入要转换的" + sourceBase + "进制数字:"); - String number = scanner.nextLine().trim(); // 读取实际的数字输入 - - // 验证源进制数字是否有效 - for (char c : number.toCharArray()) { - if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) { - throw new IllegalArgumentException("Invalid character in source base number: " + c); + // 创建窗口 + JFrame frame = new JFrame("进制转换器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 300); + frame.setLayout(new GridLayout(5, 2)); // 网格布局 + + // 创建组件 + JLabel sourceBaseLabel = new JLabel("源进制(2-16):"); + JTextField sourceBaseField = new JTextField(); + + JLabel targetBaseLabel = new JLabel("目标进制(2-16):"); + JTextField targetBaseField = new JTextField(); + + JLabel numberLabel = new JLabel("要转换的数字:"); + JTextField numberField = new JTextField(); + + JButton convertButton = new JButton("转换"); + JLabel resultLabel = new JLabel("转换结果:"); + + // 添加组件到窗口 + frame.add(sourceBaseLabel); + frame.add(sourceBaseField); + frame.add(targetBaseLabel); + frame.add(targetBaseField); + frame.add(numberLabel); + frame.add(numberField); + frame.add(convertButton); + frame.add(resultLabel); + + // 添加按钮事件处理 + convertButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + int sourceBase = Integer.parseInt(sourceBaseField.getText().trim()); + int targetBase = Integer.parseInt(targetBaseField.getText().trim()); + String number = numberField.getText().trim(); + + // 验证源进制数字是否有效 + for (char c : number.toCharArray()) { + if (!Character.isDigit(c) && + !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) { + throw new IllegalArgumentException("Invalid character in source base number: " + c); + } + } + + // 将源进制数字转换为十进制 + int decimalNumber = toDecimal(number, sourceBase); + + // 将十进制数字转换为目标进制 + String convertedNumber = fromDecimal(decimalNumber, targetBase); + + // 显示结果 + resultLabel.setText("转换后的" + targetBase + "进制数字是:" + convertedNumber); + } catch (NumberFormatException | IllegalArgumentException ex) { + JOptionPane.showMessageDialog(frame, "错误: " + ex.getMessage(), "输入错误", JOptionPane.ERROR_MESSAGE); + } } - } - - // 将源进制数字转换为十进制 - int decimalNumber = toDecimal(number, sourceBase); - - // 将十进制数字转换为目标进制 - String convertedNumber = fromDecimal(decimalNumber, targetBase); - - System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber); + }); - scanner.close(); + // 显示窗口 + frame.setVisible(true); } } diff --git a/out/production/cys_cyh_UML/.idea/workspace.xml b/out/production/cys_cyh_UML/.idea/workspace.xml new file mode 100644 index 0000000..ab27c32 --- /dev/null +++ b/out/production/cys_cyh_UML/.idea/workspace.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1728549189282 + + + + + + \ No newline at end of file diff --git a/uml10_10.java b/uml10_10.java index 79d6d0b..22f3924 100644 --- a/uml10_10.java +++ b/uml10_10.java @@ -1,22 +1,20 @@ -import java.util.Scanner; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class uml10_10 { - - // 将任意进制字符串转换为十进制整数 public static int toDecimal(String number, int base) { int decimal = 0; int power = 0; - //还好还好 - // 从字符串的末尾开始处理 + // 从字符串的末尾开始处理 for (int i = number.length() - 1; i >= 0; i--) { char digit = number.charAt(i); - // 如果是数字字符(0-9) if (Character.isDigit(digit)) { int value = Character.getNumericValue(digit); decimal += value * Math.pow(base, power); } - // 如果是字母字符(A-F 或 a-f),表示10-15 else if (Character.isLetter(digit)) { char upperDigit = Character.toUpperCase(digit); int value = upperDigit - 'A' + 10; @@ -31,14 +29,14 @@ public class uml10_10 { return decimal; } - // 将十进制整数转换为任意进制字符串 + // 将十进制整数转换为任意进制字符串 public static String fromDecimal(int number, int base) { if (number == 0) { return "0"; } StringBuilder result = new StringBuilder(); - char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 + char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 boolean isNegative = number < 0; if (isNegative) { @@ -59,34 +57,67 @@ public class uml10_10 { } public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.print("请输入源进制(2-16):"); - int sourceBase = scanner.nextInt(); - scanner.nextLine(); // 消耗掉换行符 - - System.out.print("请输入目标进制(2-16):"); - int targetBase = scanner.nextInt(); - scanner.nextLine(); // 消耗掉换行符 - - System.out.print("请输入要转换的" + sourceBase + "进制数字:"); - String number = scanner.nextLine().trim(); // 读取实际的数字输入 - - // 验证源进制数字是否有效 - for (char c : number.toCharArray()) { - if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) { - throw new IllegalArgumentException("Invalid character in source base number: " + c); + // 创建窗口 + JFrame frame = new JFrame("进制转换器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 300); + frame.setLayout(new GridLayout(5, 2)); // 网格布局 + + // 创建组件 + JLabel sourceBaseLabel = new JLabel("源进制(2-16):"); + JTextField sourceBaseField = new JTextField(); + + JLabel targetBaseLabel = new JLabel("目标进制(2-16):"); + JTextField targetBaseField = new JTextField(); + + JLabel numberLabel = new JLabel("要转换的数字:"); + JTextField numberField = new JTextField(); + + JButton convertButton = new JButton("转换"); + JLabel resultLabel = new JLabel("转换结果:"); + + // 添加组件到窗口 + frame.add(sourceBaseLabel); + frame.add(sourceBaseField); + frame.add(targetBaseLabel); + frame.add(targetBaseField); + frame.add(numberLabel); + frame.add(numberField); + frame.add(convertButton); + frame.add(resultLabel); + + // 添加按钮事件处理 + convertButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + int sourceBase = Integer.parseInt(sourceBaseField.getText().trim()); + int targetBase = Integer.parseInt(targetBaseField.getText().trim()); + String number = numberField.getText().trim(); + + // 验证源进制数字是否有效 + for (char c : number.toCharArray()) { + if (!Character.isDigit(c) && + !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) { + throw new IllegalArgumentException("Invalid character in source base number: " + c); + } + } + + // 将源进制数字转换为十进制 + int decimalNumber = toDecimal(number, sourceBase); + + // 将十进制数字转换为目标进制 + String convertedNumber = fromDecimal(decimalNumber, targetBase); + + // 显示结果 + resultLabel.setText("转换后的" + targetBase + "进制数字是:" + convertedNumber); + } catch (NumberFormatException | IllegalArgumentException ex) { + JOptionPane.showMessageDialog(frame, "错误: " + ex.getMessage(), "输入错误", JOptionPane.ERROR_MESSAGE); + } } - } - - // 将源进制数字转换为十进制 - int decimalNumber = toDecimal(number, sourceBase); - - // 将十进制数字转换为目标进制 - String convertedNumber = fromDecimal(decimalNumber, targetBase); - - System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber); + }); - scanner.close(); + // 显示窗口 + frame.setVisible(true); } -} \ No newline at end of file +}