From ea5cc8f0dc24d2fd649f167aec0337bcdea65492 Mon Sep 17 00:00:00 2001 From: summercat Date: Fri, 13 May 2022 00:37:34 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=89=88=E9=9D=A2?= =?UTF-8?q?=E5=92=8C=E5=BC=80=E5=85=B3=E5=B5=8C=E5=85=A5=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=9B=9B=E5=88=99=E6=B7=B7=E5=90=88=E8=BF=90=E7=AE=97?= =?UTF-8?q?=20=E5=AE=8C=E6=88=90=E5=B9=B3=E6=96=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 227 +++++++++++++++++++++++++ src/java2022spring/Main.java | 9 + src/java2022spring/MixedOperation.java | 113 ++++++++++++ 3 files changed, 349 insertions(+) create mode 100644 src/java2022spring/Calculation.java create mode 100644 src/java2022spring/Main.java create mode 100644 src/java2022spring/MixedOperation.java diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java new file mode 100644 index 0000000..56bc5f6 --- /dev/null +++ b/src/java2022spring/Calculation.java @@ -0,0 +1,227 @@ +//运用BigDecimal类进行精确计算 +package java2022spring; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.math.BigDecimal; + +import java.text.DecimalFormat; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JTextField; + + +class Calculation extends JFrame{ + + private GridBagConstraints gridBagConstraints = new GridBagConstraints();//实例化该对象用来对组件进行管理 + private MixedOperation mix = new MixedOperation(); //混合运算 + private GridBagLayout gridBagLayout = new GridBagLayout(); + private DecimalFormat df = new DecimalFormat(); //对字符串进行三位一组分割 + + //输入输出栏保存 + private String input = new String(""); //保存输入的算式 + private BigDecimal output = new BigDecimal("0"); //保存计算结果 + + private boolean calculateFlag = false; //如果上一步进行了计算,该标志为为true + + //设置控件位置及大小 + private void setControl(JComponent e, int x, int y, int width, int height){ + gridBagConstraints.gridx = x; + gridBagConstraints.gridy = y; + gridBagConstraints.gridwidth = width; + gridBagConstraints.gridheight = height; + gridBagLayout.setConstraints(e, gridBagConstraints); + this.add(e); + } + + public Calculation(){ + + //按键功能实现 + class Function implements ActionListener{ + + public void actionPerformed(ActionEvent e){ + String str = e.getActionCommand(); + + switch(str){ + case "0":case "1":case "2":case "3":case "4":case "5"://四则运算直接转换为字符串储存 + case "6":case "7":case "8":case "9":case ".":case "+": + case "-":case "*":case "/":case "(":case ")": + input+=str;//常用数字及运算符直接记录 + break; + + case "=": + output = mix.getMixedOperationRes(input); + calculateFlag = true; //该结果可以用于下次计算 + break; + + case "x^2": //x的平方,直接计算结果 + BigDecimal b=StoB(input); + output = b.multiply(b); + calculateFlag = true; //该结果可以用于下次计算 + break; + + case "C": //清空输入输出 + input = ""; //输入清空 + output = BigDecimal.ZERO; //输出显示"0" + break; + + case "←": //退格,清除一位输入 + if(!input.isEmpty()){ + input = input.substring(0,input.length()-1);//去掉末尾字符 由0~n变成0~n-1 + } + break; + + default:break; + } + tInput.setText(input);//显示输入栏 + tOutput.setText(df.format(output).toString());//三位一组显示输出结果 + + if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 + input = output.toString(); + calculateFlag = false;//结束此次运算 + } + } + //新增转换String成BigDecimal + public BigDecimal StoB(String s) { + Double a = Double.parseDouble(s); + BigDecimal b = BigDecimal.valueOf(a); + return b; + } + } + + + + + + + + + + + +//面板参数设置 + tInput.setHorizontalAlignment(JTextField.RIGHT); //输入算式文本显示右对齐 + tOutput.setHorizontalAlignment(JTextField.RIGHT); //结果文本显示右对齐 + + this.setSize(277,365); //界面大小 + this.setLocationRelativeTo(null);//窗口显示在中央 + this.setDefaultCloseOperation(3); + this.setResizable(false);//不可改变窗口大小 + this.setLayout(gridBagLayout); //窗体对象设置为GridBagLayout布局 + gridBagConstraints.fill = GridBagConstraints.BOTH; //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况 + + //按键 + //第1行 + setControl(tInput,0,1,5,1); //算式显示 + //第2行 + setControl(tOutput,0,2,5,1); //结果显示 + //第3行 + setControl(bSin,1,3,1,1); //sin + setControl(bCos,2,3,1,1); //cos + setControl(bTan,3,3,1,1); //tan + setControl(bSquare,4,3,1,1);//平方 + //第4行 + setControl(bDaoshu,1,4,1,1); //倒数 + setControl(bQvyv,2,4,1,1); //取余 + setControl(bC,3,4,1,1); //清空输入 + setControl(bTuige,4,4,1,1); //清除最后一位输入 + //第5行 + setControl(bLeft,1,5,1,1); //左括号 + setControl(bRight,2,5,1,1); //右括号 + setControl(bFactorial,3,5,1,1); //阶乘 + setControl(bDiv,4,5,1,1); //除号 + //第6行 + setControl(b7,1,6,1,1); //数字7 + setControl(b8,2,6,1,1); //数字8 + setControl(b9,3,6,1,1); //数字9 + setControl(bMul,4,6,1,1); //乘号 + //第7行 + setControl(b4,1,7,1,1); //数字4 + setControl(b5,2,7,1,1); //数字5 + setControl(b6,3,7,1,1); //数字6 + setControl(bdec,4,7,1,1); //减号 + //第8行 + setControl(b1,1,8,1,1); //数字1 + setControl(b2,2,8,1,1); //数字2 + setControl(b3,3,8,1,1); //数字3 + setControl(bAdd,4,8,1,1); //加号 + //第9行 + setControl(bNnegation,1,9,1,1); //取反 + setControl(b0,2,9,1,1); //数字0 + setControl(bPoint,3,9,1,1); //小数点 + setControl(bEqual,4,9,1,1); //等号 + + //注册监听器 + b0.addActionListener(new Function()); //数字0 + b1.addActionListener(new Function()); //数字1 + b2.addActionListener(new Function()); //数字2 + b3.addActionListener(new Function()); //数字3 + b4.addActionListener(new Function()); //数字4 + b5.addActionListener(new Function()); //数字5 + b6.addActionListener(new Function()); //数字6 + b7.addActionListener(new Function()); //数字7 + b8.addActionListener(new Function()); //数字8 + b9.addActionListener(new Function()); //数字9 + bPoint.addActionListener(new Function()); //小数点 + bAdd.addActionListener(new Function()); //加号 + bdec.addActionListener(new Function()); //减号 + bMul.addActionListener(new Function()); //乘号 + bDiv.addActionListener(new Function()); //除号 + bLeft.addActionListener(new Function()); //左括号 + bRight.addActionListener(new Function()); //右括号 + bEqual.addActionListener(new Function()); //等号 + + bSquare.addActionListener(new Function());//平方 + bC.addActionListener(new Function()); //清除输入输出 + bTuige.addActionListener(new Function()); //退格 + + this.setVisible(true); //窗体可见 +} + + //面板按键名称声明 + //为方便调试功能放在后面 + //第1行 + private JTextField tInput = new JTextField(""); //显示输入算式 + //第2行 + private JTextField tOutput = new JTextField(""); //显示输出结果 + //第3行 + private JButton bSquare = new JButton("x^2"); //平方 + private JButton bSin = new JButton("sin"); //正弦函数 + private JButton bCos = new JButton("cos"); //余弦函数 + private JButton bTan = new JButton("tan"); //正切函数 + //第4行 + private JButton bDaoshu = new JButton("1/x"); //倒数 + private JButton bQvyv = new JButton("%"); //取余 + private JButton bC = new JButton("C"); //清空输入输出 + private JButton bTuige = new JButton("←"); //退格,清除输入的最后一位 + //第5行 + private JButton bLeft = new JButton("("); //混合运算的左括号 + private JButton bRight = new JButton(")"); //混合运算的右括号 + private JButton bFactorial = new JButton("n!"); //阶乘 + private JButton bDiv = new JButton("/"); //除号 + //第6行 + private JButton b7 = new JButton("7"); //数字7 + private JButton b8 = new JButton("8"); //数字8 + private JButton b9 = new JButton("9"); //数字9 + private JButton bMul = new JButton("*"); //乘号 + //第7行 + private JButton b4 = new JButton("4"); //数字4 + private JButton b5 = new JButton("5"); //数字5 + private JButton b6 = new JButton("6"); //数字6 + private JButton bdec = new JButton("-"); //减号 + //第8行 + private JButton b1 = new JButton("1"); //数字1 + private JButton b2 = new JButton("2"); //数字2 + private JButton b3 = new JButton("3"); //数字3 + private JButton bAdd = new JButton("+"); //加号 + //第9行 + private JButton bNnegation = new JButton("+/-"); //正/负切换,取反 + private JButton b0 = new JButton("0"); //数字0 + private JButton bPoint = new JButton("."); //小数点 + private JButton bEqual = new JButton("="); //等号 +} \ No newline at end of file diff --git a/src/java2022spring/Main.java b/src/java2022spring/Main.java new file mode 100644 index 0000000..09d30e8 --- /dev/null +++ b/src/java2022spring/Main.java @@ -0,0 +1,9 @@ +package java2022spring; + +public class Main { + + public static void main(String[] args) { + Calculation t1=new Calculation(); + } + +} diff --git a/src/java2022spring/MixedOperation.java b/src/java2022spring/MixedOperation.java new file mode 100644 index 0000000..3dd202e --- /dev/null +++ b/src/java2022spring/MixedOperation.java @@ -0,0 +1,113 @@ +package java2022spring; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Stack; +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MixedOperation { + private final Pattern p = Pattern.compile("(^-|(?<=\\D)-)?[\\d.]+|[+\\-*\\/%!()√^]|log"); //用于比对运算符和参数的正则表达式确定优先级 + private final HashMap prio = new HashMap(); //存放运算符对应的优先级,后续通过查HASH表来判断 + private ArrayList eq1 = new ArrayList(); //保存算式的列表 + private ArrayList eq2 = new ArrayList(); //保存算式的中间列表 + private Stack stk = new Stack(); //使用该堆栈将中缀表达式转为后缀表达式 + + MixedOperation(){ + prio.put("(",0); //左括号优先级最低,但是遇到左括号直接入栈 + prio.put(")",0); //不会用到右括号的优先级,因为右括号不可能入栈,在此只把右括号当作一个运算符 + + prio.put("+",1); //加法优先级为1 + prio.put("-",1); //减法优先级为1 + prio.put("*",2); //乘法优先级为2 + prio.put("/",2); //除法优先级为2 + prio.put("%",2); //取余优先级为2 + } + + //运算前进行一些初始化操作,清空所用的列表和堆栈 + public BigDecimal getMixedOperationRes(String s){ + eq1.clear(); + eq2.clear(); + while(!stk.empty()){//循环移除栈空间中的参数 + stk.pop();//pop:移除堆栈顶部的对象,并返回该对象作为该函数的值。 + } + pretreatment(s); + Infix2Postfix(); + return new BigDecimal(postfixCalculate()); + } + + //预处理,将运算参数按照“运算符”和“数字”分别提取出来保存为单独一个字符串 + //比如输入"1.23+4.56",整体为一个字符串,经过处理后被分割成三个字符串"1.23","+","4.56" + private void pretreatment(String s){ + Matcher m = p.matcher(s); //将输入的算式s与正则表达式进行匹配 + while(m.find()){ + eq1.add(m.group()); //保存算式中运算符的位置 + } + } + //拆分输入算式,按优先级拆分括号嵌套算式、四则运算优先级等 + private void Infix2Postfix() { + for(int i=0;i prio.get(stk.peek())){//如果该运算符优先级高于栈顶运算符优先级,直接入栈 + stk.push(c); + } + else{ //该运算符优先级低于或等于栈顶运算符优先级,分四种情况 + while(!stk.empty() && !stk.peek().equals("(") && prio.get(stk.peek())>=prio.get(c)){ + eq2.add(stk.pop()); + } + stk.push(c); + } + } + else{ //对参数进行处理,参数直接添加进列表 + eq2.add(eq1.get(i)); + } + } + while(!stk.empty()){ + eq2.add(stk.pop()); + } + } + + private String postfixCalculate() { + BigDecimal p1,p2; + BigDecimal p3 = new BigDecimal("0"); + + for(int i=0;i Date: Fri, 13 May 2022 23:23:40 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E8=BE=93=E5=87=BA=E5=AE=8C=E6=88=90=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E4=B8=8B=E4=B8=80=E6=AC=A1=E8=A7=A6=E5=8F=91=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E6=8C=89=E9=94=AE=E4=BA=8B=E4=BB=B6=E6=97=B6=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E8=BE=93=E5=85=A5=E3=80=81=E8=BE=93=E5=87=BA=E6=A0=8F?= =?UTF-8?q?=E5=B9=B6=E6=98=BE=E7=A4=BA=E5=88=9A=E6=8C=89=E4=B8=8B=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E5=AD=97=EF=BC=88=E7=B1=BB=E4=BC=BCwin11=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 15 ++++++++++++++- src/java2022spring/MixedOperation.java | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index 56bc5f6..89dd17f 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -22,6 +22,8 @@ class Calculation extends JFrame{ private MixedOperation mix = new MixedOperation(); //混合运算 private GridBagLayout gridBagLayout = new GridBagLayout(); private DecimalFormat df = new DecimalFormat(); //对字符串进行三位一组分割 + + private int i = 0;//上一次输出后,下一次触发数字按键事件清空输入 //输入输出栏保存 private String input = new String(""); //保存输入的算式 @@ -49,8 +51,18 @@ class Calculation extends JFrame{ switch(str){ case "0":case "1":case "2":case "3":case "4":case "5"://四则运算直接转换为字符串储存 - case "6":case "7":case "8":case "9":case ".":case "+": + case "6":case "7":case "8":case "9": + //上一次输出后,下一次触发数字按键事件清空输入 + if(i==1) { + i=0; + input=str; + output = BigDecimal.ZERO; + break;} + case ".":case "+": case "-":case "*":case "/":case "(":case ")": + //上一次输出后,下一次触发数字按键事件清空输入 + if(i==1) + i=0; input+=str;//常用数字及运算符直接记录 break; @@ -84,6 +96,7 @@ class Calculation extends JFrame{ if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 input = output.toString(); calculateFlag = false;//结束此次运算 + i=1; } } //新增转换String成BigDecimal diff --git a/src/java2022spring/MixedOperation.java b/src/java2022spring/MixedOperation.java index 3dd202e..59dcdc2 100644 --- a/src/java2022spring/MixedOperation.java +++ b/src/java2022spring/MixedOperation.java @@ -8,7 +8,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class MixedOperation { - private final Pattern p = Pattern.compile("(^-|(?<=\\D)-)?[\\d.]+|[+\\-*\\/%!()√^]|log"); //用于比对运算符和参数的正则表达式确定优先级 + private final Pattern p = Pattern.compile("(^-|(?<=\\D)-)?[\\d.]+|[+\\-*\\/%!()]"); //用于比对运算符和参数的正则表达式确定优先级 private final HashMap prio = new HashMap(); //存放运算符对应的优先级,后续通过查HASH表来判断 private ArrayList eq1 = new ArrayList(); //保存算式的列表 private ArrayList eq2 = new ArrayList(); //保存算式的中间列表 @@ -98,7 +98,7 @@ public class MixedOperation { case "*":p3 = p2.multiply(p1);break; case "/": - p3 = p2.divide(p1,32,BigDecimal.ROUND_HALF_EVEN); + p3 = p2.divide(p1,32,BigDecimal.ROUND_HALF_EVEN);//指定四舍五入:ROUND_HALF_EVEN break; case "%":p3 = p2.remainder(p1);break; -- Gitee From 6f5d96415f3738eec59a3e1940898168befce4cb Mon Sep 17 00:00:00 2001 From: summercat Date: Sat, 14 May 2022 01:12:05 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=B0=8F=E6=95=B0=E6=9C=AB=E5=B0=BE=E9=9D=9E=E9=9B=B6=E6=9C=89?= =?UTF-8?q?=E6=95=88=E6=95=B0=E5=AD=97=E5=B9=B6=E4=B8=94=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E6=97=A0=E9=99=90=E5=BE=AA=E7=8E=AF=E5=B0=8F=E6=95=B0=E6=9C=AB?= =?UTF-8?q?=E5=B0=BE=E8=BF=87=E9=95=BF=E5=BD=B1=E5=93=8D=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BD=86=E6=B2=A1=E5=81=9A?= =?UTF-8?q?=E5=88=B0=E5=9B=9B=E8=88=8D=E4=BA=94=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index 89dd17f..f4ca7e5 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -7,7 +7,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.math.BigDecimal; - +import java.math.RoundingMode; import java.text.DecimalFormat; import javax.swing.JButton; @@ -94,7 +94,9 @@ class Calculation extends JFrame{ tOutput.setText(df.format(output).toString());//三位一组显示输出结果 if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 - input = output.toString(); + String o=new String(); + o=output.toString(); + input = QvWei(o).substring(0, 17);//防止无限循环小数末尾过长影响显示,但没做到四舍五入 calculateFlag = false;//结束此次运算 i=1; } @@ -105,18 +107,15 @@ class Calculation extends JFrame{ BigDecimal b = BigDecimal.valueOf(a); return b; } + //保留小数末尾非零有效数字 + public String QvWei(String s) { + while(s.charAt(s.length()-1)=='.'||s.charAt(s.length()-1)=='0') { + s = s.substring(0,s.length()-1); + } + return s; + } } - - - - - - - - - - //面板参数设置 tInput.setHorizontalAlignment(JTextField.RIGHT); //输入算式文本显示右对齐 tOutput.setHorizontalAlignment(JTextField.RIGHT); //结果文本显示右对齐 -- Gitee From c5a12bb6d1f5359d22c855fdfa485661b817bcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E7=8C=AB?= <10913093+h-summer-cat@user.noreply.gitee.com> Date: Wed, 18 May 2022 12:39:23 +0000 Subject: [PATCH 4/8] add LICENSE. --- LICENSE | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee58399 --- /dev/null +++ b/LICENSE @@ -0,0 +1,127 @@ + 鏈ㄥ叞瀹芥澗璁稿彲璇, 绗2鐗 + + 鏈ㄥ叞瀹芥澗璁稿彲璇侊紝 绗2鐗 + 2020骞1鏈 http://license.coscl.org.cn/MulanPSL2 + + + 鎮ㄥ鈥滆蒋浠垛濈殑澶嶅埗銆佷娇鐢ㄣ佷慨鏀瑰強鍒嗗彂鍙楁湪鍏板鏉捐鍙瘉锛岀2鐗堬紙鈥滄湰璁稿彲璇佲濓級鐨勫涓嬫潯娆剧殑绾︽潫锛 + + 0. 瀹氫箟 + + 鈥滆蒋浠垛濇槸鎸囩敱鈥滆础鐚濇瀯鎴愮殑璁稿彲鍦ㄢ滄湰璁稿彲璇佲濅笅鐨勭▼搴忓拰鐩稿叧鏂囨。鐨勯泦鍚堛 + + 鈥滆础鐚濇槸鎸囩敱浠讳竴鈥滆础鐚呪濊鍙湪鈥滄湰璁稿彲璇佲濅笅鐨勫彈鐗堟潈娉曚繚鎶ょ殑浣滃搧銆 + + 鈥滆础鐚呪濇槸鎸囧皢鍙楃増鏉冩硶淇濇姢鐨勪綔鍝佽鍙湪鈥滄湰璁稿彲璇佲濅笅鐨勮嚜鐒朵汉鎴栤滄硶浜哄疄浣撯濄 + + 鈥滄硶浜哄疄浣撯濇槸鎸囨彁浜よ础鐚殑鏈烘瀯鍙婂叾鈥滃叧鑱斿疄浣撯濄 + + 鈥滃叧鑱斿疄浣撯濇槸鎸囷紝瀵光滄湰璁稿彲璇佲濅笅鐨勮涓烘柟鑰岃█锛屾帶鍒躲佸彈鎺у埗鎴栦笌鍏跺叡鍚屽彈鎺у埗鐨勬満鏋勶紝姝ゅ鐨勬帶鍒舵槸鎸囨湁鍙楁帶鏂规垨鍏卞悓鍙楁帶鏂硅嚦灏50%鐩存帴鎴栭棿鎺ョ殑鎶曠エ鏉冦佽祫閲戞垨鍏朵粬鏈変环璇佸埜銆 + + 1. 鎺堜簣鐗堟潈璁稿彲 + + 姣忎釜鈥滆础鐚呪濇牴鎹滄湰璁稿彲璇佲濇巿浜堟偍姘镐箙鎬х殑銆佸叏鐞冩х殑銆佸厤璐圭殑銆侀潪鐙崰鐨勩佷笉鍙挙閿鐨勭増鏉冭鍙紝鎮ㄥ彲浠ュ鍒躲佷娇鐢ㄣ佷慨鏀广佸垎鍙戝叾鈥滆础鐚濓紝涓嶈淇敼涓庡惁銆 + + 2. 鎺堜簣涓撳埄璁稿彲 + + 姣忎釜鈥滆础鐚呪濇牴鎹滄湰璁稿彲璇佲濇巿浜堟偍姘镐箙鎬х殑銆佸叏鐞冩х殑銆佸厤璐圭殑銆侀潪鐙崰鐨勩佷笉鍙挙閿鐨勶紙鏍规嵁鏈潯瑙勫畾鎾ら攢闄ゅ锛変笓鍒╄鍙紝渚涙偍鍒堕犮佸鎵樺埗閫犮佷娇鐢ㄣ佽璇洪攢鍞侀攢鍞佽繘鍙e叾鈥滆础鐚濇垨浠ュ叾浠栨柟寮忚浆绉诲叾鈥滆础鐚濄傚墠杩颁笓鍒╄鍙粎闄愪簬鈥滆础鐚呪濈幇鍦ㄦ垨灏嗘潵鎷ユ湁鎴栨帶鍒剁殑鍏垛滆础鐚濇湰韬垨鍏垛滆础鐚濅笌璁稿彲鈥滆础鐚濇椂鐨勨滆蒋浠垛濈粨鍚堣屽皢蹇呯劧浼氫镜鐘殑涓撳埄鏉冨埄瑕佹眰锛屼笉鍖呮嫭瀵光滆础鐚濈殑淇敼鎴栧寘鍚滆础鐚濈殑鍏朵粬缁撳悎銆傚鏋滄偍鎴栨偍鐨勨滃叧鑱斿疄浣撯濈洿鎺ユ垨闂存帴鍦帮紝灏扁滆蒋浠垛濇垨鍏朵腑鐨勨滆础鐚濆浠讳綍浜哄彂璧蜂笓鍒╀镜鏉冭瘔璁硷紙鍖呮嫭鍙嶈瘔鎴栦氦鍙夎瘔璁硷級鎴栧叾浠栦笓鍒╃淮鏉冭鍔紝鎸囨帶鍏朵镜鐘笓鍒╂潈锛屽垯鈥滄湰璁稿彲璇佲濇巿浜堟偍瀵光滆蒋浠垛濈殑涓撳埄璁稿彲鑷偍鎻愯捣璇夎鎴栧彂璧风淮鏉冭鍔ㄤ箣鏃ョ粓姝€ + + 3. 鏃犲晢鏍囪鍙 + + 鈥滄湰璁稿彲璇佲濅笉鎻愪緵瀵光滆础鐚呪濈殑鍟嗗搧鍚嶇О銆佸晢鏍囥佹湇鍔℃爣蹇楁垨浜у搧鍚嶇О鐨勫晢鏍囪鍙紝浣嗘偍涓烘弧瓒崇4鏉¤瀹氱殑澹版槑涔夊姟鑰屽繀椤讳娇鐢ㄩ櫎澶栥 + + 4. 鍒嗗彂闄愬埗 + + 鎮ㄥ彲浠ュ湪浠讳綍濯掍粙涓皢鈥滆蒋浠垛濅互婧愮▼搴忓舰寮忔垨鍙墽琛屽舰寮忛噸鏂板垎鍙戯紝涓嶈淇敼涓庡惁锛屼絾鎮ㄥ繀椤诲悜鎺ユ敹鑰呮彁渚涒滄湰璁稿彲璇佲濈殑鍓湰锛屽苟淇濈暀鈥滆蒋浠垛濅腑鐨勭増鏉冦佸晢鏍囥佷笓鍒╁強鍏嶈矗澹版槑銆 + + 5. 鍏嶈矗澹版槑涓庤矗浠婚檺鍒 + + 鈥滆蒋浠垛濆強鍏朵腑鐨勨滆础鐚濆湪鎻愪緵鏃朵笉甯︿换浣曟槑绀烘垨榛樼ず鐨勬媴淇濄傚湪浠讳綍鎯呭喌涓嬶紝鈥滆础鐚呪濇垨鐗堟潈鎵鏈夎呬笉瀵逛换浣曚汉鍥犱娇鐢ㄢ滆蒋浠垛濇垨鍏朵腑鐨勨滆础鐚濊屽紩鍙戠殑浠讳綍鐩存帴鎴栭棿鎺ユ崯澶辨壙鎷呰矗浠伙紝涓嶈鍥犱綍绉嶅師鍥犲鑷存垨鑰呭熀浜庝綍绉嶆硶寰嬬悊璁猴紝鍗充娇鍏舵浘琚缓璁湁姝ょ鎹熷け鐨勫彲鑳芥с + + 6. 璇█ + 鈥滄湰璁稿彲璇佲濅互涓嫳鏂囧弻璇〃杩帮紝涓嫳鏂囩増鏈叿鏈夊悓绛夋硶寰嬫晥鍔涖傚鏋滀腑鑻辨枃鐗堟湰瀛樺湪浠讳綍鍐茬獊涓嶄竴鑷达紝浠ヤ腑鏂囩増涓哄噯銆 + + 鏉℃缁撴潫 + + 濡備綍灏嗘湪鍏板鏉捐鍙瘉锛岀2鐗堬紝搴旂敤鍒版偍鐨勮蒋浠 + + 濡傛灉鎮ㄥ笇鏈涘皢鏈ㄥ叞瀹芥澗璁稿彲璇侊紝绗2鐗堬紝搴旂敤鍒版偍鐨勬柊杞欢锛屼负浜嗘柟渚挎帴鏀惰呮煡闃咃紝寤鸿鎮ㄥ畬鎴愬涓嬩笁姝ワ細 + + 1锛 璇锋偍琛ュ厖濡備笅澹版槑涓殑绌虹櫧锛屽寘鎷蒋浠跺悕銆佽蒋浠剁殑棣栨鍙戣〃骞翠唤浠ュ強鎮ㄤ綔涓虹増鏉冧汉鐨勫悕瀛楋紱 + + 2锛 璇锋偍鍦ㄨ蒋浠跺寘鐨勪竴绾х洰褰曚笅鍒涘缓浠モ淟ICENSE鈥濅负鍚嶇殑鏂囦欢锛屽皢鏁翠釜璁稿彲璇佹枃鏈斁鍏ヨ鏂囦欢涓紱 + + 3锛 璇峰皢濡備笅澹版槑鏂囨湰鏀惧叆姣忎釜婧愭枃浠剁殑澶撮儴娉ㄩ噴涓 + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. + + + Mulan Permissive Software License锛孷ersion 2 + + Mulan Permissive Software License锛孷ersion 2 (Mulan PSL v2) + January 2020 http://license.coscl.org.cn/MulanPSL2 + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + + 0. Definition + + Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, 鈥榗ontrol鈥 means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT鈥橲 CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + 6. Language + + THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + + END OF THE TERMS AND CONDITIONS + + How to Apply the Mulan Permissive Software License锛孷ersion 2 (Mulan PSL v2) to Your Software + + To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + + ii Create a file named 鈥淟ICENSE鈥 which contains the whole context of this License in the first directory of your software package; + + iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. -- Gitee From 7d85b66f4af05ae52c5ae160d4948386c0ed3aa2 Mon Sep 17 00:00:00 2001 From: summercat Date: Sat, 21 May 2022 19:04:06 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=BA=95?= =?UTF-8?q?=E5=B1=82=E9=80=BB=E8=BE=91=EF=BC=8C=E6=8E=A5=E8=BF=91=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=AE=9E=E6=97=B6=E8=BF=90=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 138 ++++++++++++++++++------- src/java2022spring/MixedOperation.java | 6 +- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index f4ca7e5..9a45dbd 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -16,6 +16,7 @@ import javax.swing.JFrame; import javax.swing.JTextField; + class Calculation extends JFrame{ private GridBagConstraints gridBagConstraints = new GridBagConstraints();//实例化该对象用来对组件进行管理 @@ -23,12 +24,15 @@ class Calculation extends JFrame{ private GridBagLayout gridBagLayout = new GridBagLayout(); private DecimalFormat df = new DecimalFormat(); //对字符串进行三位一组分割 - private int i = 0;//上一次输出后,下一次触发数字按键事件清空输入 + private int i = 0;//记录算式有没有使用过等号 + private int d=0;//记录算式有没有使用过四则 //输入输出栏保存 private String input = new String(""); //保存输入的算式 private BigDecimal output = new BigDecimal("0"); //保存计算结果 - + + private String s="";//输出栏,单个实时运算 + private boolean calculateFlag = false; //如果上一步进行了计算,该标志为为true //设置控件位置及大小 @@ -49,64 +53,124 @@ class Calculation extends JFrame{ public void actionPerformed(ActionEvent e){ String str = e.getActionCommand(); + switch(str){ case "0":case "1":case "2":case "3":case "4":case "5"://四则运算直接转换为字符串储存 - case "6":case "7":case "8":case "9": - //上一次输出后,下一次触发数字按键事件清空输入 + case "6":case "7":case "8":case "9":case ".": + + if(d==1) { + s=str; + d=0; + } + else + s+=str; + break; + case "+":case "-":case "*":case "/": if(i==1) { + input=s+str; i=0; - input=str; - output = BigDecimal.ZERO; - break;} - case ".":case "+": - case "-":case "*":case "/":case "(":case ")": - //上一次输出后,下一次触发数字按键事件清空输入 - if(i==1) - i=0; - input+=str;//常用数字及运算符直接记录 + } + else + input+=s+str;//常用数字及运算符直接记录 + d=1; break; - + case "(": + if(d==1) { + input+=str; + } + break; + case ")": + if(d==0) + input+=s+str; + break; case "=": - output = mix.getMixedOperationRes(input); - calculateFlag = true; //该结果可以用于下次计算 - break; + input+=s+str; + if(input.contains("1")||input.contains("2")||input.contains("3") + ||input.contains("4")||input.contains("5")||input.contains("6") + ||input.contains("7")||input.contains("8")||input.contains("9")||input.contains("0")) { + if(input.lastIndexOf(')')==input.length()-3)//小括号结尾去掉末尾的s + input=input.substring(0,input.length()-(s.length()+1))+"="; + s = mix.getMixedOperationRes(input); + i=1; + calculateFlag = true; //该结果可以用于下次计算 + break; + } + else + input=""; +// input=input.substring(0, input.length()-s.length()-1); + break; + case "x^2": //x的平方,直接计算结果 - BigDecimal b=StoB(input); - output = b.multiply(b); - calculateFlag = true; //该结果可以用于下次计算 + + double b=StoD(s); + s = ""+b*b; + calculateFlag = false; //该结果可以用于下次计算 break; case "C": //清空输入输出 input = ""; //输入清空 output = BigDecimal.ZERO; //输出显示"0" + s=""; break; case "←": //退格,清除一位输入 - if(!input.isEmpty()){ - input = input.substring(0,input.length()-1);//去掉末尾字符 由0~n变成0~n-1 + if(!s.isEmpty()){ + s = s.substring(0,s.length()-1);//去掉末尾字符 由0~n变成0~n-1 } + else + if(!input.isEmpty()) + input = input.substring(0, input.length()-1); break; + case "+/-"://取反 + StringBuffer h=new StringBuffer(input); + h.insert(0, "-"); + input=h.toString(); + break; default:break; } + tInput.setText(input);//显示输入栏 - tOutput.setText(df.format(output).toString());//三位一组显示输出结果 + if(s.isEmpty()) { + tOutput.setText(""); + } + else{ + if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 + String o=new String(); + o=s; + if(o.indexOf('.')==-1) { + input=s; + } + else { +// input = QvWei(o).substring(0, 17);//防止无限不循环小数末尾过长影响显示,但没做到四舍五入 + s=QvWei(o); + if(s.length()>=17) { + input=s.substring(0,17); + s=input; + } + else + input=s; + } + } + tOutput.setText(s); + } +// tOutput.setText(df.format(Double.parseDouble(s)).toString());//三位一组显示输出结果 - if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 - String o=new String(); - o=output.toString(); - input = QvWei(o).substring(0, 17);//防止无限循环小数末尾过长影响显示,但没做到四舍五入 calculateFlag = false;//结束此次运算 - i=1; + if(s.length()>20) { + tInput.setText("不可以,要坏掉了~"); + tOutput.setText(""); + input = ""; //输入清空 + output = BigDecimal.ZERO; //输出显示"0" + s=""; } - } - //新增转换String成BigDecimal - public BigDecimal StoB(String s) { - Double a = Double.parseDouble(s); - BigDecimal b = BigDecimal.valueOf(a); - return b; - } + } + //新增转换String成double + public double StoD(String s) { + Double a = Double.parseDouble(s); + return a; + } //保留小数末尾非零有效数字 public String QvWei(String s) { while(s.charAt(s.length()-1)=='.'||s.charAt(s.length()-1)=='0') { @@ -120,10 +184,11 @@ class Calculation extends JFrame{ tInput.setHorizontalAlignment(JTextField.RIGHT); //输入算式文本显示右对齐 tOutput.setHorizontalAlignment(JTextField.RIGHT); //结果文本显示右对齐 - this.setSize(277,365); //界面大小 + this.setSize(260,360); //界面大小 this.setLocationRelativeTo(null);//窗口显示在中央 this.setDefaultCloseOperation(3); this.setResizable(false);//不可改变窗口大小 + this.setLayout(gridBagLayout); //窗体对象设置为GridBagLayout布局 gridBagConstraints.fill = GridBagConstraints.BOTH; //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况 @@ -192,6 +257,7 @@ class Calculation extends JFrame{ bC.addActionListener(new Function()); //清除输入输出 bTuige.addActionListener(new Function()); //退格 + bNnegation.addActionListener(new Function());//取反 this.setVisible(true); //窗体可见 } diff --git a/src/java2022spring/MixedOperation.java b/src/java2022spring/MixedOperation.java index 59dcdc2..3f7f4a0 100644 --- a/src/java2022spring/MixedOperation.java +++ b/src/java2022spring/MixedOperation.java @@ -25,8 +25,8 @@ public class MixedOperation { prio.put("%",2); //取余优先级为2 } - //运算前进行一些初始化操作,清空所用的列表和堆栈 - public BigDecimal getMixedOperationRes(String s){ + //运算前进行一些初始化操作,清空所用的列表和堆栈,并传入input + public String getMixedOperationRes(String s){ eq1.clear(); eq2.clear(); while(!stk.empty()){//循环移除栈空间中的参数 @@ -34,7 +34,7 @@ public class MixedOperation { } pretreatment(s); Infix2Postfix(); - return new BigDecimal(postfixCalculate()); + return new String(postfixCalculate()); } //预处理,将运算参数按照“运算符”和“数字”分别提取出来保存为单独一个字符串 -- Gitee From 2528ef7cd3f18c92efaa47de5ad8dd8468fb0152 Mon Sep 17 00:00:00 2001 From: summercat Date: Mon, 23 May 2022 10:39:00 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=89=E8=A7=92?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E3=80=81=E9=98=B6=E4=B9=98=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E8=BE=93=E5=85=A5=E3=80=81=E7=AD=89=E5=8F=B7=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 241 ++++++++++++++++++++-------- 1 file changed, 170 insertions(+), 71 deletions(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index 9a45dbd..bf7dc2f 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -2,7 +2,8 @@ package java2022spring; import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; +import java.awt.GridBagLayout; +import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -24,23 +25,29 @@ class Calculation extends JFrame{ private GridBagLayout gridBagLayout = new GridBagLayout(); private DecimalFormat df = new DecimalFormat(); //对字符串进行三位一组分割 - private int i = 0;//记录算式有没有使用过等号 - private int d=0;//记录算式有没有使用过四则 - + private int iEqual = 0;//记录算式有没有使用过等号 + private int iOperation=0;//记录算式有没有使用过四则 + private int iEnd = 0;// 等号运算结束,下次触发数字清空显示栏 + private int iKuohao = 0;//左右括号匹配,限制单一右括号出现 + //输入输出栏保存 - private String input = new String(""); //保存输入的算式 - private BigDecimal output = new BigDecimal("0"); //保存计算结果 + private String history = new String(""); //保存输入的算式 +// private BigDecimal output = new BigDecimal("0"); //保存计算结果 - private String s="";//输出栏,单个实时运算 + private String now="";//输出栏,单个实时运算 private boolean calculateFlag = false; //如果上一步进行了计算,该标志为为true - + private boolean _2ndFlag = false; //记录当前切换到哪组按键 + //设置控件位置及大小 private void setControl(JComponent e, int x, int y, int width, int height){ gridBagConstraints.gridx = x; gridBagConstraints.gridy = y; gridBagConstraints.gridwidth = width; gridBagConstraints.gridheight = height; + gridBagConstraints.weightx=1; + gridBagConstraints.weighty=1; + gridBagConstraints.insets=new Insets(2,2,2,2); gridBagLayout.setConstraints(e, gridBagConstraints); this.add(e); } @@ -52,147 +59,230 @@ class Calculation extends JFrame{ public void actionPerformed(ActionEvent e){ String str = e.getActionCommand(); + double dTool = 0;//工具数 switch(str){ case "0":case "1":case "2":case "3":case "4":case "5"://四则运算直接转换为字符串储存 case "6":case "7":case "8":case "9":case ".": - if(d==1) { - s=str; - d=0; + if(iEnd==1) { + history=""; + now=""; + iEnd=0; + } + if(iOperation==1) { + now=str; + iOperation=0; } else - s+=str; + now+=str; break; case "+":case "-":case "*":case "/": - if(i==1) { - input=s+str; - i=0; + if(iEqual==1) { + history=now+str; + iEqual=0; } else - input+=s+str;//常用数字及运算符直接记录 - d=1; + history+=now+str;//常用数字及运算符直接记录 + iOperation=1; break; + case "(": - if(d==1) { - input+=str; + if(iOperation==1) { + history+=str; + iKuohao=1; } break; case ")": - if(d==0) - input+=s+str; + if(iOperation==0) + history+=now+str; + iKuohao=0; break; case "=": - input+=s+str; - if(input.contains("1")||input.contains("2")||input.contains("3") - ||input.contains("4")||input.contains("5")||input.contains("6") - ||input.contains("7")||input.contains("8")||input.contains("9")||input.contains("0")) { - if(input.lastIndexOf(')')==input.length()-3)//小括号结尾去掉末尾的s - input=input.substring(0,input.length()-(s.length()+1))+"="; - s = mix.getMixedOperationRes(input); - i=1; - calculateFlag = true; //该结果可以用于下次计算 - break; - } + + if(iKuohao==1) + history+=now+")"+str; else - input=""; -// input=input.substring(0, input.length()-s.length()-1); + history+=now+str; + equal(); break; case "x^2": //x的平方,直接计算结果 - double b=StoD(s); - s = ""+b*b; - calculateFlag = false; //该结果可以用于下次计算 + double b=StoD(now); + now = ""+b*b; + calculateFlag = false; break; case "C": //清空输入输出 - input = ""; //输入清空 - output = BigDecimal.ZERO; //输出显示"0" - s=""; + history = ""; //输入清空 +// output = BigDecimal.ZERO; //输出显示"0" + now=""; break; case "←": //退格,清除一位输入 - if(!s.isEmpty()){ - s = s.substring(0,s.length()-1);//去掉末尾字符 由0~n变成0~n-1 + if(!now.isEmpty()){ + now = now.substring(0,now.length()-1);//去掉末尾字符 由0~n变成0~n-1 } else - if(!input.isEmpty()) - input = input.substring(0, input.length()-1); + if(!history.isEmpty()) + history = history.substring(0, history.length()-1); break; case "+/-"://取反 - StringBuffer h=new StringBuffer(input); + StringBuffer h=new StringBuffer(now); h.insert(0, "-"); - input=h.toString(); + now=h.toString(); break; - + case"1/x"://倒数 + dTool=StoD(now); + now = ""+1/dTool; + case"n!"://阶乘 + factorial(); + break; + + + case"2nd"://切换模式 + if(_2ndFlag){ + bSin.setText("sin"); //正弦 + bCos.setText("cos"); //余弦 + bTan.setText("tan"); //正切 + _2ndFlag=false; + } + else { + bSin.setText("asin"); //正弦 + bCos.setText("acos"); //余弦 + bTan.setText("atan"); //正切 + _2ndFlag=true; + } + break; + + case"sin": + dTool=Math.toRadians(StoD(now)); + now=""+Math.sin(dTool); + break; + case"cos": + dTool=Math.toRadians(StoD(now)); + now=""+Math.cos(dTool); + break; + case"tan": + dTool=Math.toRadians(StoD(now)); + now=""+Math.tan(dTool); + break; + case"asin": + dTool=Math.toRadians(StoD(now)); + now=""+Math.asin(dTool); + break; + case"acos": + dTool=Math.toRadians(StoD(now)); + now=""+Math.acos(dTool); + break; + case"atan": + dTool=Math.toRadians(StoD(now)); + now=""+Math.atan(dTool); + break; + default:break; } - tInput.setText(input);//显示输入栏 - if(s.isEmpty()) { + tInput.setText(history);//显示输入栏 + if(now.isEmpty()) { tOutput.setText(""); } else{ if(calculateFlag){ //上一步进行了计算,把计算结果赋给input进行连续运算 String o=new String(); - o=s; + o=now; if(o.indexOf('.')==-1) { - input=s; + history=now; } else { // input = QvWei(o).substring(0, 17);//防止无限不循环小数末尾过长影响显示,但没做到四舍五入 - s=QvWei(o); - if(s.length()>=17) { - input=s.substring(0,17); - s=input; + now=QvWei(o); + if(now.length()>=17) { + history=now.substring(0,17); + now=history; } else - input=s; + history=now; } } - tOutput.setText(s); + if(now.isEmpty()) + tOutput.setText(now); + else + tOutput.setText(df.format(Double.parseDouble(now)).toString());//三位一组显示输出结果 } // tOutput.setText(df.format(Double.parseDouble(s)).toString());//三位一组显示输出结果 calculateFlag = false;//结束此次运算 - if(s.length()>20) { + if(now.length()>20) { tInput.setText("不可以,要坏掉了~"); tOutput.setText(""); - input = ""; //输入清空 - output = BigDecimal.ZERO; //输出显示"0" - s=""; + history = ""; //输入清空 +// output = BigDecimal.ZERO; //输出显示"0" + now=""; } } - //新增转换String成double - public double StoD(String s) { + + //新增转换String成double + private double StoD(String s) { Double a = Double.parseDouble(s); return a; } //保留小数末尾非零有效数字 - public String QvWei(String s) { + private String QvWei(String s) { while(s.charAt(s.length()-1)=='.'||s.charAt(s.length()-1)=='0') { s = s.substring(0,s.length()-1); } return s; } + //阶乘 + private void factorial() { + double i=0; + i=StoD(QvWei(now)); + if(QvWei(now).indexOf('.')==-1) { + double sum=1; + System.out.println(i); + for(int s=1;s<=i;s++) { + sum*=s; + } + now=""+sum; + } + else + return; + } + //分情况处理等号 + private void equal() { + if(history.contains("1")||history.contains("2")||history.contains("3") + ||history.contains("4")||history.contains("5")||history.contains("6") + ||history.contains("7")||history.contains("8")||history.contains("9")||history.contains("0")) { + if(history.lastIndexOf(')')==history.length()-3)//小括号结尾去掉末尾的s + history=history.substring(0,history.length()-(now.length()+1))+"="; + now = mix.getMixedOperationRes(history); + iEqual=1; + iEnd=1; + calculateFlag = true; //该结果可以用于下次计算 + } + else + history=""; + } } //面板参数设置 tInput.setHorizontalAlignment(JTextField.RIGHT); //输入算式文本显示右对齐 tOutput.setHorizontalAlignment(JTextField.RIGHT); //结果文本显示右对齐 - this.setSize(260,360); //界面大小 + this.setSize(330,550); //界面大小 this.setLocationRelativeTo(null);//窗口显示在中央 this.setDefaultCloseOperation(3); - this.setResizable(false);//不可改变窗口大小 + this.setResizable(true);//不可改变窗口大小 this.setLayout(gridBagLayout); //窗体对象设置为GridBagLayout布局 gridBagConstraints.fill = GridBagConstraints.BOTH; //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况 //按键 +// setControl(bSwitch,0,0,2,1); //第1行 setControl(tInput,0,1,5,1); //算式显示 //第2行 @@ -201,10 +291,10 @@ class Calculation extends JFrame{ setControl(bSin,1,3,1,1); //sin setControl(bCos,2,3,1,1); //cos setControl(bTan,3,3,1,1); //tan - setControl(bSquare,4,3,1,1);//平方 + setControl(b2nd,4,3,1,1);//平方 第二模式 //第4行 setControl(bDaoshu,1,4,1,1); //倒数 - setControl(bQvyv,2,4,1,1); //取余 + setControl(bSquare,2,4,1,1); //平方 setControl(bC,3,4,1,1); //清空输入 setControl(bTuige,4,4,1,1); //清除最后一位输入 //第5行 @@ -256,25 +346,34 @@ class Calculation extends JFrame{ bSquare.addActionListener(new Function());//平方 bC.addActionListener(new Function()); //清除输入输出 bTuige.addActionListener(new Function()); //退格 - + bDaoshu.addActionListener(new Function());//倒数 bNnegation.addActionListener(new Function());//取反 + bFactorial.addActionListener(new Function());//阶乘 + + b2nd.addActionListener(new Function()); + + bSin.addActionListener(new Function()); + bCos.addActionListener(new Function()); + bTan.addActionListener(new Function()); + this.setVisible(true); //窗体可见 } //面板按键名称声明 //为方便调试功能放在后面 +// private JButton bSwitch = new JButton("更多"); //第1行 private JTextField tInput = new JTextField(""); //显示输入算式 //第2行 private JTextField tOutput = new JTextField(""); //显示输出结果 //第3行 - private JButton bSquare = new JButton("x^2"); //平方 + private JButton b2nd = new JButton("2nd"); //第二模式 private JButton bSin = new JButton("sin"); //正弦函数 private JButton bCos = new JButton("cos"); //余弦函数 private JButton bTan = new JButton("tan"); //正切函数 //第4行 private JButton bDaoshu = new JButton("1/x"); //倒数 - private JButton bQvyv = new JButton("%"); //取余 + private JButton bSquare = new JButton("x^2"); //平方 private JButton bC = new JButton("C"); //清空输入输出 private JButton bTuige = new JButton("←"); //退格,清除输入的最后一位 //第5行 -- Gitee From 98a46b024990108d123f225c5be078c0a6bf7675 Mon Sep 17 00:00:00 2001 From: summercat Date: Mon, 23 May 2022 11:01:14 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=94=B9=E5=8F=98=E5=BA=95=E5=B1=82?= =?UTF-8?q?=E7=AE=97=E6=B3=95=EF=BC=8C=E6=9B=B4=E6=8E=A5=E8=BF=91windows?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=20=E6=96=B0=E5=A2=9E=E4=B8=89?= =?UTF-8?q?=E8=A7=92=E5=87=BD=E6=95=B0=EF=BC=8C=E9=98=B6=E4=B9=98=EF=BC=8C?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=E7=AD=89=E5=8A=9F=E8=83=BD=E3=80=82=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E6=98=BE=E7=A4=BA=EF=BC=8C=E7=AD=89=E5=8F=B7=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 30 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index bf7dc2f..4cad522 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -16,8 +16,6 @@ import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JTextField; - - class Calculation extends JFrame{ private GridBagConstraints gridBagConstraints = new GridBagConstraints();//实例化该对象用来对组件进行管理 @@ -109,6 +107,7 @@ class Calculation extends JFrame{ break; + case "x^2": //x的平方,直接计算结果 double b=StoD(now); @@ -141,6 +140,7 @@ class Calculation extends JFrame{ case"n!"://阶乘 factorial(); break; + case"2nd"://切换模式 @@ -186,7 +186,20 @@ class Calculation extends JFrame{ default:break; } - tInput.setText(history);//显示输入栏 + output(); + calculateFlag = false;//结束此次运算 + if(now.length()>20) { + tInput.setText("不可以,要坏掉了~"); + tOutput.setText(""); + history = ""; //清空输入 + now=""; + } + } + + + + private void output() { + tInput.setText(history);//显示输入栏 if(now.isEmpty()) { tOutput.setText(""); } @@ -215,16 +228,7 @@ class Calculation extends JFrame{ } // tOutput.setText(df.format(Double.parseDouble(s)).toString());//三位一组显示输出结果 - calculateFlag = false;//结束此次运算 - if(now.length()>20) { - tInput.setText("不可以,要坏掉了~"); - tOutput.setText(""); - history = ""; //输入清空 -// output = BigDecimal.ZERO; //输出显示"0" - now=""; - } - } - + } //新增转换String成double private double StoD(String s) { Double a = Double.parseDouble(s); -- Gitee From fabacfcf1d90c96b8ec19ad971934d0beae0b51f Mon Sep 17 00:00:00 2001 From: summercat Date: Mon, 23 May 2022 11:06:28 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=94=B9=E5=8F=98=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E5=96=84=E7=AD=89=E5=8F=B7=E8=BF=90=E7=AE=97?= =?UTF-8?q?=EF=BC=8C=E6=8B=AC=E5=8F=B7=E8=BF=90=E7=AE=97=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=B8=89=E8=A7=92=E5=87=BD=E6=95=B0=E3=80=81=E5=80=92?= =?UTF-8?q?=E6=95=B0=E3=80=81=E9=98=B6=E4=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculation.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java2022spring/Calculation.java b/src/java2022spring/Calculation.java index 4cad522..9cd4a40 100644 --- a/src/java2022spring/Calculation.java +++ b/src/java2022spring/Calculation.java @@ -226,7 +226,6 @@ class Calculation extends JFrame{ else tOutput.setText(df.format(Double.parseDouble(now)).toString());//三位一组显示输出结果 } -// tOutput.setText(df.format(Double.parseDouble(s)).toString());//三位一组显示输出结果 } //新增转换String成double -- Gitee