diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/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/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5f24dd5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file 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/jinzhi/jinzhi.iml b/jinzhi/jinzhi.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/jinzhi/jinzhi.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/jinzhi/src/JinZhi.java b/jinzhi/src/JinZhi.java new file mode 100644 index 0000000..6b26597 --- /dev/null +++ b/jinzhi/src/JinZhi.java @@ -0,0 +1,103 @@ +import java.util.Scanner; + +public class JinZhi { + + // 辅助方法:将任意进制的数转换为十进制 + /** + * 将任意进制的数转换为十进制数 + * + * @param number 要转换的数的字符串表示 + * @param base 该数的原始进制 + * @return 转换后的十进制数 + */ + public static int toDecimal(String number, int base) { + int decimalValue = 0; // 十进制结果 + int power = 0; // 当前位的权重(幂次) + + // 从字符串的最后一个字符开始遍历 + for (int i = number.length() - 1; i >= 0; i--) { + char digit = number.charAt(i); // 当前字符 + int value = 0; // 当前字符对应的十进制值 + + // 根据字符计算其十进制值 + if (Character.isDigit(digit)) { + value = digit - '0'; // 如果是数字,直接减去'0'的ASCII码 + } else if (digit >= 'A' && digit <= 'F') { + value = digit - 'A' + 10; // 如果是大写字母,减去'A'的ASCII码后加10 + } else if (digit >= 'a' && digit <= 'f') { + value = digit - 'a' + 10; // 如果是小写字母,减去'a'的ASCII码后加10 + } else { + throw new IllegalArgumentException("输入的数字中包含无效字符: " + digit); + } + + // 将当前字符的十进制值乘以权重后累加到结果中 + decimalValue += value * Math.pow(base, power); + power++; // 权重增加 + } + + return decimalValue; + } + + // 辅助方法:将十进制数转换为任意进制 + /** + * 将十进制数转换为任意进制的数 + * + * @param decimalValue 要转换的十进制数 + * @param base 目标进制 + * @return 转换后的目标进制数的字符串表示 + */ + public static String fromDecimal(int decimalValue, int base) { + if (decimalValue == 0) { + return "0"; // 如果十进制数为0,直接返回"0" + } + + StringBuilder result = new StringBuilder(); // 用于存储转换结果的StringBuilder对象 + char[] digits = "0123456789ABCDEF".toCharArray(); // 表示所有可能的进制字符的数组 + + // 使用循环进行进制转换 + while (decimalValue > 0) { + int remainder = decimalValue % base; // 取余数作为当前位的值 + result.insert(0, digits[remainder]); // 将当前位的值添加到结果的开头 + decimalValue /= base; // 十进制数除以目标进制,为下一次循环做准备 + } + + return result.toString(); // 返回转换后的结果字符串 + } + + // 主方法:执行进制转换 + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); // 创建Scanner对象用于读取用户输入 + + // 读取要转换的数、其原始进制和目标进制 + System.out.print("请输入要转换的数: "); + String number = scanner.nextLine(); + System.out.print("请输入该数的原始进制(2-16): "); + int inputBase = scanner.nextInt(); + + // 读取目标进制 + System.out.print("请输入目标进制(2-16): "); + int targetBase = scanner.nextInt(); + + // 验证输入的有效性 + if (inputBase < 2 || inputBase > 16 || targetBase < 2 || targetBase > 16) { + System.out.println("无效的进制。进制必须在2到16之间。"); + return; // 如果输入无效,则直接返回,不执行后续操作 + } + + try { + // 将输入的数转换为十进制数 + int decimalValue = toDecimal(number, inputBase); + + // 将十进制数转换为目标进制数 + String result = fromDecimal(decimalValue, targetBase); + + // 输出转换后的结果 + System.out.println(number + "初始是" + inputBase + "进制;" + "在" + targetBase + "进制下是"+ result); + } catch (IllegalArgumentException e) { + // 捕获并处理非法参数异常 + System.out.println("错误: " + e.getMessage()); + } + + scanner.close(); // 关闭Scanner对象,释放资源 + } +} diff --git a/jisuan/jisuan.iml b/jisuan/jisuan.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/jisuan/jisuan.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/jisuan/src/Main.java b/jisuan/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/jisuan/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/jisuan/src/jisuan1.java b/jisuan/src/jisuan1.java new file mode 100644 index 0000000..6f7cfb1 --- /dev/null +++ b/jisuan/src/jisuan1.java @@ -0,0 +1,211 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class jisuan1{ + public static void main(String[] args) { + JS win=new JS(); + } +} +class JS extends JFrame implements ActionListener{ + private StringBuilder sBuilder = new StringBuilder(); + JTextArea text=new JTextArea(); + double a,b; + Double sum; + int i; + public JS() + { + setBounds(100,100,400,400); + setTitle("计算器"); + this.setLayout(new BorderLayout()); + JPanel p1=new JPanel(); + JPanel p2=new JPanel(); + text.setPreferredSize(new Dimension (370,60)); + p2.setLayout(new FlowLayout()); + p1.add(text); + this.add(p1,BorderLayout.NORTH); + + + p2.setLayout(new GridLayout(5,4)); + JButton button[]=new JButton[20]; + button[0]=new JButton("C"); + button[1]=new JButton(" "); + button[2]=new JButton("%"); + button[3]=new JButton("÷"); + button[4]=new JButton("7"); + button[5]=new JButton("8"); + button[6]=new JButton("9"); + button[7]=new JButton("x"); + button[8]=new JButton("4"); + button[9]=new JButton("5"); + button[10]=new JButton("6"); + button[11]=new JButton("—"); + button[12]=new JButton("1"); + button[13]=new JButton("2"); + button[14]=new JButton("3"); + button[15]=new JButton("+"); + button[16]=new JButton(""); + button[17]=new JButton("0"); + button[18]=new JButton("."); + button[19]=new JButton("="); + + for(int i=0;i + + + + + + + + + + \ No newline at end of file