From 4d982d7e926a90d62a3e6c23127a81010e7f3279 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Thu, 3 Jun 2021 20:56:50 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=B4=9F=E6=95=B0?= =?UTF-8?q?=E5=8F=AF=E5=8F=82=E4=B8=8E=E5=8A=A0=E5=87=8F=E4=B9=98=E9=99=A4?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 582 ++++++++++++++++++++++++ src/java2020spring/PanelNullLayout.java | 32 ++ src/java2020spring/Test.java | 13 +- 3 files changed, 625 insertions(+), 2 deletions(-) create mode 100644 src/java2020spring/Calculator.java create mode 100644 src/java2020spring/PanelNullLayout.java diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java new file mode 100644 index 0000000..e6c463f --- /dev/null +++ b/src/java2020spring/Calculator.java @@ -0,0 +1,582 @@ +package java2020spring; + +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.util.LinkedList; +import java.util.Scanner; + +public class Calculator extends JFrame implements ActionListener { + public static JFrame mainframe; + public static JTextField showText1, showText2; // 1为显示框,2为输入框 + public static JPanel mainp = new JPanel(); + public static JPanel p = new JPanel(); // 放按钮 + public static JPanel p0 = new JPanel(); // 放文本框 + public static JMenuBar menubar; + public static JMenu menu, subMenu1, subMenu2, menu2, menu3, subMenu3; + public static String str1 = "", str2 = ""; + double result, m, n, flag = 0; // flag指示变量,判断按"="前按的是哪个按钮 + public static String[] anniu = { "e^x", "π", "e", "C", "DEL", + + "X^2", "1/x", "|x|", "exp", "mod", + + "√x", "(", ")", "n!", "÷", + + "x^y", "7", "8", "9", "x", + + "10^x", "4", "5", "6", "-", + + "lg", "1", "2", "3", "+", + + "ln", "+/-", "0", ".", "=" }; + public static String[] sanjiao = { "sin", "cos", "tan", "sec", "csc", "cot" }; + public static String[] fansanjiao = { "sin^-1", "cos^-1", "tan^-1", "根据角度求弧度" }; + public static String[] gengduo = { "x^3", "3√x", "y√x", "2^x", "logyx" }; + public JButton button[] = new JButton[anniu.length]; + public JButton sanjiao1[] = new JButton[sanjiao.length]; + public JButton fansanjiao1[] = new JButton[fansanjiao.length]; + public JButton qita[] = new JButton[gengduo.length]; + + public Calculator() { + mainframe = Test.returnFrame(); + init(); + setVisible(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + } + + void init() { + + mainp.setLayout(new BorderLayout()); + + menubar = new JMenuBar(); + menu = new JMenu("三角学"); + menu2 = new JMenu("更多"); + menu3 = new JMenu("计算时间"); + subMenu1 = new JMenu("1nd"); + subMenu2 = new JMenu("2nd"); + subMenu3 = new JMenu("另一界面"); + menu3.add(subMenu3); + // subMenu1.setAccelerator(KeyStroke.getKeyStroke('A')); //选择计算三角函数按快捷建A + // subMenu2.setAccelerator(KeyStroke.getKeyStroke('B'));//选择计算反三角函数按快捷键B + menu.add(subMenu1); + menu.addSeparator(); + menu.add(subMenu2); + // 为子菜单添加按钮 + /* 子菜单1计算三角函数 */ + JPanel p1 = new JPanel(); + for (int i = 0; i < 6; i++) { + sanjiao1[i] = new JButton(sanjiao[i]); + p1.add(sanjiao1[i]); + } + /* 子菜单2计算反三角函数 */ + JPanel p2 = new JPanel(); + for (int i = 0; i < 4; i++) { + fansanjiao1[i] = new JButton(fansanjiao[i]); + p2.add(fansanjiao1[i]); + } + /* 菜单“更多” */ + JPanel p3 = new JPanel(); + for (int i = 0; i < 5; i++) { + qita[i] = new JButton(gengduo[i]); + p3.add(qita[i]); + } + // JPanel p4=new JPanel(); + subMenu1.add(p1); + subMenu2.add(p2); + menu2.add(p3); + // menu3.add(p4); + + menubar.add(menu); + menubar.add(menu2); + menubar.add(menu3); + + this.setJMenuBar(menubar);// 添加计算三角学、更多函数的功能 + + showText1 = new JTextField(30); + showText2 = new JTextField(30); + showText1.setFont(new java.awt.Font("黑体", Font.PLAIN, 40)); + showText2.setFont(new java.awt.Font("黑体", Font.PLAIN, 40)); + this.setBackground(Color.cyan); + setLayout(new BorderLayout()); + p.setLayout(new GridLayout(7, 5)); + p0.setLayout(new BorderLayout()); + p0.add(showText1, BorderLayout.NORTH); + + p0.add(showText2, BorderLayout.CENTER); + mainp.add(p0, BorderLayout.NORTH); + mainp.add(p, BorderLayout.CENTER); + add(mainp); + showText1.setSize(300, 50); + showText2.setSize(20, 70); + showText1.setEnabled(false); + for (int i = 0; i < anniu.length; i++) { + + button[i] = new JButton(anniu[i]); + // button[i].setPreferredSize(new Dimension(20,20)); + button[i].setFont(new Font("黑体", 1, 20)); + p.add(button[i]); + } + + for (int i = 0; i < anniu.length; i++) { + if (i == 16 || i == 17 || i == 18 || i == 21 || i == 22 || i == 23 || i == 26 || i == 27 || i == 28 + || i == 31 || i == 32 || i == 33) { + button[i].setBackground(Color.white); + } else { + + button[i].setBackground(Color.lightGray); + + } + } + + showText1.setHorizontalAlignment(SwingConstants.RIGHT); + showText2.setHorizontalAlignment(SwingConstants.RIGHT); + + subMenu3.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + mainframe.remove(mainp); + System.out.println("nihao "); + mainp = new PanelNullLayout().View(); + mainframe.add(mainp); + } + }); + + // 版面设计 + + for (int i = 0; i < button.length; i++) { // 每个按钮都注册监听器 + button[i].addActionListener(this); + } + for (int i = 0; i < sanjiao1.length; i++) { + sanjiao1[i].addActionListener(this); + } + for (int i = 0; i < fansanjiao1.length; i++) { + fansanjiao1[i].addActionListener(this); + } + for (int i = 0; i < qita.length; i++) { + qita[i].addActionListener(this); + } + + } + + @Override + public void actionPerformed(ActionEvent e) { + + // 实现相关功能 + try { + if (showText2.getText() == "") + throw new java.lang.NumberFormatException(); + } catch (java.lang.NumberFormatException ee) { + System.out.println("无效输入"); + } + + // 按下1-9 + if (e.getSource() == button[16] || e.getSource() == button[17] || e.getSource() == button[18] + || e.getSource() == button[21] || e.getSource() == button[22] || e.getSource() == button[23] + || e.getSource() == button[26] || e.getSource() == button[27] || e.getSource() == button[28]) { + str2 = str2 + e.getActionCommand(); + showText2.setText(str2); + showText1.setText(str2); + } + // 按下0 + if (e.getActionCommand().equals("0")) { + str2 += e.getActionCommand(); + showText2.setText(str2); + } + + // 按下小数点 + if (e.getActionCommand().equals(".")) { + str2 += "."; + showText2.setText(str2); + } + // 按下 "(" + if (e.getActionCommand().equals("(")) { + str2 += "("; + showText2.setText(str2); + } + // 按下")" + if (e.getActionCommand().equals(")")) { + str2 += ")"; + showText2.setText(str2); + } + // 按下DEL退一格 + if (e.getActionCommand().equals("DEL")) { + if (str2.length() == 0 && str2.length() == 1) { + str2 = ""; + } else { + StringBuffer s = new StringBuffer(showText2.getText()); + s.deleteCharAt(showText2.getText().length() - 1); + showText2.setText(s.toString()); + str1 = showText2.getText(); + showText1.setText(str1); + str2 = s.toString(); + } + } + // 按下C按钮清除 + if (e.getActionCommand().equals("C")) { + str1 = ""; + str2 = ""; + showText1.setText(str1); + showText2.setText(str2); + } + + // 按下加减乘除 + if (e.getActionCommand().equals("+") || e.getActionCommand().equals("-") || e.getActionCommand().equals("x") + || e.getActionCommand().equals("÷")) { + str2 = str2 + e.getActionCommand(); + showText1.setText(str2); + + } + // "x^y" + if (e.getActionCommand().equals("x^y")) { + str1 = str2; + str2 = ""; + flag = 1; + + } + if (e.getActionCommand().equals("=") && flag == 1) { + m = Double.parseDouble(str2); + n = Double.parseDouble(str1); + result = Math.pow(m, n); + showText2.setText(result + ""); + flag = 0; + } + // "exp"科学计数 + if (e.getActionCommand().equals("exp")) { + str1 = str2; + str2 = ""; + flag = 2; + } + if (e.getActionCommand().equals("=") && flag == 2) { + m = Double.parseDouble(str2); + n = Double.parseDouble(str1); + result = Math.pow(10, m) * n; + showText2.setText(result + ""); + flag = 0; + } + // "mod"求余 + if (e.getActionCommand().equals("mod")) { + str1 = str2; + str2 = ""; + flag = 3; + } + if (e.getActionCommand().equals("=") && flag == 3) { + n = Double.parseDouble(str2); + m = Double.parseDouble(str1); + result = m % n; + str2 = result + ""; + showText2.setText(str2); + flag = 0; + } + // 按下"=" + if (e.getActionCommand().equals("=")) { + showText1.setText(showText2.getText() + button[34].getText()); + Scanner scanner = new Scanner(showText2.getText()); + str2 = saveNumber_Symbol(scanner.next()); // 分别存符号和数字 + showText2.setText(str2); + + } + + // 开根号 + if (e.getActionCommand().equals("√x")) { + m = Double.parseDouble(str2); + if (m >= 0) { + result = Math.sqrt(m); + str2 = "" + result; + showText2.setText(str2); + + } else { + showText1.setText("数值大于零,才能开根号"); + } + } + // 平方 + if (e.getActionCommand().equals("X^2")) { + m = Double.parseDouble(str2); + result = m * m; + str2 = "" + result; + showText2.setText(str2); + + } + // 倒数 + if (e.getActionCommand().equals("1/x")) { + if (str2 == "") { + showText1.setText("除数不能为0!"); + } else if (Double.parseDouble(str2) != 0) { + m = Double.parseDouble(str2); + result = 1 / m; + str2 = "" + result; + showText2.setText(str2); + + } else { + showText1.setText("除数不能为0!"); + } + } + // 求e^x + if (e.getActionCommand().equals("e^x")) { + m = Double.parseDouble(str2); + result = Math.pow(2.718281828459, m); + str2 = result + ""; + showText2.setText(str2); + } + + // "+/-"号 ?? + if (e.getActionCommand().equals("+/-")) { + str2 = "-" + str2; + showText2.setText(str2); + + } + // "π" + if (e.getActionCommand().equals("π")) { + str2 += "3.1415926535898"; + showText2.setText(str2); + } + // "e" + if (e.getActionCommand().equals("e")) { + str2 += "2.718281828459"; + showText2.setText(str2); + } + // "|x|" + if (e.getActionCommand().equals("|x|")) { + + m = Double.parseDouble(str2); + result = Math.abs(m); + str2 = result + ""; + showText2.setText(str2); + + } + // "n!" + if (e.getActionCommand().equals("n!")) { + m = Double.parseDouble(str2); + if (m % 1 != 0) { + showText1.setText("error!请输入整数"); + } else if (m >= 0) { // 0的阶乘为1 + result = 1; + for (int i = 1; i <= m; i++) { + result = result * i; + } + } + str2 = result + ""; + showText2.setText(str2); + } + + // "10^x" + if (e.getActionCommand().equals("10^x")) { + m = Double.parseDouble(str2); + result = Math.pow(10, m); + str2 = result + ""; + showText2.setText(str2); + } + // "lg" + if (e.getActionCommand().equals("lg")) { + m = Double.parseDouble(str2); + if (m > 0) { + result = Math.log10(m); + str2 = result + ""; + showText2.setText(str2); + } else { + showText1.setText("请输入大于0的数!"); + } + } + // "ln" + if (e.getActionCommand().equals("ln")) { + m = Double.parseDouble(str2); + if (m > 0) { + result = Math.log(m); + str2 = "" + Integer.parseInt(new java.text.DecimalFormat("0").format(result)); // 四舍五入 + showText2.setText(str2); + } else { + showText1.setText("请输入大于0的数!"); + } + + } + // 菜单“更多”里 + if (e.getActionCommand().equals("x^3")) { + m = Double.parseDouble(str2); + result = m * m * m; + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("3√x")) { + m = Double.parseDouble(str2); + result = Math.cbrt(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("y√x")) { + str1 = str2; + str2 = ""; + flag = 4; + } + if (e.getActionCommand().equals("=") && flag == 4) { + m = Double.parseDouble(str1); + n = Double.parseDouble(str2); + result = Math.pow(m, 1 / n); + str2 = result + ""; + showText2.setText(str2); + flag = 0; + } + if (e.getActionCommand().equals("2^x")) { + m = Double.parseDouble(str2); + result = Math.pow(2, m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("logyx")) { + flag = 5; + str1 = str2; + str2 = ""; + } + if (e.getActionCommand().equals("=") && flag == 5) { + m = Double.parseDouble(str1); + n = Double.parseDouble(str2); + if (n != 1 && m > 0 && n > 0) { + result = Math.log(m) / Math.log(n); + str2 = result + ""; + showText2.setText(str2); + flag = 0; + } else { + showText1.setText("真数需都大于0,且分母不能为0"); + } + } + // 三角学“1nd”的计算 + if (e.getActionCommand().equals("sin")) { + m = Double.parseDouble(str2); + result = Math.sin(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("cos")) { + m = Double.parseDouble(str2); + result = Math.cos(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("tan")) { + m = Double.parseDouble(str2); + result = Math.tan(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("sec")) { + m = Double.parseDouble(str2); + result = 1 / Math.cos(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("csc")) { + m = Double.parseDouble(str2); + result = 1 / Math.cos(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("cot")) { + m = Double.parseDouble(str2); + result = 1 / Math.tan(m); + str2 = result + ""; + showText2.setText(str2); + } + // 三角学"2nd"即已知值求角度的计算 + if (e.getActionCommand().equals("sin^-1")) { + m = Double.parseDouble(str2); + result = Math.asin(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("cos^-1")) { + m = Double.parseDouble(str2); + result = Math.acos(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("tan^-1")) { + m = Double.parseDouble(str2); + result = Math.atan(m); + str2 = result + ""; + showText2.setText(str2); + } + if (e.getActionCommand().equals("根据角度求弧度")) { + m = Double.parseDouble(str2); + result = Math.toRadians(m); + str2 = result + ""; + showText2.setText(str2); + } + + } + + /* 分割字符串存储运算符和数字分别到calcuStrList和numberList */ + public String saveNumber_Symbol(String str) { + if (str.startsWith("-")) { + StringBuffer s1 = new StringBuffer(str); + s1.insert(0, "0"); + str = s1.toString(); + } // 若第一个数字是负数,在"-"前加0 + + str = str + "#"; // 加上"#"用于获取分割最后一个数字 + char[] strChar = str.toCharArray(); + LinkedList calcuStrList = new LinkedList<>();// 存储运算符号 + LinkedList numberList = new LinkedList<>();// 存储数字 + StringBuffer buff = new StringBuffer(); + for (char fuhao : strChar) { + if (fuhao == '+' || fuhao == '-' || fuhao == 'x' || fuhao == '÷') { + calcuStrList.add(String.valueOf(fuhao)); + numberList.add(buff.toString()); + buff.delete(0, buff.length()); + } else if (fuhao == '#') {// 判断是不是最后一个数 + numberList.add(buff.toString()); + } else { + buff.append(fuhao); + } + } + return calculationOrder(calcuStrList, numberList); + } + + /* 控制加减乘除的运算优先级顺序 */ + public String calculationOrder(LinkedList calcuStr, LinkedList number) { + if (calcuStr.size() > 0) { + int multiply = calcuStr.indexOf("x"); + int except = calcuStr.indexOf("÷"); + + // 除在前乘在后,或有除无乘,则先算除 + if ((multiply != -1 && except != -1 && except < multiply) + || (calcuStr.contains("÷") && !calcuStr.contains("x"))) { + calcuResult("÷", calcuStr, number); + } + // 乘在前除在后,或有乘无除,先算乘 + if ((multiply != -1 && except != -1 && multiply < except) + || (calcuStr.contains("x") && !calcuStr.contains("÷"))) { + calcuResult("x", calcuStr, number); + } + // 无乘除只有加减,则从左往右计算 + else if (calcuStr.size() > 0) { + calcuResult(calcuStr.get(0), calcuStr, number); + } + } + return number.get(0); + } + + /* 通过symbol找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 */ + public String calcuResult(String symbol, LinkedList calcuStr, LinkedList number) { + int position = calcuStr.indexOf(symbol); + double a = Double.parseDouble(number.get(position)); + double b = Double.parseDouble(number.get(position + 1)); + String calculateNumber = ""; + switch (symbol) { + case "+": + calculateNumber = String.valueOf(a + b); + break; + case "-": + calculateNumber = String.valueOf(a - b); + break; + case "x": + calculateNumber = String.valueOf(a * b); + break; + case "÷": + calculateNumber = String.valueOf(a / b); + break; + } + number.set(position, calculateNumber);// 两个数运算完后将计算结果覆盖到第一个数的位置 + calcuStr.remove(position);// 每用完一个运算符就删除该运算符 + number.remove(position + 1);// 两个数运算完后删除后面的数 + return calculationOrder(calcuStr, number); + } + +} diff --git a/src/java2020spring/PanelNullLayout.java b/src/java2020spring/PanelNullLayout.java new file mode 100644 index 0000000..59ffa27 --- /dev/null +++ b/src/java2020spring/PanelNullLayout.java @@ -0,0 +1,32 @@ +package java2020spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class PanelNullLayout extends JPanel implements ActionListener { + JTextField t1 = new JTextField(); + static JPanel p; + + PanelNullLayout() { + + } + + public static JPanel View() { + JFrame frame = Test.returnFrame(); + JButton b = new JButton("确定"); + p = new JPanel(); + p.add(b); + frame.add(p); + p.setLayout(null); + p.setBounds(100, 100, 420, 600); + System.out.println("nihao"); + + return p; + + } + + public void actionPerformed(ActionEvent e) { + + } +} diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java index fc09c19..4d3cffd 100644 --- a/src/java2020spring/Test.java +++ b/src/java2020spring/Test.java @@ -1,10 +1,19 @@ package java2020spring; +import javax.swing.JFrame; + public class Test { + static Calculator win; public static void main(String[] args) { - System.out.println("Hello world!"); - + win = new Calculator(); + win.setTitle("ljl's calculator"); + win.setBounds(100, 100, 420, 600); + win.setVisible(true); + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } + public static JFrame returnFrame() { + return win; + } } -- Gitee From 7a6bafd30b735030f3d9da08c6991195558aa366 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Wed, 9 Jun 2021 12:39:55 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=AE=8C=E6=88=90=E8=B4=9F=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E5=8A=A0=E5=87=8F=E4=B9=98=E9=99=A4=E8=BF=90=E7=AE=97=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E8=AE=A1=E7=AE=97=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 197 ++++++++++++++---------- src/java2020spring/PanelNullLayout.java | 119 +++++++++++--- src/java2020spring/Test.java | 7 +- 3 files changed, 217 insertions(+), 106 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index e6c463f..7d3d3b2 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -7,13 +7,12 @@ import java.util.LinkedList; import java.util.Scanner; public class Calculator extends JFrame implements ActionListener { - public static JFrame mainframe; public static JTextField showText1, showText2; // 1为显示框,2为输入框 - public static JPanel mainp = new JPanel(); - public static JPanel p = new JPanel(); // 放按钮 - public static JPanel p0 = new JPanel(); // 放文本框 + public static JPanel mainp = new JPanel(); //mainp为主面板 + public static JPanel p = new JPanel(); // 放按钮的面板 + public static JPanel p0 = new JPanel(); // 放文本框的面板 public static JMenuBar menubar; - public static JMenu menu, subMenu1, subMenu2, menu2, menu3, subMenu3; + public static JMenu menu, subMenu1, subMenu2, menu2, menu3; public static String str1 = "", str2 = ""; double result, m, n, flag = 0; // flag指示变量,判断按"="前按的是哪个按钮 public static String[] anniu = { "e^x", "π", "e", "C", "DEL", @@ -38,27 +37,17 @@ public class Calculator extends JFrame implements ActionListener { public JButton qita[] = new JButton[gengduo.length]; public Calculator() { - mainframe = Test.returnFrame(); init(); - setVisible(true); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } - + // 版面设计 void init() { - - mainp.setLayout(new BorderLayout()); - + mainp.setLayout(new BorderLayout()); menubar = new JMenuBar(); menu = new JMenu("三角学"); menu2 = new JMenu("更多"); menu3 = new JMenu("计算时间"); subMenu1 = new JMenu("1nd"); subMenu2 = new JMenu("2nd"); - subMenu3 = new JMenu("另一界面"); - menu3.add(subMenu3); - // subMenu1.setAccelerator(KeyStroke.getKeyStroke('A')); //选择计算三角函数按快捷建A - // subMenu2.setAccelerator(KeyStroke.getKeyStroke('B'));//选择计算反三角函数按快捷键B menu.add(subMenu1); menu.addSeparator(); menu.add(subMenu2); @@ -81,12 +70,13 @@ public class Calculator extends JFrame implements ActionListener { qita[i] = new JButton(gengduo[i]); p3.add(qita[i]); } - // JPanel p4=new JPanel(); + JPanel p4=new JPanel(); + JButton huan=new JButton("另一界面"); + p4.add(huan);// subMenu1.add(p1); subMenu2.add(p2); menu2.add(p3); - // menu3.add(p4); - + menu3.add(p4); menubar.add(menu); menubar.add(menu2); menubar.add(menu3); @@ -106,14 +96,13 @@ public class Calculator extends JFrame implements ActionListener { p0.add(showText2, BorderLayout.CENTER); mainp.add(p0, BorderLayout.NORTH); mainp.add(p, BorderLayout.CENTER); - add(mainp); - showText1.setSize(300, 50); - showText2.setSize(20, 70); + showText1.setPreferredSize(new Dimension(420,50)); + showText2.setPreferredSize(new Dimension(420,70)); showText1.setEnabled(false); for (int i = 0; i < anniu.length; i++) { button[i] = new JButton(anniu[i]); - // button[i].setPreferredSize(new Dimension(20,20)); + button[i].setPreferredSize(new Dimension(10,10)); button[i].setFont(new Font("黑体", 1, 20)); p.add(button[i]); } @@ -131,18 +120,11 @@ public class Calculator extends JFrame implements ActionListener { showText1.setHorizontalAlignment(SwingConstants.RIGHT); showText2.setHorizontalAlignment(SwingConstants.RIGHT); - - subMenu3.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - mainframe.remove(mainp); - System.out.println("nihao "); - mainp = new PanelNullLayout().View(); - mainframe.add(mainp); - } - }); - - // 版面设计 - + add(mainp); + + //注册监听器 + + huan.addActionListener(this); for (int i = 0; i < button.length; i++) { // 每个按钮都注册监听器 button[i].addActionListener(this); } @@ -162,13 +144,28 @@ public class Calculator extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { // 实现相关功能 - try { - if (showText2.getText() == "") - throw new java.lang.NumberFormatException(); - } catch (java.lang.NumberFormatException ee) { - System.out.println("无效输入"); - } - + //什么都没有输入的情况下点击按钮,报错 + + if(str2==""&&e.getSource()!=button[1]||e.getSource()!=button[2]||e.getSource() !=button[16] || e.getSource() != button[17] || e.getSource() != button[18] + || e.getSource() != button[21] || e.getSource() != button[22] || e.getSource() != button[23] + || e.getSource() != button[26] || e.getSource() != button[27] || e.getSource() != button[28]||e.getSource() != button[31]||e.getSource() != button[32]||e.getSource() != button[33]) { + try { + throw new java.lang.NumberFormatException(); + } + + catch (java.lang.NumberFormatException ee) { + showText1.setText("无效输入!请重新输入!"); + } + } + //按下菜单中按钮转换界面计算时间 + if(e.getActionCommand()=="另一界面") { + mainp.removeAll(); + mainp.add(new PanelNullLayout()); + mainp.validate(); + mainp.repaint(); + + } + // 按下1-9 if (e.getSource() == button[16] || e.getSource() == button[17] || e.getSource() == button[18] || e.getSource() == button[21] || e.getSource() == button[22] || e.getSource() == button[23] @@ -184,10 +181,14 @@ public class Calculator extends JFrame implements ActionListener { } // 按下小数点 + if (e.getActionCommand().equals(".")) { + str2 += "."; showText2.setText(str2); - } + } + + // 按下 "(" if (e.getActionCommand().equals("(")) { str2 += "("; @@ -267,14 +268,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); flag = 0; } - // 按下"=" - if (e.getActionCommand().equals("=")) { - showText1.setText(showText2.getText() + button[34].getText()); - Scanner scanner = new Scanner(showText2.getText()); - str2 = saveNumber_Symbol(scanner.next()); // 分别存符号和数字 - showText2.setText(str2); - - } + // 开根号 if (e.getActionCommand().equals("√x")) { @@ -283,6 +277,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.sqrt(m); str2 = "" + result; showText2.setText(str2); + showText1.setText("√"+m+"="); } else { showText1.setText("数值大于零,才能开根号"); @@ -294,7 +289,7 @@ public class Calculator extends JFrame implements ActionListener { result = m * m; str2 = "" + result; showText2.setText(str2); - + showText1.setText(m+"^2"+"="); } // 倒数 if (e.getActionCommand().equals("1/x")) { @@ -305,6 +300,7 @@ public class Calculator extends JFrame implements ActionListener { result = 1 / m; str2 = "" + result; showText2.setText(str2); + showText1.setText("1/"+m+"="); } else { showText1.setText("除数不能为0!"); @@ -316,9 +312,10 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(2.718281828459, m); str2 = result + ""; showText2.setText(str2); + showText1.setText("e^"+m+"="); } - - // "+/-"号 ?? + + // "+/-"号 if (e.getActionCommand().equals("+/-")) { str2 = "-" + str2; showText2.setText(str2); @@ -341,21 +338,24 @@ public class Calculator extends JFrame implements ActionListener { result = Math.abs(m); str2 = result + ""; showText2.setText(str2); - + showText1.setText("|"+m+"|"+"="); } // "n!" if (e.getActionCommand().equals("n!")) { m = Double.parseDouble(str2); if (m % 1 != 0) { showText1.setText("error!请输入整数"); - } else if (m >= 0) { // 0的阶乘为1 + } + else if (m >= 0) { // 0的阶乘为1 result = 1; for (int i = 1; i <= m; i++) { result = result * i; } - } + str2 = result + ""; showText2.setText(str2); + showText1.setText("fact"+"("+m+")"+"="); + } } // "10^x" @@ -364,6 +364,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(10, m); str2 = result + ""; showText2.setText(str2); + showText1.setText("10^"+m+"="); } // "lg" if (e.getActionCommand().equals("lg")) { @@ -372,7 +373,9 @@ public class Calculator extends JFrame implements ActionListener { result = Math.log10(m); str2 = result + ""; showText2.setText(str2); - } else { + showText1.setText("lg"+m+"="); + } + else { showText1.setText("请输入大于0的数!"); } } @@ -383,7 +386,9 @@ public class Calculator extends JFrame implements ActionListener { result = Math.log(m); str2 = "" + Integer.parseInt(new java.text.DecimalFormat("0").format(result)); // 四舍五入 showText2.setText(str2); - } else { + showText1.setText("ln"+m+"="); + } + else { showText1.setText("请输入大于0的数!"); } @@ -394,12 +399,14 @@ public class Calculator extends JFrame implements ActionListener { result = m * m * m; str2 = result + ""; showText2.setText(str2); + showText1.setText("x^"+m+"="); } if (e.getActionCommand().equals("3√x")) { m = Double.parseDouble(str2); result = Math.cbrt(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("3√"+m+"="); } if (e.getActionCommand().equals("y√x")) { str1 = str2; @@ -419,6 +426,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(2, m); str2 = result + ""; showText2.setText(str2); + showText1.setText("2^"+m+"="); } if (e.getActionCommand().equals("logyx")) { flag = 5; @@ -443,36 +451,42 @@ public class Calculator extends JFrame implements ActionListener { result = Math.sin(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("sin"+m+"="); } if (e.getActionCommand().equals("cos")) { m = Double.parseDouble(str2); result = Math.cos(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("cos"+m+"="); } if (e.getActionCommand().equals("tan")) { m = Double.parseDouble(str2); result = Math.tan(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("tan"+m+"="); } if (e.getActionCommand().equals("sec")) { m = Double.parseDouble(str2); result = 1 / Math.cos(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("sec"+m+"="); } if (e.getActionCommand().equals("csc")) { m = Double.parseDouble(str2); result = 1 / Math.cos(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("csc"+m+"="); } if (e.getActionCommand().equals("cot")) { m = Double.parseDouble(str2); result = 1 / Math.tan(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("cot"+m+"="); } // 三角学"2nd"即已知值求角度的计算 if (e.getActionCommand().equals("sin^-1")) { @@ -480,18 +494,21 @@ public class Calculator extends JFrame implements ActionListener { result = Math.asin(m); str2 = result + ""; showText2.setText(str2); + } if (e.getActionCommand().equals("cos^-1")) { m = Double.parseDouble(str2); result = Math.acos(m); str2 = result + ""; showText2.setText(str2); + } if (e.getActionCommand().equals("tan^-1")) { m = Double.parseDouble(str2); result = Math.atan(m); str2 = result + ""; showText2.setText(str2); + } if (e.getActionCommand().equals("根据角度求弧度")) { m = Double.parseDouble(str2); @@ -499,37 +516,50 @@ public class Calculator extends JFrame implements ActionListener { str2 = result + ""; showText2.setText(str2); } + // 按下"=" + if (e.getActionCommand().equals("=")) { + + showText1.setText(showText2.getText() + button[34].getText()); + Scanner scanner = new Scanner(showText2.getText()); + str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字 + showText2.setText(str2); + } } + + - /* 分割字符串存储运算符和数字分别到calcuStrList和numberList */ - public String saveNumber_Symbol(String str) { + // 分割字符串,把运算符和数字分别放到calcuStrList和numberList里 + public String saveNumberAndCalculator(String str) { + // 若第一个数字是负数,在"-"前加0 if (str.startsWith("-")) { StringBuffer s1 = new StringBuffer(str); s1.insert(0, "0"); str = s1.toString(); - } // 若第一个数字是负数,在"-"前加0 - - str = str + "#"; // 加上"#"用于获取分割最后一个数字 + } + // 加上"#"用于获取分割最后一个数字 + str = str + "#"; char[] strChar = str.toCharArray(); - LinkedList calcuStrList = new LinkedList<>();// 存储运算符号 - LinkedList numberList = new LinkedList<>();// 存储数字 - StringBuffer buff = new StringBuffer(); + LinkedList calcuStrList = new LinkedList<>(); // 存储运算符号 + LinkedList numberList = new LinkedList<>(); // 存储数字 + StringBuffer buffer = new StringBuffer(); for (char fuhao : strChar) { if (fuhao == '+' || fuhao == '-' || fuhao == 'x' || fuhao == '÷') { calcuStrList.add(String.valueOf(fuhao)); - numberList.add(buff.toString()); - buff.delete(0, buff.length()); - } else if (fuhao == '#') {// 判断是不是最后一个数 - numberList.add(buff.toString()); + numberList.add(buffer.toString()); + buffer.delete(0, buffer.length()); + } + // 判断是不是最后一个数,是的话加到数字列表里 + else if (fuhao == '#') { + numberList.add(buffer.toString()); } else { - buff.append(fuhao); + buffer.append(fuhao); } } return calculationOrder(calcuStrList, numberList); } - /* 控制加减乘除的运算优先级顺序 */ + // 控制加减乘除的运算优先级顺序 public String calculationOrder(LinkedList calcuStr, LinkedList number) { if (calcuStr.size() > 0) { int multiply = calcuStr.indexOf("x"); @@ -550,16 +580,16 @@ public class Calculator extends JFrame implements ActionListener { calcuResult(calcuStr.get(0), calcuStr, number); } } - return number.get(0); + return number.get(0); //返回最终数链表中第一个位置的数据,即经过一次计算的结果 } - /* 通过symbol找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 */ - public String calcuResult(String symbol, LinkedList calcuStr, LinkedList number) { - int position = calcuStr.indexOf(symbol); + // 通过location找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 + public String calcuResult(String location, LinkedList calcuStr, LinkedList number) { + int position = calcuStr.indexOf(location); double a = Double.parseDouble(number.get(position)); double b = Double.parseDouble(number.get(position + 1)); String calculateNumber = ""; - switch (symbol) { + switch (location) { case "+": calculateNumber = String.valueOf(a + b); break; @@ -573,10 +603,11 @@ public class Calculator extends JFrame implements ActionListener { calculateNumber = String.valueOf(a / b); break; } - number.set(position, calculateNumber);// 两个数运算完后将计算结果覆盖到第一个数的位置 - calcuStr.remove(position);// 每用完一个运算符就删除该运算符 - number.remove(position + 1);// 两个数运算完后删除后面的数 - return calculationOrder(calcuStr, number); + number.set(position, calculateNumber);// 两个数算完后把结果给前一个数 + calcuStr.remove(position);// 在cacuStr集中每算完一个符号就删一个符号 + number.remove(position + 1);// 在number集中两个数运算完后删除后面的数 + return calculationOrder(calcuStr, number); //一直算直到calcu集中没有符号为止 } + } diff --git a/src/java2020spring/PanelNullLayout.java b/src/java2020spring/PanelNullLayout.java index 59ffa27..b3c6f98 100644 --- a/src/java2020spring/PanelNullLayout.java +++ b/src/java2020spring/PanelNullLayout.java @@ -3,30 +3,111 @@ package java2020spring; import javax.swing.*; import java.awt.*; import java.awt.event.*; +import java.util.*; -public class PanelNullLayout extends JPanel implements ActionListener { - JTextField t1 = new JTextField(); - static JPanel p; +public class PanelNullLayout extends JPanel implements ActionListener,ItemListener { + Calendar calendar; + static String year,month,day; //所选起始时间 + static int yearNow,monthNow,dayNow; //现在时间 + static long time1,time2; + JTextArea t1 ; //显示区 + JLabel a,b,c,d,e; + JComboBox choiceYear,choiceMonth,choiceDay; + JButton action ;//计算按钮 + //版面设计 PanelNullLayout() { + setLayout(null); + a=new JLabel("与今天相隔时间:"); + b=new JLabel("起始时间:"); + c=new JLabel("年"); + d=new JLabel("月"); + e=new JLabel("日"); + t1=new JTextArea(9,30); + action=new JButton("计算"); + + calendar=Calendar.getInstance(); + calendar.setTime(new Date()); + yearNow=calendar.get(Calendar.YEAR); + monthNow=calendar.get(Calendar.MONTH)+1; + dayNow=calendar.get(Calendar.DAY_OF_MONTH); + t1.setText("现在的时间是:"+yearNow+"年"+monthNow+"月"+dayNow+"日"); + time1=calendar.getTimeInMillis(); + add(a); + add(b); + add(c); + add(d); + add(e); + add(t1); + add(action); + + a.setBounds(40,30,120,30); + b.setBounds(100,50,100,40); + c.setBounds(230, 50,20,40); + d.setBounds(300,50,20,40); + e.setBounds(370, 50, 20, 40); + t1.setBounds(100,100,200,300); + action.setBounds(320,100,60,40); + + choiceYear=new JComboBox(); + choiceMonth=new JComboBox(); + choiceDay=new JComboBox(); + choiceYear.addItem("0"); + choiceMonth.addItem("0"); + choiceDay.addItem("0"); + for(int i=1601;i<=2601;i++) { + choiceYear.addItem(i); + } + for(int i=1;i<=12;i++) { + choiceMonth.addItem(i); + } + for(int i=1;i<=31;i++) { + choiceDay.addItem(i); + } + add(choiceYear); + add(choiceMonth); + add(choiceDay); + choiceYear.setBounds(160,50,60,40); + choiceMonth.setBounds(250,50,40,40); + choiceDay.setBounds(320,50,40,40); + + //加监听器 + choiceYear.addItemListener(this); + choiceMonth.addItemListener(this); + choiceDay.addItemListener(this); + action.addActionListener(this); + + } - } - - public static JPanel View() { - JFrame frame = Test.returnFrame(); - JButton b = new JButton("确定"); - p = new JPanel(); - p.add(b); - frame.add(p); - p.setLayout(null); - p.setBounds(100, 100, 420, 600); - System.out.println("nihao"); - - return p; - - } + public void actionPerformed(ActionEvent e) { - + try { + calendar.set(Integer.parseInt(year),Integer.parseInt(month)-1,Integer.parseInt(day)); + time2=calendar.getTimeInMillis(); + if(time1>=time2) { + long subDay=(time1-time2)/(1000*60*60*24); + t1.append("\n"+"与现在相隔"+subDay+"天"); + } + else { + long subDay=(time2-time1)/(1000*60*60*24); + t1.append("\n"+"与现在相隔"+subDay+"天"); + } + + } + catch(Exception exp) { + t1.append("\n错误!请选择起始时间!"); + } + + + + } + + + public void itemStateChanged(ItemEvent e) { + year=choiceYear.getSelectedItem().toString(); + month=choiceMonth.getSelectedItem().toString(); + day=choiceDay.getSelectedItem().toString(); + } } diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java index 4d3cffd..b12860f 100644 --- a/src/java2020spring/Test.java +++ b/src/java2020spring/Test.java @@ -8,12 +8,11 @@ public class Test { public static void main(String[] args) { win = new Calculator(); win.setTitle("ljl's calculator"); - win.setBounds(100, 100, 420, 600); + win.setBounds(100, 100, 450, 600); win.setVisible(true); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } - public static JFrame returnFrame() { - return win; - } + + } -- Gitee From 9583d026460c1cee583b957b08a297fd692bee26 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Wed, 9 Jun 2021 20:48:11 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E6=94=B9=E8=BF=9B=E4=BA=86=E5=9C=A8=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=95=B0=E5=AD=97=E6=83=85=E5=86=B5=E4=B8=8B=E6=8C=89?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E7=9A=84=E5=AE=B9=E9=94=99=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E8=B0=83=E6=95=B4=E4=BA=86=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 167 +++++++++++---------- src/java2020spring/PanelNullLayout.java | 185 ++++++++++++------------ src/java2020spring/Test.java | 2 - 3 files changed, 171 insertions(+), 183 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 7d3d3b2..b10a54b 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -8,7 +8,7 @@ import java.util.Scanner; public class Calculator extends JFrame implements ActionListener { public static JTextField showText1, showText2; // 1为显示框,2为输入框 - public static JPanel mainp = new JPanel(); //mainp为主面板 + public static JPanel mainp = new JPanel(); // mainp为主面板 public static JPanel p = new JPanel(); // 放按钮的面板 public static JPanel p0 = new JPanel(); // 放文本框的面板 public static JMenuBar menubar; @@ -39,9 +39,10 @@ public class Calculator extends JFrame implements ActionListener { public Calculator() { init(); } + // 版面设计 void init() { - mainp.setLayout(new BorderLayout()); + mainp.setLayout(new BorderLayout()); menubar = new JMenuBar(); menu = new JMenu("三角学"); menu2 = new JMenu("更多"); @@ -70,8 +71,8 @@ public class Calculator extends JFrame implements ActionListener { qita[i] = new JButton(gengduo[i]); p3.add(qita[i]); } - JPanel p4=new JPanel(); - JButton huan=new JButton("另一界面"); + JPanel p4 = new JPanel(); + JButton huan = new JButton("另一界面"); p4.add(huan);// subMenu1.add(p1); subMenu2.add(p2); @@ -96,13 +97,13 @@ public class Calculator extends JFrame implements ActionListener { p0.add(showText2, BorderLayout.CENTER); mainp.add(p0, BorderLayout.NORTH); mainp.add(p, BorderLayout.CENTER); - showText1.setPreferredSize(new Dimension(420,50)); - showText2.setPreferredSize(new Dimension(420,70)); + showText1.setPreferredSize(new Dimension(420, 50)); + showText2.setPreferredSize(new Dimension(420, 70)); showText1.setEnabled(false); for (int i = 0; i < anniu.length; i++) { button[i] = new JButton(anniu[i]); - button[i].setPreferredSize(new Dimension(10,10)); + button[i].setPreferredSize(new Dimension(10, 10)); button[i].setFont(new Font("黑体", 1, 20)); p.add(button[i]); } @@ -121,9 +122,9 @@ public class Calculator extends JFrame implements ActionListener { showText1.setHorizontalAlignment(SwingConstants.RIGHT); showText2.setHorizontalAlignment(SwingConstants.RIGHT); add(mainp); - - //注册监听器 - + + // 注册监听器 + huan.addActionListener(this); for (int i = 0; i < button.length; i++) { // 每个按钮都注册监听器 button[i].addActionListener(this); @@ -144,28 +145,25 @@ public class Calculator extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { // 实现相关功能 - //什么都没有输入的情况下点击按钮,报错 - - if(str2==""&&e.getSource()!=button[1]||e.getSource()!=button[2]||e.getSource() !=button[16] || e.getSource() != button[17] || e.getSource() != button[18] - || e.getSource() != button[21] || e.getSource() != button[22] || e.getSource() != button[23] - || e.getSource() != button[26] || e.getSource() != button[27] || e.getSource() != button[28]||e.getSource() != button[31]||e.getSource() != button[32]||e.getSource() != button[33]) { - try { - throw new java.lang.NumberFormatException(); + // 什么都没有输入的情况下点击按钮,报错提示重新输入 + if (str2 == "") { + try { + throw new java.lang.NumberFormatException(); + } + + catch (java.lang.NumberFormatException ee) { + showText1.setText("无效输入!请重新输入!"); } - - catch (java.lang.NumberFormatException ee) { - showText1.setText("无效输入!请重新输入!"); - } - } - //按下菜单中按钮转换界面计算时间 - if(e.getActionCommand()=="另一界面") { - mainp.removeAll(); - mainp.add(new PanelNullLayout()); - mainp.validate(); - mainp.repaint(); - - } - + } + // 按下菜单中按钮转换界面计算时间 + if (e.getActionCommand() == "另一界面") { + mainp.removeAll(); + mainp.add(new PanelNullLayout()); + mainp.validate(); + mainp.repaint(); + + } + // 按下1-9 if (e.getSource() == button[16] || e.getSource() == button[17] || e.getSource() == button[18] || e.getSource() == button[21] || e.getSource() == button[22] || e.getSource() == button[23] @@ -178,26 +176,29 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("0")) { str2 += e.getActionCommand(); showText2.setText(str2); + showText1.setText(str2); } // 按下小数点 - + if (e.getActionCommand().equals(".")) { - + str2 += "."; showText2.setText(str2); - } - - + showText1.setText(str2); + } + // 按下 "(" if (e.getActionCommand().equals("(")) { str2 += "("; showText2.setText(str2); + showText1.setText(str2); } // 按下")" if (e.getActionCommand().equals(")")) { str2 += ")"; showText2.setText(str2); + showText1.setText(str2); } // 按下DEL退一格 if (e.getActionCommand().equals("DEL")) { @@ -224,7 +225,7 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("+") || e.getActionCommand().equals("-") || e.getActionCommand().equals("x") || e.getActionCommand().equals("÷")) { str2 = str2 + e.getActionCommand(); - showText1.setText(str2); + showText2.setText(str2); } // "x^y" @@ -234,10 +235,11 @@ public class Calculator extends JFrame implements ActionListener { flag = 1; } + if (e.getActionCommand().equals("=") && flag == 1) { m = Double.parseDouble(str2); n = Double.parseDouble(str1); - result = Math.pow(m, n); + result = Math.pow(n, m); showText2.setText(result + ""); flag = 0; } @@ -268,7 +270,6 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); flag = 0; } - // 开根号 if (e.getActionCommand().equals("√x")) { @@ -277,7 +278,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.sqrt(m); str2 = "" + result; showText2.setText(str2); - showText1.setText("√"+m+"="); + showText1.setText("√" + m + "="); } else { showText1.setText("数值大于零,才能开根号"); @@ -289,7 +290,7 @@ public class Calculator extends JFrame implements ActionListener { result = m * m; str2 = "" + result; showText2.setText(str2); - showText1.setText(m+"^2"+"="); + showText1.setText(m + "^2" + "="); } // 倒数 if (e.getActionCommand().equals("1/x")) { @@ -300,7 +301,7 @@ public class Calculator extends JFrame implements ActionListener { result = 1 / m; str2 = "" + result; showText2.setText(str2); - showText1.setText("1/"+m+"="); + showText1.setText("1/" + m + "="); } else { showText1.setText("除数不能为0!"); @@ -312,49 +313,50 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(2.718281828459, m); str2 = result + ""; showText2.setText(str2); - showText1.setText("e^"+m+"="); + showText1.setText("e^" + m + "="); } - - // "+/-"号 + + // "+/-"号 if (e.getActionCommand().equals("+/-")) { str2 = "-" + str2; showText2.setText(str2); + showText1.setText(str2); } // "π" if (e.getActionCommand().equals("π")) { str2 += "3.1415926535898"; showText2.setText(str2); + showText1.setText(str2); } // "e" if (e.getActionCommand().equals("e")) { str2 += "2.718281828459"; showText2.setText(str2); + showText1.setText(str2); } // "|x|" if (e.getActionCommand().equals("|x|")) { - m = Double.parseDouble(str2); result = Math.abs(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("|"+m+"|"+"="); + showText1.setText("|" + m + "|" + "="); } // "n!" if (e.getActionCommand().equals("n!")) { m = Double.parseDouble(str2); if (m % 1 != 0) { showText1.setText("error!请输入整数"); - } - else if (m >= 0) { // 0的阶乘为1 + } else if (m >= 0) { // 0的阶乘为1 result = 1; for (int i = 1; i <= m; i++) { result = result * i; } - - str2 = result + ""; - showText2.setText(str2); - showText1.setText("fact"+"("+m+")"+"="); + + str2 = result + ""; + showText2.setText(str2); + showText1.setText("fact" + "(" + m + ")" + "="); } } @@ -364,7 +366,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(10, m); str2 = result + ""; showText2.setText(str2); - showText1.setText("10^"+m+"="); + showText1.setText("10^" + m + "="); } // "lg" if (e.getActionCommand().equals("lg")) { @@ -373,9 +375,8 @@ public class Calculator extends JFrame implements ActionListener { result = Math.log10(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("lg"+m+"="); - } - else { + showText1.setText("lg" + m + "="); + } else { showText1.setText("请输入大于0的数!"); } } @@ -386,9 +387,8 @@ public class Calculator extends JFrame implements ActionListener { result = Math.log(m); str2 = "" + Integer.parseInt(new java.text.DecimalFormat("0").format(result)); // 四舍五入 showText2.setText(str2); - showText1.setText("ln"+m+"="); - } - else { + showText1.setText("ln" + m + "="); + } else { showText1.setText("请输入大于0的数!"); } @@ -399,14 +399,14 @@ public class Calculator extends JFrame implements ActionListener { result = m * m * m; str2 = result + ""; showText2.setText(str2); - showText1.setText("x^"+m+"="); + showText1.setText(m + "^3" + "="); } if (e.getActionCommand().equals("3√x")) { m = Double.parseDouble(str2); result = Math.cbrt(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("3√"+m+"="); + showText1.setText("3√" + m + "="); } if (e.getActionCommand().equals("y√x")) { str1 = str2; @@ -426,7 +426,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(2, m); str2 = result + ""; showText2.setText(str2); - showText1.setText("2^"+m+"="); + showText1.setText("2^" + m + "="); } if (e.getActionCommand().equals("logyx")) { flag = 5; @@ -451,42 +451,42 @@ public class Calculator extends JFrame implements ActionListener { result = Math.sin(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("sin"+m+"="); + showText1.setText("sin" + m + "="); } if (e.getActionCommand().equals("cos")) { m = Double.parseDouble(str2); result = Math.cos(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("cos"+m+"="); + showText1.setText("cos" + m + "="); } if (e.getActionCommand().equals("tan")) { m = Double.parseDouble(str2); result = Math.tan(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("tan"+m+"="); + showText1.setText("tan" + m + "="); } if (e.getActionCommand().equals("sec")) { m = Double.parseDouble(str2); result = 1 / Math.cos(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("sec"+m+"="); + showText1.setText("sec" + m + "="); } if (e.getActionCommand().equals("csc")) { m = Double.parseDouble(str2); result = 1 / Math.cos(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("csc"+m+"="); + showText1.setText("csc" + m + "="); } if (e.getActionCommand().equals("cot")) { m = Double.parseDouble(str2); result = 1 / Math.tan(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("cot"+m+"="); + showText1.setText("cot" + m + "="); } // 三角学"2nd"即已知值求角度的计算 if (e.getActionCommand().equals("sin^-1")) { @@ -494,21 +494,21 @@ public class Calculator extends JFrame implements ActionListener { result = Math.asin(m); str2 = result + ""; showText2.setText(str2); - + } if (e.getActionCommand().equals("cos^-1")) { m = Double.parseDouble(str2); result = Math.acos(m); str2 = result + ""; showText2.setText(str2); - + } if (e.getActionCommand().equals("tan^-1")) { m = Double.parseDouble(str2); result = Math.atan(m); str2 = result + ""; showText2.setText(str2); - + } if (e.getActionCommand().equals("根据角度求弧度")) { m = Double.parseDouble(str2); @@ -518,7 +518,7 @@ public class Calculator extends JFrame implements ActionListener { } // 按下"=" if (e.getActionCommand().equals("=")) { - + showText1.setText(showText2.getText() + button[34].getText()); Scanner scanner = new Scanner(showText2.getText()); str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字 @@ -526,8 +526,6 @@ public class Calculator extends JFrame implements ActionListener { } } - - // 分割字符串,把运算符和数字分别放到calcuStrList和numberList里 public String saveNumberAndCalculator(String str) { @@ -536,11 +534,11 @@ public class Calculator extends JFrame implements ActionListener { StringBuffer s1 = new StringBuffer(str); s1.insert(0, "0"); str = s1.toString(); - } + } // 加上"#"用于获取分割最后一个数字 - str = str + "#"; + str = str + "#"; char[] strChar = str.toCharArray(); - LinkedList calcuStrList = new LinkedList<>(); // 存储运算符号 + LinkedList calcuStrList = new LinkedList<>(); // 存储运算符号 LinkedList numberList = new LinkedList<>(); // 存储数字 StringBuffer buffer = new StringBuffer(); for (char fuhao : strChar) { @@ -548,9 +546,9 @@ public class Calculator extends JFrame implements ActionListener { calcuStrList.add(String.valueOf(fuhao)); numberList.add(buffer.toString()); buffer.delete(0, buffer.length()); - } + } // 判断是不是最后一个数,是的话加到数字列表里 - else if (fuhao == '#') { + else if (fuhao == '#') { numberList.add(buffer.toString()); } else { buffer.append(fuhao); @@ -559,7 +557,7 @@ public class Calculator extends JFrame implements ActionListener { return calculationOrder(calcuStrList, numberList); } - // 控制加减乘除的运算优先级顺序 + // 控制加减乘除的运算优先级顺序 public String calculationOrder(LinkedList calcuStr, LinkedList number) { if (calcuStr.size() > 0) { int multiply = calcuStr.indexOf("x"); @@ -580,10 +578,10 @@ public class Calculator extends JFrame implements ActionListener { calcuResult(calcuStr.get(0), calcuStr, number); } } - return number.get(0); //返回最终数链表中第一个位置的数据,即经过一次计算的结果 + return number.get(0); // 返回最终数链表中第一个位置的数据,即经过一次计算的结果 } - // 通过location找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 + // 通过location找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 public String calcuResult(String location, LinkedList calcuStr, LinkedList number) { int position = calcuStr.indexOf(location); double a = Double.parseDouble(number.get(position)); @@ -606,8 +604,7 @@ public class Calculator extends JFrame implements ActionListener { number.set(position, calculateNumber);// 两个数算完后把结果给前一个数 calcuStr.remove(position);// 在cacuStr集中每算完一个符号就删一个符号 number.remove(position + 1);// 在number集中两个数运算完后删除后面的数 - return calculationOrder(calcuStr, number); //一直算直到calcu集中没有符号为止 + return calculationOrder(calcuStr, number); // 一直算直到calcu集中没有符号为止 } - } diff --git a/src/java2020spring/PanelNullLayout.java b/src/java2020spring/PanelNullLayout.java index b3c6f98..36aaf51 100644 --- a/src/java2020spring/PanelNullLayout.java +++ b/src/java2020spring/PanelNullLayout.java @@ -5,109 +5,102 @@ import java.awt.*; import java.awt.event.*; import java.util.*; -public class PanelNullLayout extends JPanel implements ActionListener,ItemListener { +public class PanelNullLayout extends JPanel implements ActionListener, ItemListener { Calendar calendar; - static String year,month,day; //所选起始时间 - static int yearNow,monthNow,dayNow; //现在时间 - static long time1,time2; - JTextArea t1 ; //显示区 - JLabel a,b,c,d,e; - JComboBox choiceYear,choiceMonth,choiceDay; - JButton action ;//计算按钮 + static String year, month, day; // 所选起始时间 + static int yearNow, monthNow, dayNow; // 现在时间 + static long time1, time2; + JTextArea t1; // 显示区 + JLabel a, b, c, d, e; + JComboBox choiceYear, choiceMonth, choiceDay; + JButton action;// 计算按钮 - //版面设计 + // 版面设计 PanelNullLayout() { setLayout(null); - a=new JLabel("与今天相隔时间:"); - b=new JLabel("起始时间:"); - c=new JLabel("年"); - d=new JLabel("月"); - e=new JLabel("日"); - t1=new JTextArea(9,30); - action=new JButton("计算"); - - calendar=Calendar.getInstance(); - calendar.setTime(new Date()); - yearNow=calendar.get(Calendar.YEAR); - monthNow=calendar.get(Calendar.MONTH)+1; - dayNow=calendar.get(Calendar.DAY_OF_MONTH); - t1.setText("现在的时间是:"+yearNow+"年"+monthNow+"月"+dayNow+"日"); - time1=calendar.getTimeInMillis(); - add(a); - add(b); - add(c); - add(d); - add(e); - add(t1); - add(action); - - a.setBounds(40,30,120,30); - b.setBounds(100,50,100,40); - c.setBounds(230, 50,20,40); - d.setBounds(300,50,20,40); - e.setBounds(370, 50, 20, 40); - t1.setBounds(100,100,200,300); - action.setBounds(320,100,60,40); - - choiceYear=new JComboBox(); - choiceMonth=new JComboBox(); - choiceDay=new JComboBox(); - choiceYear.addItem("0"); - choiceMonth.addItem("0"); - choiceDay.addItem("0"); - for(int i=1601;i<=2601;i++) { - choiceYear.addItem(i); - } - for(int i=1;i<=12;i++) { - choiceMonth.addItem(i); - } - for(int i=1;i<=31;i++) { - choiceDay.addItem(i); - } - add(choiceYear); - add(choiceMonth); - add(choiceDay); - choiceYear.setBounds(160,50,60,40); - choiceMonth.setBounds(250,50,40,40); - choiceDay.setBounds(320,50,40,40); - - //加监听器 - choiceYear.addItemListener(this); - choiceMonth.addItemListener(this); - choiceDay.addItemListener(this); - action.addActionListener(this); - - } + a = new JLabel("与今天相隔时间:"); + b = new JLabel("起始时间:"); + c = new JLabel("年"); + d = new JLabel("月"); + e = new JLabel("日"); + t1 = new JTextArea(9, 30); + action = new JButton("计算"); - + calendar = Calendar.getInstance(); + calendar.setTime(new Date()); + yearNow = calendar.get(Calendar.YEAR); + monthNow = calendar.get(Calendar.MONTH) + 1; + dayNow = calendar.get(Calendar.DAY_OF_MONTH); + t1.setText("现在的时间是:" + yearNow + "年" + monthNow + "月" + dayNow + "日"); + time1 = calendar.getTimeInMillis(); + add(a); + add(b); + add(c); + add(d); + add(e); + add(t1); + add(action); + + a.setBounds(40, 30, 120, 30); + b.setBounds(100, 50, 100, 40); + c.setBounds(230, 50, 20, 40); + d.setBounds(300, 50, 20, 40); + e.setBounds(370, 50, 20, 40); + t1.setBounds(100, 100, 200, 300); + action.setBounds(320, 100, 60, 40); + + choiceYear = new JComboBox(); + choiceMonth = new JComboBox(); + choiceDay = new JComboBox(); + choiceYear.addItem("0"); + choiceMonth.addItem("0"); + choiceDay.addItem("0"); + for (int i = 1601; i <= 2601; i++) { + choiceYear.addItem(i); + } + for (int i = 1; i <= 12; i++) { + choiceMonth.addItem(i); + } + for (int i = 1; i <= 31; i++) { + choiceDay.addItem(i); + } + add(choiceYear); + add(choiceMonth); + add(choiceDay); + choiceYear.setBounds(160, 50, 60, 40); + choiceMonth.setBounds(250, 50, 40, 40); + choiceDay.setBounds(320, 50, 40, 40); + + // 加监听器 + choiceYear.addItemListener(this); + choiceMonth.addItemListener(this); + choiceDay.addItemListener(this); + action.addActionListener(this); + + } public void actionPerformed(ActionEvent e) { - try { - calendar.set(Integer.parseInt(year),Integer.parseInt(month)-1,Integer.parseInt(day)); - time2=calendar.getTimeInMillis(); - if(time1>=time2) { - long subDay=(time1-time2)/(1000*60*60*24); - t1.append("\n"+"与现在相隔"+subDay+"天"); - } - else { - long subDay=(time2-time1)/(1000*60*60*24); - t1.append("\n"+"与现在相隔"+subDay+"天"); - } - - } - catch(Exception exp) { - t1.append("\n错误!请选择起始时间!"); - } - - - - } - - + try { + calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day)); + time2 = calendar.getTimeInMillis(); + if (time1 >= time2) { + long subDay = (time1 - time2) / (1000 * 60 * 60 * 24); + t1.append("\n" + "与现在相隔" + subDay + "天"); + } else { + long subDay = (time2 - time1) / (1000 * 60 * 60 * 24); + t1.append("\n" + "与现在相隔" + subDay + "天"); + } + + } catch (Exception exp) { + t1.append("\n错误!请选择起始时间!"); + } + + } + public void itemStateChanged(ItemEvent e) { - year=choiceYear.getSelectedItem().toString(); - month=choiceMonth.getSelectedItem().toString(); - day=choiceDay.getSelectedItem().toString(); - + year = choiceYear.getSelectedItem().toString(); + month = choiceMonth.getSelectedItem().toString(); + day = choiceDay.getSelectedItem().toString(); + } } diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java index b12860f..217cc48 100644 --- a/src/java2020spring/Test.java +++ b/src/java2020spring/Test.java @@ -13,6 +13,4 @@ public class Test { win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } - - } -- Gitee From cffc657f66a48156c67bf58de12b000725e9bfd1 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Wed, 9 Jun 2021 22:25:27 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=99=A4=E6=95=B0?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E4=B8=BA0=E7=9A=84=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 80 ++++++++++++++++++------------ 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index b10a54b..26a5180 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -14,7 +14,7 @@ public class Calculator extends JFrame implements ActionListener { public static JMenuBar menubar; public static JMenu menu, subMenu1, subMenu2, menu2, menu3; public static String str1 = "", str2 = ""; - double result, m, n, flag = 0; // flag指示变量,判断按"="前按的是哪个按钮 + double result, m, n, flag = 0; // flag指示变量,判断按"="前按的是哪个按键 public static String[] anniu = { "e^x", "π", "e", "C", "DEL", "X^2", "1/x", "|x|", "exp", "mod", @@ -192,13 +192,13 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("(")) { str2 += "("; showText2.setText(str2); - showText1.setText(str2); + showText1.setText("抱歉,尚不可用"); } // 按下")" if (e.getActionCommand().equals(")")) { str2 += ")"; showText2.setText(str2); - showText1.setText(str2); + showText1.setText("抱歉,尚不可用"); } // 按下DEL退一格 if (e.getActionCommand().equals("DEL")) { @@ -271,7 +271,7 @@ public class Calculator extends JFrame implements ActionListener { flag = 0; } - // 开根号 + // "√x"开根号 if (e.getActionCommand().equals("√x")) { m = Double.parseDouble(str2); if (m >= 0) { @@ -284,7 +284,7 @@ public class Calculator extends JFrame implements ActionListener { showText1.setText("数值大于零,才能开根号"); } } - // 平方 + //"X^2" 平方 if (e.getActionCommand().equals("X^2")) { m = Double.parseDouble(str2); result = m * m; @@ -292,9 +292,9 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText(m + "^2" + "="); } - // 倒数 + // "1/x"倒数 if (e.getActionCommand().equals("1/x")) { - if (str2 == "") { + if (str2 =="0") { showText1.setText("除数不能为0!"); } else if (Double.parseDouble(str2) != 0) { m = Double.parseDouble(str2); @@ -393,7 +393,8 @@ public class Calculator extends JFrame implements ActionListener { } } - // 菜单“更多”里 + // 菜单“更多”里的函数 + //"x^3" if (e.getActionCommand().equals("x^3")) { m = Double.parseDouble(str2); result = m * m * m; @@ -401,6 +402,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText(m + "^3" + "="); } + //"3√x" if (e.getActionCommand().equals("3√x")) { m = Double.parseDouble(str2); result = Math.cbrt(m); @@ -408,11 +410,13 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText("3√" + m + "="); } + //"y√x" if (e.getActionCommand().equals("y√x")) { str1 = str2; str2 = ""; flag = 4; } + if (e.getActionCommand().equals("=") && flag == 4) { m = Double.parseDouble(str1); n = Double.parseDouble(str2); @@ -421,6 +425,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); flag = 0; } + //"2^x" if (e.getActionCommand().equals("2^x")) { m = Double.parseDouble(str2); result = Math.pow(2, m); @@ -428,6 +433,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText("2^" + m + "="); } + //"logyx" if (e.getActionCommand().equals("logyx")) { flag = 5; str1 = str2; @@ -436,12 +442,14 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("=") && flag == 5) { m = Double.parseDouble(str1); n = Double.parseDouble(str2); - if (n != 1 && m > 0 && n > 0) { + if (n != 1 && m > 0 && n > 0) //保证真数和分母都大于0 + { result = Math.log(m) / Math.log(n); str2 = result + ""; showText2.setText(str2); flag = 0; - } else { + } + else { showText1.setText("真数需都大于0,且分母不能为0"); } } @@ -520,8 +528,8 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("=")) { showText1.setText(showText2.getText() + button[34].getText()); - Scanner scanner = new Scanner(showText2.getText()); - str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字 + Scanner scanner = new Scanner(showText2.getText()); //用Scanner分割输入框输入的字符,得到一个个字符 + str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字直到没有字符 showText2.setText(str2); } @@ -538,12 +546,12 @@ public class Calculator extends JFrame implements ActionListener { // 加上"#"用于获取分割最后一个数字 str = str + "#"; char[] strChar = str.toCharArray(); - LinkedList calcuStrList = new LinkedList<>(); // 存储运算符号 + LinkedList fuhaoList = new LinkedList<>(); // 存储运算符号 LinkedList numberList = new LinkedList<>(); // 存储数字 - StringBuffer buffer = new StringBuffer(); + StringBuffer buffer = new StringBuffer(); //中间存放数字的buffer for (char fuhao : strChar) { if (fuhao == '+' || fuhao == '-' || fuhao == 'x' || fuhao == '÷') { - calcuStrList.add(String.valueOf(fuhao)); + fuhaoList.add(String.valueOf(fuhao)); numberList.add(buffer.toString()); buffer.delete(0, buffer.length()); } @@ -554,39 +562,39 @@ public class Calculator extends JFrame implements ActionListener { buffer.append(fuhao); } } - return calculationOrder(calcuStrList, numberList); + return calculationOrder(fuhaoList, numberList); } // 控制加减乘除的运算优先级顺序 - public String calculationOrder(LinkedList calcuStr, LinkedList number) { - if (calcuStr.size() > 0) { - int multiply = calcuStr.indexOf("x"); - int except = calcuStr.indexOf("÷"); + public String calculationOrder(LinkedList calculate, LinkedList number) { + if (calculate.size() > 0) { + int multiply = calculate.indexOf("x"); + int except = calculate.indexOf("÷"); // 除在前乘在后,或有除无乘,则先算除 if ((multiply != -1 && except != -1 && except < multiply) - || (calcuStr.contains("÷") && !calcuStr.contains("x"))) { - calcuResult("÷", calcuStr, number); + || (calculate.contains("÷") && !calculate.contains("x"))) { + calcuResult("÷", calculate, number); } // 乘在前除在后,或有乘无除,先算乘 if ((multiply != -1 && except != -1 && multiply < except) - || (calcuStr.contains("x") && !calcuStr.contains("÷"))) { - calcuResult("x", calcuStr, number); + || (calculate.contains("x") && !calculate.contains("÷"))) { + calcuResult("x", calculate, number); } - // 无乘除只有加减,则从左往右计算 - else if (calcuStr.size() > 0) { - calcuResult(calcuStr.get(0), calcuStr, number); + // 无乘除只有加减,则按规定从左往右计算 + else if (calculate.size() > 0) { + calcuResult(calculate.get(0), calculate, number); } } return number.get(0); // 返回最终数链表中第一个位置的数据,即经过一次计算的结果 } - // 通过location找到运算符在calcuStr出现的位置,根据位置在number中找到符号前后的两个数进行计算 - public String calcuResult(String location, LinkedList calcuStr, LinkedList number) { - int position = calcuStr.indexOf(location); + // 通过location找到运算符在calculate出现的位置,根据位置在number中找到对应符号前后的两个数进行计算 + public String calcuResult(String location, LinkedList calculate, LinkedList number) { + int position = calculate.indexOf(location); double a = Double.parseDouble(number.get(position)); double b = Double.parseDouble(number.get(position + 1)); - String calculateNumber = ""; + String calculateNumber =""; switch (location) { case "+": calculateNumber = String.valueOf(a + b); @@ -598,13 +606,19 @@ public class Calculator extends JFrame implements ActionListener { calculateNumber = String.valueOf(a * b); break; case "÷": + if(b==0) { + showText1.setText("除数不能为0!"); + break; + } + else { calculateNumber = String.valueOf(a / b); break; + } } number.set(position, calculateNumber);// 两个数算完后把结果给前一个数 - calcuStr.remove(position);// 在cacuStr集中每算完一个符号就删一个符号 + calculate.remove(position);// 在calculate集中每算完一个符号就删一个符号 number.remove(position + 1);// 在number集中两个数运算完后删除后面的数 - return calculationOrder(calcuStr, number); // 一直算直到calcu集中没有符号为止 + return calculationOrder(calculate, number); // 一直算直到calculate集中没有符号为止 } } -- Gitee From ee9d5b8d86eb544b31549962687c2b0456a15a18 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Thu, 10 Jun 2021 15:54:30 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E6=98=BE=E7=A4=BA=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 74 +++++++++++++++--------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 26a5180..4301c43 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -260,7 +260,7 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("mod")) { str1 = str2; str2 = ""; - flag = 3; + flag = 3; } if (e.getActionCommand().equals("=") && flag == 3) { n = Double.parseDouble(str2); @@ -268,6 +268,7 @@ public class Calculator extends JFrame implements ActionListener { result = m % n; str2 = result + ""; showText2.setText(str2); + showText1.setText(str1+"mod"+str2+"="); flag = 0; } @@ -284,7 +285,7 @@ public class Calculator extends JFrame implements ActionListener { showText1.setText("数值大于零,才能开根号"); } } - //"X^2" 平方 + // "X^2" 平方 if (e.getActionCommand().equals("X^2")) { m = Double.parseDouble(str2); result = m * m; @@ -294,7 +295,7 @@ public class Calculator extends JFrame implements ActionListener { } // "1/x"倒数 if (e.getActionCommand().equals("1/x")) { - if (str2 =="0") { + if (str2 == "0") { showText1.setText("除数不能为0!"); } else if (Double.parseDouble(str2) != 0) { m = Double.parseDouble(str2); @@ -394,7 +395,7 @@ public class Calculator extends JFrame implements ActionListener { } // 菜单“更多”里的函数 - //"x^3" + // "x^3" if (e.getActionCommand().equals("x^3")) { m = Double.parseDouble(str2); result = m * m * m; @@ -402,7 +403,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText(m + "^3" + "="); } - //"3√x" + // "3√x" if (e.getActionCommand().equals("3√x")) { m = Double.parseDouble(str2); result = Math.cbrt(m); @@ -410,22 +411,23 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText("3√" + m + "="); } - //"y√x" + // "y√x" if (e.getActionCommand().equals("y√x")) { str1 = str2; str2 = ""; flag = 4; } - + if (e.getActionCommand().equals("=") && flag == 4) { m = Double.parseDouble(str1); n = Double.parseDouble(str2); result = Math.pow(m, 1 / n); str2 = result + ""; showText2.setText(str2); + showText1.setText(n+"√"+m+"="); flag = 0; } - //"2^x" + // "2^x" if (e.getActionCommand().equals("2^x")) { m = Double.parseDouble(str2); result = Math.pow(2, m); @@ -433,7 +435,7 @@ public class Calculator extends JFrame implements ActionListener { showText2.setText(str2); showText1.setText("2^" + m + "="); } - //"logyx" + // "logyx" if (e.getActionCommand().equals("logyx")) { flag = 5; str1 = str2; @@ -442,14 +444,14 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("=") && flag == 5) { m = Double.parseDouble(str1); n = Double.parseDouble(str2); - if (n != 1 && m > 0 && n > 0) //保证真数和分母都大于0 + if (n != 1 && m > 0 && n > 0) // 保证真数和分母都大于0 { result = Math.log(m) / Math.log(n); str2 = result + ""; showText2.setText(str2); + showText1.setText("log"+m+n+"="); flag = 0; - } - else { + } else { showText1.setText("真数需都大于0,且分母不能为0"); } } @@ -502,20 +504,21 @@ public class Calculator extends JFrame implements ActionListener { result = Math.asin(m); str2 = result + ""; showText2.setText(str2); - + showText1.setText("sin^-1"+"("+m+")"); } if (e.getActionCommand().equals("cos^-1")) { m = Double.parseDouble(str2); result = Math.acos(m); str2 = result + ""; showText2.setText(str2); - + showText1.setText("cos^-1"+"("+m+")"); } if (e.getActionCommand().equals("tan^-1")) { m = Double.parseDouble(str2); result = Math.atan(m); str2 = result + ""; showText2.setText(str2); + showText1.setText("tan^-1"+"("+m+")"); } if (e.getActionCommand().equals("根据角度求弧度")) { @@ -528,14 +531,14 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("=")) { showText1.setText(showText2.getText() + button[34].getText()); - Scanner scanner = new Scanner(showText2.getText()); //用Scanner分割输入框输入的字符,得到一个个字符 + Scanner scanner = new Scanner(showText2.getText()); // 用Scanner分割输入框输入的字符,得到一个个字符 str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字直到没有字符 showText2.setText(str2); } } - // 分割字符串,把运算符和数字分别放到calcuStrList和numberList里 + // 分割字符串,把运算符和数字分别放到fuhaoList和numberList里 public String saveNumberAndCalculator(String str) { // 若第一个数字是负数,在"-"前加0 if (str.startsWith("-")) { @@ -548,7 +551,7 @@ public class Calculator extends JFrame implements ActionListener { char[] strChar = str.toCharArray(); LinkedList fuhaoList = new LinkedList<>(); // 存储运算符号 LinkedList numberList = new LinkedList<>(); // 存储数字 - StringBuffer buffer = new StringBuffer(); //中间存放数字的buffer + StringBuffer buffer = new StringBuffer(); for (char fuhao : strChar) { if (fuhao == '+' || fuhao == '-' || fuhao == 'x' || fuhao == '÷') { fuhaoList.add(String.valueOf(fuhao)); @@ -562,39 +565,39 @@ public class Calculator extends JFrame implements ActionListener { buffer.append(fuhao); } } - return calculationOrder(fuhaoList, numberList); + return calculateOrder(fuhaoList, numberList); } // 控制加减乘除的运算优先级顺序 - public String calculationOrder(LinkedList calculate, LinkedList number) { + public String calculateOrder(LinkedList calculate, LinkedList number) { if (calculate.size() > 0) { int multiply = calculate.indexOf("x"); int except = calculate.indexOf("÷"); - // 除在前乘在后,或有除无乘,则先算除 - if ((multiply != -1 && except != -1 && except < multiply) - || (calculate.contains("÷") && !calculate.contains("x"))) { - calcuResult("÷", calculate, number); - } - // 乘在前除在后,或有乘无除,先算乘 + // 如果乘在前除在后或者有乘无除,则先算乘 if ((multiply != -1 && except != -1 && multiply < except) || (calculate.contains("x") && !calculate.contains("÷"))) { - calcuResult("x", calculate, number); + calculateResult("x", calculate, number); } - // 无乘除只有加减,则按规定从左往右计算 + // 如果除在前乘在后或者有除无乘,则先算除 + else if ((multiply != -1 && except != -1 && except < multiply) + || (calculate.contains("÷") && !calculate.contains("x"))) { + calculateResult("÷", calculate, number); + } + // 如果无乘除只有加减,则按规定从左往右计算 else if (calculate.size() > 0) { - calcuResult(calculate.get(0), calculate, number); + calculateResult(calculate.get(0), calculate, number); } } return number.get(0); // 返回最终数链表中第一个位置的数据,即经过一次计算的结果 } // 通过location找到运算符在calculate出现的位置,根据位置在number中找到对应符号前后的两个数进行计算 - public String calcuResult(String location, LinkedList calculate, LinkedList number) { + public String calculateResult(String location, LinkedList calculate, LinkedList number){ int position = calculate.indexOf(location); double a = Double.parseDouble(number.get(position)); double b = Double.parseDouble(number.get(position + 1)); - String calculateNumber =""; + String calculateNumber = ""; switch (location) { case "+": calculateNumber = String.valueOf(a + b); @@ -606,19 +609,18 @@ public class Calculator extends JFrame implements ActionListener { calculateNumber = String.valueOf(a * b); break; case "÷": - if(b==0) { + if (b == 0) { showText1.setText("除数不能为0!"); break; - } - else { - calculateNumber = String.valueOf(a / b); - break; + } else { + calculateNumber = String.valueOf(a / b); + break; } } number.set(position, calculateNumber);// 两个数算完后把结果给前一个数 calculate.remove(position);// 在calculate集中每算完一个符号就删一个符号 number.remove(position + 1);// 在number集中两个数运算完后删除后面的数 - return calculationOrder(calculate, number); // 一直算直到calculate集中没有符号为止 + return calculateOrder(calculate, number); // 一直算直到calculate集中没有符号为止 } } -- Gitee From 702660672c7829ad9ea79c9461ea84cb2d8ec5e8 Mon Sep 17 00:00:00 2001 From: Elfli <646963079@qq.com> Date: Thu, 10 Jun 2021 16:13:35 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=98=BE=E7=A4=BA=E6=A1=86?= =?UTF-8?q?=E7=9A=84=E6=98=BE=E7=A4=BAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 4301c43..0f38ad5 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -241,6 +241,7 @@ public class Calculator extends JFrame implements ActionListener { n = Double.parseDouble(str1); result = Math.pow(n, m); showText2.setText(result + ""); + showText1.setText(n + "^" + m + "="); flag = 0; } // "exp"科学计数 @@ -260,7 +261,7 @@ public class Calculator extends JFrame implements ActionListener { if (e.getActionCommand().equals("mod")) { str1 = str2; str2 = ""; - flag = 3; + flag = 3; } if (e.getActionCommand().equals("=") && flag == 3) { n = Double.parseDouble(str2); @@ -268,7 +269,7 @@ public class Calculator extends JFrame implements ActionListener { result = m % n; str2 = result + ""; showText2.setText(str2); - showText1.setText(str1+"mod"+str2+"="); + showText1.setText(str1 + "mod" + str2 + "="); flag = 0; } @@ -424,7 +425,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.pow(m, 1 / n); str2 = result + ""; showText2.setText(str2); - showText1.setText(n+"√"+m+"="); + showText1.setText(n + "√" + m + "="); flag = 0; } // "2^x" @@ -449,7 +450,7 @@ public class Calculator extends JFrame implements ActionListener { result = Math.log(m) / Math.log(n); str2 = result + ""; showText2.setText(str2); - showText1.setText("log"+m+n+"="); + showText1.setText("log" + m + n + "="); flag = 0; } else { showText1.setText("真数需都大于0,且分母不能为0"); @@ -504,21 +505,21 @@ public class Calculator extends JFrame implements ActionListener { result = Math.asin(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("sin^-1"+"("+m+")"); + showText1.setText("sin^-1" + "(" + m + ")"); } if (e.getActionCommand().equals("cos^-1")) { m = Double.parseDouble(str2); result = Math.acos(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("cos^-1"+"("+m+")"); + showText1.setText("cos^-1" + "(" + m + ")"); } if (e.getActionCommand().equals("tan^-1")) { m = Double.parseDouble(str2); result = Math.atan(m); str2 = result + ""; showText2.setText(str2); - showText1.setText("tan^-1"+"("+m+")"); + showText1.setText("tan^-1" + "(" + m + ")"); } if (e.getActionCommand().equals("根据角度求弧度")) { @@ -593,7 +594,7 @@ public class Calculator extends JFrame implements ActionListener { } // 通过location找到运算符在calculate出现的位置,根据位置在number中找到对应符号前后的两个数进行计算 - public String calculateResult(String location, LinkedList calculate, LinkedList number){ + public String calculateResult(String location, LinkedList calculate, LinkedList number) { int position = calculate.indexOf(location); double a = Double.parseDouble(number.get(position)); double b = Double.parseDouble(number.get(position + 1)); -- Gitee