diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..0f38ad59214a4fa91591eee200451addd9f077e1 --- /dev/null +++ b/src/java2020spring/Calculator.java @@ -0,0 +1,627 @@ +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 JTextField showText1, showText2; // 1为显示框,2为输入框 + 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; + 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() { + init(); + } + + // 版面设计 + 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"); + 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(); + JButton huan = new JButton("另一界面"); + p4.add(huan);// + 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); + 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].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); + add(mainp); + + // 注册监听器 + + huan.addActionListener(this); + 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) { + + // 实现相关功能 + // 什么都没有输入的情况下点击按钮,报错提示重新输入 + if (str2 == "") { + 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] + || 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); + showText1.setText(str2); + } + + // 按下小数点 + + if (e.getActionCommand().equals(".")) { + + str2 += "."; + showText2.setText(str2); + showText1.setText(str2); + } + + // 按下 "(" + if (e.getActionCommand().equals("(")) { + str2 += "("; + showText2.setText(str2); + showText1.setText("抱歉,尚不可用"); + } + // 按下")" + if (e.getActionCommand().equals(")")) { + str2 += ")"; + showText2.setText(str2); + showText1.setText("抱歉,尚不可用"); + } + // 按下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(); + showText2.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(n, m); + showText2.setText(result + ""); + showText1.setText(n + "^" + m + "="); + 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); + showText1.setText(str1 + "mod" + str2 + "="); + flag = 0; + } + + // "√x"开根号 + if (e.getActionCommand().equals("√x")) { + m = Double.parseDouble(str2); + if (m >= 0) { + result = Math.sqrt(m); + str2 = "" + result; + showText2.setText(str2); + showText1.setText("√" + m + "="); + + } else { + showText1.setText("数值大于零,才能开根号"); + } + } + // "X^2" 平方 + if (e.getActionCommand().equals("X^2")) { + m = Double.parseDouble(str2); + result = m * m; + str2 = "" + result; + showText2.setText(str2); + showText1.setText(m + "^2" + "="); + } + // "1/x"倒数 + if (e.getActionCommand().equals("1/x")) { + if (str2 == "0") { + showText1.setText("除数不能为0!"); + } else if (Double.parseDouble(str2) != 0) { + m = Double.parseDouble(str2); + result = 1 / m; + str2 = "" + result; + showText2.setText(str2); + showText1.setText("1/" + m + "="); + + } 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); + 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 + "|" + "="); + } + // "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); + showText1.setText("fact" + "(" + m + ")" + "="); + } + } + + // "10^x" + if (e.getActionCommand().equals("10^x")) { + m = Double.parseDouble(str2); + result = Math.pow(10, m); + str2 = result + ""; + showText2.setText(str2); + showText1.setText("10^" + m + "="); + } + // "lg" + if (e.getActionCommand().equals("lg")) { + m = Double.parseDouble(str2); + if (m > 0) { + result = Math.log10(m); + str2 = result + ""; + showText2.setText(str2); + showText1.setText("lg" + m + "="); + } 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); + showText1.setText("ln" + m + "="); + } else { + showText1.setText("请输入大于0的数!"); + } + + } + // 菜单“更多”里的函数 + // "x^3" + if (e.getActionCommand().equals("x^3")) { + m = Double.parseDouble(str2); + result = m * m * m; + str2 = result + ""; + showText2.setText(str2); + showText1.setText(m + "^3" + "="); + } + // "3√x" + if (e.getActionCommand().equals("3√x")) { + m = Double.parseDouble(str2); + result = Math.cbrt(m); + str2 = result + ""; + 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); + result = Math.pow(m, 1 / n); + str2 = result + ""; + showText2.setText(str2); + showText1.setText(n + "√" + m + "="); + flag = 0; + } + // "2^x" + if (e.getActionCommand().equals("2^x")) { + m = Double.parseDouble(str2); + result = Math.pow(2, m); + str2 = result + ""; + showText2.setText(str2); + showText1.setText("2^" + m + "="); + } + // "logyx" + 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) // 保证真数和分母都大于0 + { + result = Math.log(m) / Math.log(n); + str2 = result + ""; + showText2.setText(str2); + showText1.setText("log" + m + n + "="); + 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); + 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")) { + m = Double.parseDouble(str2); + 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("根据角度求弧度")) { + m = Double.parseDouble(str2); + result = Math.toRadians(m); + str2 = result + ""; + showText2.setText(str2); + } + // 按下"=" + if (e.getActionCommand().equals("=")) { + + showText1.setText(showText2.getText() + button[34].getText()); + Scanner scanner = new Scanner(showText2.getText()); // 用Scanner分割输入框输入的字符,得到一个个字符 + str2 = saveNumberAndCalculator(scanner.next()); // 分别存符号和数字直到没有字符 + showText2.setText(str2); + + } + } + + // 分割字符串,把运算符和数字分别放到fuhaoList和numberList里 + public String saveNumberAndCalculator(String str) { + // 若第一个数字是负数,在"-"前加0 + if (str.startsWith("-")) { + StringBuffer s1 = new StringBuffer(str); + s1.insert(0, "0"); + str = s1.toString(); + } + // 加上"#"用于获取分割最后一个数字 + str = str + "#"; + char[] strChar = str.toCharArray(); + LinkedList fuhaoList = new LinkedList<>(); // 存储运算符号 + LinkedList numberList = new LinkedList<>(); // 存储数字 + StringBuffer buffer = new StringBuffer(); + for (char fuhao : strChar) { + if (fuhao == '+' || fuhao == '-' || fuhao == 'x' || fuhao == '÷') { + fuhaoList.add(String.valueOf(fuhao)); + numberList.add(buffer.toString()); + buffer.delete(0, buffer.length()); + } + // 判断是不是最后一个数,是的话加到数字列表里 + else if (fuhao == '#') { + numberList.add(buffer.toString()); + } else { + buffer.append(fuhao); + } + } + return calculateOrder(fuhaoList, numberList); + } + + // 控制加减乘除的运算优先级顺序 + 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 && multiply < except) + || (calculate.contains("x") && !calculate.contains("÷"))) { + 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) { + calculateResult(calculate.get(0), calculate, number); + } + } + return number.get(0); // 返回最终数链表中第一个位置的数据,即经过一次计算的结果 + } + + // 通过location找到运算符在calculate出现的位置,根据位置在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 = ""; + switch (location) { + case "+": + calculateNumber = String.valueOf(a + b); + break; + case "-": + calculateNumber = String.valueOf(a - b); + break; + case "x": + 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);// 两个数算完后把结果给前一个数 + calculate.remove(position);// 在calculate集中每算完一个符号就删一个符号 + number.remove(position + 1);// 在number集中两个数运算完后删除后面的数 + return calculateOrder(calculate, number); // 一直算直到calculate集中没有符号为止 + } + +} diff --git a/src/java2020spring/PanelNullLayout.java b/src/java2020spring/PanelNullLayout.java new file mode 100644 index 0000000000000000000000000000000000000000..36aaf5175743f4f07c5466b793e09a56d3f065c4 --- /dev/null +++ b/src/java2020spring/PanelNullLayout.java @@ -0,0 +1,106 @@ +package java2020spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +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 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 fc09c195f566d0e707f5e8a2f05fd168069c73a7..217cc4866003c25a4e977fb3ad4cbbe2ffdbab67 100644 --- a/src/java2020spring/Test.java +++ b/src/java2020spring/Test.java @@ -1,10 +1,16 @@ 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, 450, 600); + win.setVisible(true); + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }