Compare commits

...

2 Commits
main ... master

Author SHA1 Message Date
Cys cc4e40dd46 陈昱杉新增功能
1 month ago
Cys e7a7ed430b Cys2
1 month ago

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cys_cyh_UML.iml" filepath="$PROJECT_DIR$/.idea/cys_cyh_UML.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,92 @@
import java.util.Scanner;
public class Cys {
// 将任意进制字符串转换为十进制整数
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();
}
}

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cys_cyh_UML.iml" filepath="$PROJECT_DIR$/.idea/cys_cyh_UML.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
Loading…
Cancel
Save