diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000000000000000000000000000000000..393386350584b5886347afb32886ff41bce41474 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/MigLayout.xml b/.idea/libraries/MigLayout.xml new file mode 100644 index 0000000000000000000000000000000000000000..ab653dfbbaf2390c8e9ceb3e08b0265a2fd219bb --- /dev/null +++ b/.idea/libraries/MigLayout.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..ebe01736f9211ba9fa43d3a87cc37ca6b91d04d8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..ea25cb6475b9409a6cd5868ef50ea0cdc7ce9a84 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/classes/production/java2022spring/java2022spring/Calculator.class b/classes/production/java2022spring/java2022spring/Calculator.class new file mode 100644 index 0000000000000000000000000000000000000000..8be9cffb9efb68c5a9704534244963688cc3f0ca Binary files /dev/null and b/classes/production/java2022spring/java2022spring/Calculator.class differ diff --git a/classes/production/java2022spring/java2022spring/Main.class b/classes/production/java2022spring/java2022spring/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..11a5cf0a1e0765c7c3b08640def195ad3c15778c Binary files /dev/null and b/classes/production/java2022spring/java2022spring/Main.class differ diff --git a/classes/production/java2022spring/java2022spring/Test.class b/classes/production/java2022spring/java2022spring/Test.class new file mode 100644 index 0000000000000000000000000000000000000000..b5674a468b5ae1c1f323e527080ba64303f634c1 Binary files /dev/null and b/classes/production/java2022spring/java2022spring/Test.class differ diff --git a/java2022spring.iml b/java2022spring.iml new file mode 100644 index 0000000000000000000000000000000000000000..a7a8a9da203a5d22daf6838e52a639771c95a796 --- /dev/null +++ b/java2022spring.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/miglayout-core-javadoc.jar b/lib/miglayout-core-javadoc.jar new file mode 100644 index 0000000000000000000000000000000000000000..71c05f40de658b43c5a17c508a5435dec1ca98f8 Binary files /dev/null and b/lib/miglayout-core-javadoc.jar differ diff --git a/lib/miglayout-core-sources.jar b/lib/miglayout-core-sources.jar new file mode 100644 index 0000000000000000000000000000000000000000..d377305d13c68ecc5c8ce838a323467d809c2846 Binary files /dev/null and b/lib/miglayout-core-sources.jar differ diff --git a/lib/miglayout-core.jar b/lib/miglayout-core.jar new file mode 100644 index 0000000000000000000000000000000000000000..2b8e720ac6e2b670e7fccca9a3b89641123b548f Binary files /dev/null and b/lib/miglayout-core.jar differ diff --git a/lib/miglayout-swing-javadoc.jar b/lib/miglayout-swing-javadoc.jar new file mode 100644 index 0000000000000000000000000000000000000000..667ef2fe4e155511442f85523f95d5f775248375 Binary files /dev/null and b/lib/miglayout-swing-javadoc.jar differ diff --git a/lib/miglayout-swing-sources.jar b/lib/miglayout-swing-sources.jar new file mode 100644 index 0000000000000000000000000000000000000000..49f4bd267b60aa525f8c21b68550d157513ed881 Binary files /dev/null and b/lib/miglayout-swing-sources.jar differ diff --git a/lib/miglayout-swing.jar b/lib/miglayout-swing.jar new file mode 100644 index 0000000000000000000000000000000000000000..da98004da3bef99152c63c062c9417a5d0c7fed1 Binary files /dev/null and b/lib/miglayout-swing.jar differ diff --git a/readme.md b/readme.md index 4af16add33b576d095308a1888db35774618715d..3ec439d206f8195801154372da3645b7ea8c26d5 100644 --- a/readme.md +++ b/readme.md @@ -1,2 +1,2 @@ -# ѧ -0103 \ No newline at end of file +# 科学计算器 +0103 张三 \ No newline at end of file diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..4597b05f82750d1272e7f5cabea264cd41415ffe --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,325 @@ +package java2022spring; +import javax.swing.*; + import java.awt.*; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Objects; +public class Calculator extends JFrame implements ActionListener { + JTextField text = new JTextField(""); + private String str = "";//文本框内容字符串 + + public Calculator() { + + setResizable(false); + setBounds(800, 200, 600, 800);//设置窗口出现位置和大小 + setTitle("计算器"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置为点击×号关闭程序运行 + this.setLayout(new BorderLayout()); + JPanel jPanel1 = new JPanel(); + JPanel jPanel2 = new JPanel(); + text.setPreferredSize(new Dimension(570, 100));//文本框大小 + jPanel2.setLayout(new FlowLayout()); + jPanel1.add(text); + this.add(jPanel1, BorderLayout.NORTH); + text.setHorizontalAlignment(SwingConstants.RIGHT);//将JTextFiled内容设置为右对齐 + jPanel2.setLayout(new GridLayout(7, 4)); + JButton button[] = new JButton[28]; + text.setFont(new Font("宋体", Font.BOLD, 50));//设置显示的字体 + text.setBorder(BorderFactory.createLineBorder(Color.black, 1));//给文本框加黑边框 + jPanel1.setBorder(BorderFactory.createRaisedBevelBorder());//设置边框 + jPanel1.setBackground(Color.white);//设置组件颜色 + jPanel2.setBackground(Color.white);// + jPanel2.setBorder(BorderFactory.createRaisedBevelBorder());//设置边框 + + + + + //给按钮命名 + String buttonName[] = { //代码修改为这种更简洁且更便于修改的形式 + "%", "C", "⇚", "✓", + "(", ")", "x^2", "1/x", + "cos", "sin", "tan", "/", + "7", "8", "9", "*", + "4", "5", "6", "-", + "1", "2", "3", "+", + "π", "0", ".", "=" + }; + + + for (int i = 0; i < button.length; i++) { + button[i] = new JButton(buttonName[i]); + button[i].setFont(new Font("粗体", Font.BOLD, 20)); //修改字体 + button[i].addActionListener(this); + button[i].setBorder(BorderFactory.createRaisedBevelBorder()); + button[i].setBackground(Color.white); + //button[i].setContentAreaFilled(false); + //button[i].setBackground(Color.darkGray); + } + button[27].setBorder(BorderFactory.createLoweredBevelBorder()); + button[27].setBackground(Color.ORANGE); + button[27].setContentAreaFilled(true); + for (int i = 0; i < button.length; i++) { + jPanel2.add(button[i]); + } + this.add(jPanel2, BorderLayout.CENTER); + + + setVisible(true);//不知道为什么这个只有放在后面才能正常显示组件 + + + } + + //注册监视器 + + public void actionPerformed(ActionEvent e) { + String clickButton = e.getActionCommand(); + if (Objects.equals(clickButton, "=")) + { + text.setText(this.str); + + if (clickButton.equals("="))//调用计算方法,得出最终结果 + { + String[] s = houzhui(this.str);//中缀表达式转换为后缀表达式 + String result = Calculate(s); + this.str = result + ""; + text.setText(this.str); + } + } else if (Objects.equals(clickButton, "C"))//清空按钮,消除显示屏文本框前面所有的输入和结果 + { + this.str = ""; + text.setText("0"); + } else if (Objects.equals(clickButton, "⇚"))//退格,将文本最后一个字符去掉 + { + String t = text.getText(); + int i = t.length(); + if (i > 0) { + t = t.substring(0, i - 1); + this.str = t; + if (i == 0) { + text.setText("0"); + } else { + text.setText(t); + } + } + + } else if (Objects.equals(clickButton, "✓")) { + String n = Rooting(this.str); + text.setText(n); + this.str = n; + } else if (Objects.equals(clickButton, "1/x")) { + String q = reciprocal(this.str); + text.setText(q); + this.str=q; + } else if (Objects.equals(clickButton, "x^2")) { + String m = square(this.str); + text.setText(m); + this.str = m; + } else if(Objects.equals(clickButton,"cos")){ + String cos = cosCalculate(this.str); + text.setText(cos); + + } else if(Objects.equals(clickButton,"sin")){ + String sin = sinCalculate(this.str); + text.setText(sin); + } else if(Objects.equals(clickButton,"tan")){ + String tan = tanCalculate(this.str); + text.setText(tan); + } else if(Objects.equals(clickButton,"π")){ + + String m=String.valueOf(Math.PI); + this.str=this.str+m; + text.setText(this.str); + + + } else { + this.str = this.str + clickButton; + text.setText(this.str); + } + + + } + + // 计算方法 + public String Calculate(String[] str) { + String[] Result = new String[1000]; + int Top = -1; + for (int i = 0; str[i] != null; i++) { + if ("+-*%/".indexOf(str[i]) < 0) { + Top++; + Result[Top] = str[i]; + } + if ("+-*%/".indexOf(str[i]) >= 0) + { + double x, y, n; + x = Double.parseDouble(Result[Top]); + Top--; + y = Double.parseDouble(Result[Top]); + Top--; + if ("*".indexOf(str[i]) >= 0) { + n = y * x; + Top++; + Result[Top] = String.valueOf(n); + + } + if ("/".indexOf(str[i]) >= 0) + { + if (x == 0)// + { + String s = "被除数不能为0"; + return s; + } else { + n = y / x; + Top++; + Result[Top] = String.valueOf(n);// + } + } + if ("%".indexOf(str[i]) >= 0) + { + if (x == 0)// 被除数不允许为0 + { + String s = " 被除数不允许为0"; + return s; + } else { + n = y % x; + Top++; + Result[Top] = String.valueOf(n); + } + } + if ("-".indexOf(str[i]) >= 0) { + n = y - x; + Top++; + Result[Top] = String.valueOf(n); + } + if ("+".indexOf(str[i]) >= 0) { + n = y + x; + Top++; + Result[Top] = String.valueOf(n); + } + } + } + return Result[Top];// 返回最终结果 + } + + //开方运算方法 + public String Rooting(String str) { + String result = ""; + double a = Double.parseDouble(str), b = 0; + b = Math.sqrt(a); + result = String.valueOf(b);//将运算结果转换为string类型并赋给string类型的变量result + return result; + } + + //平方运算方法 + public String square(String str) { + String result = ""; + double a = Double.parseDouble(str), b = 0; + b = Math.pow(a, 2); + result = String.valueOf(b); + return result; + } + //计算倒数 + public String reciprocal(String str){ + String result = ""; + double a = Double.parseDouble(str), b = 0; + if(a==0){ + result="0没有倒数"; + str=""; + return result; + }else { + b = 1 / a; + result = String.valueOf(b); + return result; + } + + } + + //计算cos值 + public String cosCalculate(String str){ + String result=""; + double a = Double.parseDouble(str),b=0; + b = Math.cos(a); + result = String.valueOf(b); + return result; + } + //计算sin值 + public String sinCalculate(String str) { + String result = ""; + double a = Double.parseDouble(str), b = 0; + b = Math.sin(a); + result = String.valueOf(b); + return result; + } + //计算tan值 + public String tanCalculate(String str){ + String result=""; + double a = Double.parseDouble(str),b=0; + b = Math.tan(a); + result = String.valueOf(b); + return result; + } + + + /*将中缀表达式转换为后缀表达式 + 以下的算法思想引用借鉴了博客: + https://blog.csdn.net/weixin_50301255/article/details/121889662的中缀表达式转为后缀表达式计算的算法*/ + private String[] houzhui(String str) { + + try { + String s = "";// 用于承接多位数的字符串 + char[] opStack = new char[100]; + String[] postQueue = new String[100];// 后缀表达式字符串数组,为了将多位数存储为独立的字符串 + int top = -1, j = 0; + for (int i = 0; i < str.length(); i++)// 遍历中缀表达式 + { + if ("0123456789.".indexOf(str.charAt(i)) >= 0) // 遇到数字字符的情况直接入队 + { + s = "";// 作为承接字符,每次开始时都要清空 + for (; i < str.length() && "0123456789.".indexOf(str.charAt(i)) >= 0; i++) { + s = s + str.charAt(i); + } + i--; + postQueue[j] = s;// 数字字符直接加入后缀表达式 + j++; + } else if ("(".indexOf(str.charAt(i)) >= 0) { + top++; + opStack[top] = str.charAt(i); + } else if (")".indexOf(str.charAt(i)) >= 0) {// 遇到右括号 + for (; ; )// 栈顶元素循环出栈,直到遇到左括号为止 + { + if (opStack[top] != '(') {// 栈顶元素不是左括号 + postQueue[j] = opStack[top] + "";// 栈顶元素出栈 + j++; + top--; + } else { // 找到栈顶元素是左括号 + top--; + break; + } + } + } else if ("*%/+-".indexOf(str.charAt(i)) >= 0) { + if (top == -1) { + top++; + opStack[top] = str.charAt(i); + } else if ("*%/".indexOf(opStack[top]) >= 0) { + postQueue[j] = opStack[top] + ""; + j++; + opStack[top] = str.charAt(i); + } else { + top++; + opStack[top] = str.charAt(i); + } + } + } + while (top != -1) { + postQueue[j] = opStack[top] + ""; + j++; + top--; + } + return postQueue; + } catch (Exception e) { + String[] error ={"error!"}; + return error; + } + } + +} diff --git a/src/java2022spring/Main.java b/src/java2022spring/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..fc7a692121851e495cc22b979d8fb7bfb47330b0 --- /dev/null +++ b/src/java2022spring/Main.java @@ -0,0 +1,7 @@ +package java2022spring; + +public class Main { + public static void main(String[] args) { + Calculator calculator=new Calculator();//执行构造方法打开计算器 + } +}