Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
|
17ef4b703d | 9 months ago |
|
1ea76fef40 | 10 months ago |
|
bd1029dec6 | 10 months ago |
|
1f84ac1076 | 10 months ago |
@ -0,0 +1,3 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.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="openjdk-22" 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/Decimal-conversion.iml" filepath="$PROJECT_DIR$/.idea/Decimal-conversion.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -0,0 +1,103 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class BaseConverterGUI extends JFrame implements ActionListener {
|
||||||
|
private JTextField inputField;
|
||||||
|
private JTextField sourceBaseField;
|
||||||
|
private JTextField targetBaseField;
|
||||||
|
private JButton convertButton;
|
||||||
|
private JLabel resultLabel;
|
||||||
|
|
||||||
|
public BaseConverterGUI() {
|
||||||
|
setTitle("基数转换器");
|
||||||
|
setSize(400, 200);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
// 输入框
|
||||||
|
inputField = new JTextField(10);
|
||||||
|
add(new JLabel("输入数值:"));
|
||||||
|
add(inputField);
|
||||||
|
|
||||||
|
// 源基数输入框
|
||||||
|
sourceBaseField = new JTextField(5);
|
||||||
|
add(new JLabel("源基数:"));
|
||||||
|
add(sourceBaseField);
|
||||||
|
|
||||||
|
// 目标基数输入框
|
||||||
|
targetBaseField = new JTextField(5);
|
||||||
|
add(new JLabel("目标基数:"));
|
||||||
|
add(targetBaseField);
|
||||||
|
|
||||||
|
// 转换按钮
|
||||||
|
convertButton = new JButton("转换");
|
||||||
|
convertButton.addActionListener(this);
|
||||||
|
add(convertButton);
|
||||||
|
|
||||||
|
// 结果标签
|
||||||
|
resultLabel = new JLabel("结果:");
|
||||||
|
add(resultLabel);
|
||||||
|
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (e.getSource() == convertButton) {
|
||||||
|
String numStr = inputField.getText();
|
||||||
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
||||||
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
||||||
|
|
||||||
|
try {
|
||||||
|
String convertedNum = convertBase(numStr, sourceBase, targetBase);
|
||||||
|
resultLabel.setText("转换结果: " + convertedNum + " (基数 " + targetBase + ")");
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "输入无效,请检查输入。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String convertBase(String numStr, int sourceBase, int targetBase) {
|
||||||
|
int decimal = convertToDecimal(numStr, sourceBase);
|
||||||
|
return convertFromDecimal(decimal, targetBase);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int convertToDecimal(String numStr, int base) {
|
||||||
|
return Integer.parseInt(numStr, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String convertFromDecimal(int decimal, int base) {
|
||||||
|
if (base < 2 || base > 16) {
|
||||||
|
throw new IllegalArgumentException("基数必须在2到16之间");
|
||||||
|
}
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
boolean isNegative = decimal < 0;
|
||||||
|
decimal = Math.abs(decimal);
|
||||||
|
|
||||||
|
while (decimal > 0) {
|
||||||
|
int remainder = decimal % base;
|
||||||
|
if (remainder < 10) {
|
||||||
|
result.append(remainder);
|
||||||
|
} else {
|
||||||
|
result.append((char) ('A' + remainder - 10)); // Convert to A-F for bases > 10
|
||||||
|
}
|
||||||
|
decimal /= base;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.length() == 0) {
|
||||||
|
return "0";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNegative) {
|
||||||
|
result.append('-');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.reverse().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> new BaseConverterGUI());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>基数转换器</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>基数转换器</h1>
|
||||||
|
<form id="baseConverterForm">
|
||||||
|
<label for="sourceBase">源基数 (2-16):</label>
|
||||||
|
<input type="number" id="sourceBase" name="sourceBase" min="2" max="16" required>
|
||||||
|
<br><br>
|
||||||
|
<label for="valueToConvert">要转换的值:</label>
|
||||||
|
<input type="text" id="valueToConvert" name="valueToConvert" required>
|
||||||
|
<br><br>
|
||||||
|
<label for="targetBase">目标基数 (2-16):</label>
|
||||||
|
<input type="number" id="targetBase" name="targetBase" min="2" max="16" required>
|
||||||
|
<br><br>
|
||||||
|
<button type="submit">转换</button>
|
||||||
|
</form>
|
||||||
|
<div id="result"></div>
|
||||||
|
|
||||||
|
<!-- JavaScript代码将放在这里 -->
|
||||||
|
<script>
|
||||||
|
document.getElementById('baseConverterForm').addEventListener('submit', function(event) {
|
||||||
|
event.preventDefault(); // 阻止表单默认提交行为
|
||||||
|
|
||||||
|
const sourceBase = parseInt(document.getElementById('sourceBase').value);
|
||||||
|
const targetBase = parseInt(document.getElementById('targetBase').value);
|
||||||
|
const valueToConvert = document.getElementById('valueToConvert').value.toUpperCase();
|
||||||
|
|
||||||
|
// 转换逻辑
|
||||||
|
let decimalValue = parseInt(valueToConvert, sourceBase);
|
||||||
|
let convertedValue = decimalValue.toString(targetBase);
|
||||||
|
|
||||||
|
// 将结果展示在页面上
|
||||||
|
document.getElementById('result').innerHTML = `转换后的值: ${convertedValue}`;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,3 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.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="openjdk-22" 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/Decimal-conversion.iml" filepath="$PROJECT_DIR$/.idea/Decimal-conversion.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
Binary file not shown.
@ -0,0 +1,2 @@
|
|||||||
|
# Decimal-conversion
|
||||||
|
|
@ -0,0 +1,125 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>进制转换器</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Arial', sans-serif;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 30px;
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||||
|
max-width: 400px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
input[type="number"],
|
||||||
|
input[type="text"] {
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 5px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #5cb85c;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.3s ease;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #4cae4c;
|
||||||
|
}
|
||||||
|
#result {
|
||||||
|
margin-top: 20px;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #dff0d8;
|
||||||
|
border: 1px solid #d6e9c6;
|
||||||
|
border-radius: 5px;
|
||||||
|
color: #3c763d;
|
||||||
|
}
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
body {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1>进制转换器</h1>
|
||||||
|
<form id="baseConverterForm">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="sourceBase">初始进制(2-16):</label>
|
||||||
|
<input type="number" id="sourceBase" name="sourceBase" min="2" max="16" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="valueToConvert">要转换的值:</label>
|
||||||
|
<input type="text" id="valueToConvert" name="valueToConvert" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="targetBase">目标进制(2-16):</label>
|
||||||
|
<input type="number" id="targetBase" name="targetBase" min="2" max="16" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit">转换</button>
|
||||||
|
</form>
|
||||||
|
<div id="result"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.getElementById('baseConverterForm').addEventListener('submit', function(event) {
|
||||||
|
event.preventDefault(); // 阻止表单默认提交行为
|
||||||
|
|
||||||
|
const sourceBase = parseInt(document.getElementById('sourceBase').value);
|
||||||
|
const targetBase = parseInt(document.getElementById('targetBase').value);
|
||||||
|
const valueToConvert = document.getElementById('valueToConvert').value;
|
||||||
|
|
||||||
|
// 转换逻辑
|
||||||
|
let decimalValue;
|
||||||
|
try {
|
||||||
|
decimalValue = parseInt(valueToConvert, sourceBase);
|
||||||
|
} catch (error) {
|
||||||
|
document.getElementById('result').innerHTML = '请按规范输入!!!';
|
||||||
|
return; // 遇到错误时停止执行
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNaN(decimalValue)) {
|
||||||
|
document.getElementById('result').innerHTML = '请按规范输入!!!';
|
||||||
|
} else {
|
||||||
|
let convertedValue = decimalValue.toString(targetBase);
|
||||||
|
// 将结果展示在页面上
|
||||||
|
document.getElementById('result').innerHTML = `转换后的值: <span style="font-family: monospace;">${convertedValue}</span>`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in new issue