From a0125dad22324560dbc6c15c63b0b42ac0b9ef8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Wed, 2 Jun 2021 11:22:07 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E4=BA=86=E5=B8=83=E5=B1=80?= =?UTF-8?q?=EF=BC=8C=E7=AA=97=E5=8F=A3=EF=BC=8C=E6=98=BE=E7=A4=BA=E5=B1=8F?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Calculator diff --git a/src/Calculator b/src/Calculator new file mode 100644 index 0000000..9797127 --- /dev/null +++ b/src/Calculator @@ -0,0 +1,74 @@ +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.math.BigDecimal; + +import javax.swing.*; +import javax.swing.border.Border; +import javax.swing.border.LineBorder; + +public class Calculator extends JFrame{ + private JTextField display1,display2,display3; + private JButton num[],operate[]; + private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"}; + //定义了只能在该类内部才能访问的名叫name的字符串变量 + private operateNum t1=new operateNum(),t2=new operateNum();//操作数1,操作数2 + private storageField storage=new storageField();//存储区 + private String action="t1";//表示要操作的对象 "t1"操作第一个操作数,"t2"操作第二个操作数 + private String sign="";//运算符,默认为空 + private String s1,s2; + private boolean Disable=false; + + public Calculator() + { + super("计算器"); + this.setLayout(null); + //设置布局管理器为默认值 + this.setSize(228, 324); + //设置显示框架的宽和高 + this.setResizable(false); + //设置窗口固定大小不可改变 + JPanel jp=new JPanel(null); + //设置布局管理器为空,就可以自己手动的设置组件的坐标位置和大小 + jp.setBounds(10,15,getWidth()-24,getHeight()); + //设置窗口坐标,宽和高 + jp.setBackground(new Color(228,228,228)); + this.getContentPane().setBackground(new Color(238,228,228)); + //设置窗口背景色 + + /**显示屏**/ + display1=new JTextField(""); + display2=new JTextField("0"); + display3=new JTextField(""); + //新建文本框 + display1.setEnabled(false); + display2.setEnabled(false); + display3.setEnabled(false); + //设置控件可否使用 + display1.setBounds(0, 0, 204, 26); + display2.setBounds(20, 20, 184, 34); + display3.setBounds(0, 20, 20, 34); + //设置按钮的位置 + display1.setHorizontalAlignment(JLabel.RIGHT); + display2.setHorizontalAlignment(JLabel.RIGHT); + display3.setHorizontalAlignment(JLabel.CENTER); + //左右对齐属性设置 + display1.setFont(new Font("宋体",Font.PLAIN,12)); + display2.setFont(new Font("宋体",Font.BOLD,20)); + display3.setFont(new Font("宋体",Font.PLAIN,20)); + //设置字体的风格 + display1.setDisabledTextColor(Color.BLACK); + display2.setDisabledTextColor(Color.BLACK); + display3.setDisabledTextColor(Color.BLACK); + //设置字体的颜色 + display1.setBorder(new LineBorder(new Color(242,247,252))); + display2.setBorder(new LineBorder(new Color(242,247,252))); + display3.setBorder(new LineBorder(new Color(242,247,252))); + //设置线条边框 + display1.setBackground(new Color(228,228,252)); + display2.setBackground(new Color(228,228,252)); + display3.setBackground(new Color(228,228,252)); + //设置按钮背景色 + \ No newline at end of file -- Gitee From 0df5bf7e5656953db181e09768df511e98b16fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Wed, 2 Jun 2021 11:26:30 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E4=BA=86=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 112 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/src/Calculator b/src/Calculator index 9797127..7fab577 100644 --- a/src/Calculator +++ b/src/Calculator @@ -71,4 +71,114 @@ public class Calculator extends JFrame{ display2.setBackground(new Color(228,228,252)); display3.setBackground(new Color(228,228,252)); //设置按钮背景色 - \ No newline at end of file + + /**按钮 35px*29px 28个***/ + int i; + operate=new JButton[28]; + + /**前24个**/ + for(i=0;i<24;i++) + { + operate[i]=new JButton(name[i]); + operate[i].setMargin(new java.awt.Insets(0,0,0,0)); + operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35, 29); + jp.add(operate[i]); + } + + /***=号***/ + operate[i]=new JButton(name[i]); + operate[i].setMargin(new java.awt.Insets(0,0,0,0)); + operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35, 29*2+5); + jp.add(operate[i]); + i++; + + /***0号***/ + operate[i]=new JButton(name[i]); + operate[i].setMargin(new java.awt.Insets(0,0,0,0)); + operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35*2+7, 29); + jp.add(operate[i]); + + /**前24个**/ + for(i=i+1;i Date: Wed, 2 Jun 2021 19:12:54 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=AD=98=E5=82=A8=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/Calculator b/src/Calculator index 7fab577..360215d 100644 --- a/src/Calculator +++ b/src/Calculator @@ -1,3 +1,5 @@ +package java2020spring; + import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; @@ -181,4 +183,52 @@ public class Calculator extends JFrame{ { ((JButton)e.getSource()).setBackground(new Color(233,240,247)); } + } + + /* + * MC 取消存储区,清空存储区数据 + */ + public void mc() + { + storage.storageNum=0; + storage.storageMode=false; + display3.setText(null); + } + + /* + * MR 读取存储器存储的数据 + */ + public void mr() + { + t1.value=storage.storageNum; + s2=""+t1.value; + if(t1.value==Math.floor(t1.value)) + s2=""+(int)t1.value; + display2.setText(s2); + t1.clear=true; + } + + /* + * MS 保存数据到存储器 + */ + public void ms() + { + storage.storageNum=t1.value; + display3.setText("M");//屏幕左下角显示M标志 + } + + /* + * M+ 已经储存的数加上当前计算结果并将和存入存储器 + */ + public void mAdd() + { + storage.storageNum=storage.storageNum+t1.value; + } + + /* + * M- 已经储存的数减去当前计算结果并将差存入存储器 + */ + public void mCut() + { + storage.storageNum=storage.storageNum-t1.value; } \ No newline at end of file -- Gitee From 007bc342873067030676944af7396f8720bc9501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Wed, 2 Jun 2021 19:13:23 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E6=93=8D=E4=BD=9C=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/operateNum | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/operateNum diff --git a/src/operateNum b/src/operateNum new file mode 100644 index 0000000..673245f --- /dev/null +++ b/src/operateNum @@ -0,0 +1,33 @@ +package java2020spring; + +public class operateNum {//操作数类 + public double value;//操作数的实际值 + public int dotWei;//操作数如果是小数,记录小数点后的位数 + public String invertedString;//1/x字符串 + public String sqrtedString;//sqrt(x)字符串 + public boolean isSqrted,isInverted;//做标记是否√,1/x过,用于后续判断 + public boolean clear;//clear为真表示是否重置操作数 + public boolean isFloat;//isFloat为真,表示操作数是小数 + public operateNum() + { + value=0; + dotWei=0; + sqrtedString=null; + invertedString=null; + isSqrted=false; + isInverted=false; + clear=false; + isFloat=false; + } + public void reset()//重置操作数 + { + value=0; + dotWei=0; + sqrtedString=null; + invertedString=null; + isSqrted=false; + isInverted=false; + clear=false; + isFloat=false; + } +} -- Gitee From 80d6ee09b99f136ed7a8b69f42eb3b4a84e85e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Wed, 2 Jun 2021 19:13:40 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=AD=98=E5=82=A8=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/storageField | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/storageField diff --git a/src/storageField b/src/storageField new file mode 100644 index 0000000..90c2fbe --- /dev/null +++ b/src/storageField @@ -0,0 +1,11 @@ +package java2020spring; + +public class storageField {//存储区 + public double storageNum;//存储区存储的数据 + public boolean storageMode;//是否启动存储区 + public storageField() + { + storageNum=0; + storageMode=false; + } +} \ No newline at end of file -- Gitee From c537547548c4794df38d241f8f537a1aad44dbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Wed, 2 Jun 2021 19:17:19 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E2=86=90=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E5=B0=BE=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Calculator b/src/Calculator index 360215d..a11126e 100644 --- a/src/Calculator +++ b/src/Calculator @@ -231,4 +231,44 @@ public class Calculator extends JFrame{ public void mCut() { storage.storageNum=storage.storageNum-t1.value; - } \ No newline at end of file + } + + /* + * ← 输入的数去掉尾数 + * + */ + public void cutEnd() + { + if(action=="t1"&&t1.value!=0)//表示对t1进行操作 + { + if(t1.isFloat==false)//如果t1为整数 + { + t1.value=(int)t1.value/10; + s2=""+(int)t1.value; + } + else{//如果t1为小数 + BigDecimal bd=new BigDecimal(t1.value); + t1.value=bd.setScale(--t1.dotWei,BigDecimal.ROUND_DOWN).doubleValue(); + s2=""+t1.value; + if(t1.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 + t1.isFloat=false; + } + } + else if(action=="t2"&&t2.value!=0)//表示对t2进行操作 + { + if(t2.isFloat==false)//如果t2为整数 + { + t2.value=(int)t2.value/10; + s2=""+(int)t2.value; + } + else{//如果t2为小数 + BigDecimal bd=new BigDecimal(t2.value); + t2.value=bd.setScale(--t2.dotWei,BigDecimal.ROUND_DOWN ).doubleValue(); + s2=""+t2.value; + if(t2.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 + t2.isFloat=false; + } + } + display2.setText(s2);//输出修改后的操作数 + } + \ No newline at end of file -- Gitee From 6a8c3d9f33ff208ba2e8670e8ac1fd0edb9ee0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 3 Jun 2021 11:01:28 +0800 Subject: [PATCH 07/21] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AECE=EF=BC=8CC=EF=BC=8C=C2=B1?= =?UTF-8?q?=EF=BC=8C=E6=A0=B9=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 74 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Calculator b/src/Calculator index a11126e..c9f9f5a 100644 --- a/src/Calculator +++ b/src/Calculator @@ -271,4 +271,76 @@ public class Calculator extends JFrame{ } display2.setText(s2);//输出修改后的操作数 } - \ No newline at end of file + + /* + * CE 清空当前操作数操作数 + */ + public void cutNum() + { + if(action=="t1") + t1.reset(); + else if(action=="t2") + t2.reset(); + display2.setText("0");//初始化显示屏2 + } + + /* + * C 归零 重置计算器 + */ + public void clear() + { + t1.reset(); + t2.reset(); + //初始化数据成员 + action="t1"; + sign=""; + //初始化显示屏 + display1.setText(""); + display2.setText("0"); + } + + /* + * ± 正负号 + */ + public void revolt() + { + if(action=="t1") + { + t1.value=-t1.value; + s2=""+t1.value; + } + else if(action=="t2") + { + t2.value=-t2.value; + s2=""+t2.value; + } + display2.setText(s2); + } + + /* + * √ 根号 + */ + public void sqrt() + { + double x;//临时变量 + if(action=="t1") + { + t1.sqrtedString="sqrt("+t1.value+")"; + t1.value=Math.sqrt(t1.value); + t1.isSqrted=true; + x=t1.value; + } + else + { + t2.sqrtedString="sqrt("+t2.value+")"; + t2.value=Math.sqrt(t2.value); + t2.isSqrted=true; + x=t2.value; + } + s2=x+""; + if(x==Math.floor(x))//如果x为整数 + { + s2=(int)x+"";//则将浮点数x先转化为int再转化成字符串 + } + display2.setText(s2); + } \ No newline at end of file -- Gitee From 9775f7414d24bbe4374ac16d1fc0c8fc0ed41e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 18:53:36 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=8F=8C=E7=B2=BE?= =?UTF-8?q?=E6=B5=AE=E7=82=B9=E6=95=B0=E3=80=811/x=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/Calculator b/src/Calculator index c9f9f5a..9f67056 100644 --- a/src/Calculator +++ b/src/Calculator @@ -343,4 +343,109 @@ public class Calculator extends JFrame{ s2=(int)x+"";//则将浮点数x先转化为int再转化成字符串 } display2.setText(s2); + } + + /* + * 按下数字键 + */ + public void read(int value) + { + d2.setFont(new Font("宋体",Font.BOLD,20));//默认字体大小 + d2.setText(null);//清屏 + if(t1.clear==true) + t1.reset(); + if(t2.clear==true) + t2.reset(); + if(action=="t1")//表示输数据给t1 + { + + if(t1.isFloat==true)//若t1为浮点数 + { + int i=1; + double num=value; + ++t1.dotWei; + while(i<=t1.dotWei) + { + num*=0.1; + i++; + } + t1.value=t1.value+num;//将新的小数点位添加到操作数t1中 + //因为双精度浮点数 其精度比较高,而我们只需取它的t1.dotWei保存 + t1.value=Double.parseDouble(String.format("%."+t1.dotWei+"f",t1.value)); + //因为双精度浮点数 其精度比较高,而我们只需取它的t1.dotWei显示在屏幕上 + d2.setText(String.format("%."+t1.dotWei+"f", t1.value)); + } + else//t1为整数 + { + t1.value=t1.value*10+value;//将新的整数位加倒t1中 + d2.setText((int)t1.value+"");//屏幕输出t1的值 + } + } + else if(action=="t2")//表示输数据给t2 + { + if(t2.isFloat==true)//若t2为浮点数 + { + int i=1; + double num=value; + ++t2.dotWei; + while(i<=t2.dotWei) + { + num*=0.1; + i++; + } + t2.value=t2.value+num;//将新的小数点位添加到操作数t2中 + //因为双精度浮点数 其精度比较高,而我们只需取它的t2.dotWei保存 + t2.value=Double.parseDouble(String.format("%."+t2.dotWei+"f",t2.value)); + //因为双精度浮点数 其精度比较高,而我们只需取它的t2.dotWei显示在屏幕上 + d2.setText(String.format("%."+t2.dotWei+"f", t2.value)); + } + else//t2为整数 + { + t2.value=t2.value*10+value; + d2.setText((int)t2.value+""); + } + + } + } + public void divide() + { + run("/"); + } + public void mo() + { + run("%"); + } + public void mul() + { + run("*"); + } + + /* + * 1/x + */ + public void inverted() + { + double num; + String str; + if(action=="t1") + { + t1.invertedString="1/"+t1.value;//1/x形式字符串 + t1.value=1/t1.value; + t1.isInverted=true; + num=t1.value; + } + else{ + t2.invertedString="1/"+t2.value;//1/x形式字符串 + t2.value=1/t2.value; + t1.isInverted=true; + num=t2.value; + } + str=num+""; + if(str.length()>=16)//计算器屏幕所能显示数据的最大长度 + { + d2.setFont(new Font("宋体",Font.BOLD,14));//缩小字体输出 + d2.setText(str.substring(0, 16)); + } + else + d2.setText(str); } \ No newline at end of file -- Gitee From 854231620a9cc3680cefc88b64009ceffb766255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 19:57:08 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E2=80=9C+=20-=20*=20/?= =?UTF-8?q?=20%=E2=80=9D=E8=AE=A1=E7=AE=97=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 139 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/src/Calculator b/src/Calculator index 9f67056..4f794f1 100644 --- a/src/Calculator +++ b/src/Calculator @@ -448,4 +448,143 @@ public class Calculator extends JFrame{ } else d2.setText(str); + } + public void cut() + { + run("-"); + } + public void add() + { + run("+"); + } + public void sum() + { + d2.setFont(new Font("宋体",Font.BOLD,20)); + int a1=t1.dotWei,a2=t2.dotWei,i; + switch(sign) + { //运算后 结果保存到t1中 + case "+":t1.value=t1.value+t2.value;break; + case "-":t1.value=t1.value-t2.value;break; + case "*":t1.value=t1.value*t2.value;break; + case "/":t1.value=t1.value/t2.value;break; + case "%":t1.value=t1.value%t2.value;break; + } + if(t2.value==0&&sign=="/")//除数为0 + { + Disable=true; + d2.setText(t1.value+""); + d1.setText(null); + action="t1"; + return ; + } + if(t1.value==Math.floor(t1.value))//结果为整数 + { + d2.setText((int)t1.value+""); + t1.dotWei=0; + t1.isFloat=false; + } + else{//结果为小数 + String str=t1.value+""; + //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位 + if(sign.equals("+")||sign.equals("-")||sign.equals("%")) + { + i=a1>a2?a1:a2; + str=t1.value+""; + str=str.substring(0, str.indexOf(".")+i+1);//取i位输出 + } + //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位 + else if(sign.equals("*")) + { + i=a1+a2; + BigDecimal bd=new BigDecimal(t1.value); + t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); + str=t1.value+"";//更新修改后的str + } + //结果超过显示数据的最大长度 + if(str.length()>=16) + { + d2.setFont(new Font("宋体",Font.BOLD,14)); + str=str.substring(0, 16); + } + d2.setText(str); + t1.dotWei=str.length()-str.indexOf(".")-1;//更新t1w值 + } + + d1.setText(null); + action="t1"; + t1.clear=true;//开始新的表达式运算时,t1要先重置 + t2.clear=true;//开始新的表达式运算时,t2要先重置 + sign=""; + } + public void dot() + { + if(action=="t1") + t1.isFloat=true; + else + t2.isFloat=true; + d2.setText(d2.getText()+"."); + } + public void run(String SIGN) + { + d2.setFont(new Font("宋体",Font.BOLD,20)); + action="t2"; + int a1=t1.dotWei,a2=t2.dotWei,i; + if(!sign.equals(""))//检测是否为以为表达式的第一运算 + { + switch(sign) + {//运算后 结果保存到t1中 + case "+":t1.value=t1.value+t2.value;break; + case "-":t1.value=t1.value-t2.value;break; + case "*":t1.value=t1.value*t2.value;break; + case "/":t1.value=t1.value/t2.value;break; + case "%":t1.value=t1.value%t2.value;break; + } + } + String temp=isSpecileHandle(); + if(temp==null) + temp=d2.getText();//先保存display2文本框里的数据 + if(t2.value==0&&sign=="/")//除数为0 + { + Disable=true; + d2.setText(t1.value+""); + d1.setText(d1.getText()+t1.value); + } + if(t1.value==Math.floor(t1.value))//结果为整数 + { + d2.setText((int)t1.value+""); + t1.dotWei=0; + t1.isFloat=false; + } + else{ + String str=t1.value+""; + //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位 + if(sign.equals("+")||sign.equals("-")||sign.equals("%")) + { + i=a1>a2?a1:a2; + BigDecimal bd=new BigDecimal(t1.value); + t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); + str=t1.value+"";//更新修改后的str + } + //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位 + else if(sign.equals("*")) + { + i=a1+a2; + BigDecimal bd=new BigDecimal(t1.value); + t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); + str=t1.value+"";//更新修改后的str + } + //结果超过显示数据的最大长度 + if(str.length()>=16) + { + d2.setFont(new Font("宋体",Font.BOLD,14)); + str=str.substring(0, 16); + } + d2.setText(str); + t1.dotWei=str.length()-str.indexOf(".")-1;//每次加完后,如果结果t1的值为小数则更新t1w的值 + } + sign=SIGN; + d1.setText(d1.getText()+temp+sign); + t2.value=t1.value;//运算后,操作数t2默认的值为t1的值 + t2.clear=true;//下一次键入数据,t2要重置 + t1.clear=false;//下一次键入数据,t1不要重置 } \ No newline at end of file -- Gitee From 20e3069eb48e27147027642cff50acd2a33a6858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 20:03:25 +0800 Subject: [PATCH 10/21] =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E5=AD=97=E6=98=AF=E5=90=A6=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=BF=87sqrt=E6=88=961/x=E8=BF=90=E7=AE=97=20=E5=B9=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Calculator b/src/Calculator index 4f794f1..e1733c4 100644 --- a/src/Calculator +++ b/src/Calculator @@ -587,4 +587,34 @@ public class Calculator extends JFrame{ t2.value=t1.value;//运算后,操作数t2默认的值为t1的值 t2.clear=true;//下一次键入数据,t2要重置 t1.clear=false;//下一次键入数据,t1不要重置 + } + + /* + * 操作数是否sqrt()或1/x过, + * 如果有,则返回"sqrt(x)"或"1/x"字符串 + */ + public String isSpecileHandle() + { + String temp=null; + if(t1.isSqrted) + { + temp=t1.sqrtedString; + t1.isSqrted=false; + } + else if(t2.isSqrted) + { + temp=t2.sqrtedString; + t2.isSqrted=false; + } + if(t1.isInverted) + { + temp=t1.invertedString; + t1.isInverted=false; + } + else if(t2.isInverted) + { + temp=t2.invertedString; + t2.isInverted=false; + } + return temp; } \ No newline at end of file -- Gitee From 21b80c9bd97ec1ff9b130a1bd055307c47b28dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 20:06:18 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E9=94=AE=E7=9A=84=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Calculator b/src/Calculator index e1733c4..9901e14 100644 --- a/src/Calculator +++ b/src/Calculator @@ -617,4 +617,28 @@ public class Calculator extends JFrame{ t2.isInverted=false; } return temp; - } \ No newline at end of file + } + class keyAdapter extends KeyAdapter{ + public void keyPressed(KeyEvent e) + { + + int keycode=e.getKeyCode(); + if((keycode>=96&&keycode<=105)||(keycode>=48&&keycode<=57))//数字键 + { + if(keycode>=96) + keycode-=48; + switch(keycode) + { + case 48:read(0);break; + case 49:read(1);break; + case 50:read(2);break; + case 51:read(3);break; + case 52:read(4);break; + case 53:read(5);break; + case 54:read(6);break; + case 55:read(7);break; + case 56:read(8);break; + case 57:read(9);break; + } + } + \ No newline at end of file -- Gitee From 88417e255a4f8ee4dcc43be611cd6f37fd031679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 20:12:07 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E7=AC=A6=E9=94=AE=E2=80=9C+=E3=80=81-?= =?UTF-8?q?=E3=80=81*=E3=80=81/=E3=80=81=3D=E3=80=81.=E2=80=9D=E5=92=8C?= =?UTF-8?q?=E9=80=80=E6=A0=BC=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Calculator b/src/Calculator index 9901e14..c8fa712 100644 --- a/src/Calculator +++ b/src/Calculator @@ -618,6 +618,8 @@ public class Calculator extends JFrame{ } return temp; } + + //数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e) { @@ -641,4 +643,24 @@ public class Calculator extends JFrame{ case 57:read(9);break; } } - \ No newline at end of file + + //运算符键+ - * / =和. 以及退格键(* =号) + else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8) + { + if((keycode==110||keycode==46)) + dot(); + else if(keycode==111||keycode==47) + run("/"); + else if(keycode==45||keycode==109) + run("-"); + else if(keycode==107) + run("+"); + else if(keycode==106) + run("*"); + else if(keycode==61||keycode==10)//=号 + sum(); + else if(keycode==8) + cutEnd(); + } + } + } \ No newline at end of file -- Gitee From 606b873e887f842c22bf5eb9a93254adda0c294f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 20:12:22 +0800 Subject: [PATCH 13/21] =?UTF-8?q?=E4=B8=BB=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Calculator b/src/Calculator index c8fa712..863abc3 100644 --- a/src/Calculator +++ b/src/Calculator @@ -644,7 +644,7 @@ public class Calculator extends JFrame{ } } - //运算符键+ - * / =和. 以及退格键(* =号) + //运算符键+ - * / =和. 以及退格键 else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8) { if((keycode==110||keycode==46)) @@ -663,4 +663,9 @@ public class Calculator extends JFrame{ cutEnd(); } } - } \ No newline at end of file + } + + public static void main(String[] args) { + Calculator app=new Calculator(); + } +} \ No newline at end of file -- Gitee From 5fd4ab15e89ad8f1cd343cc06f190c7658c290d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Mon, 7 Jun 2021 20:46:17 +0800 Subject: [PATCH 14/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 93 +++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/src/Calculator b/src/Calculator index 863abc3..c8fdd9d 100644 --- a/src/Calculator +++ b/src/Calculator @@ -12,7 +12,7 @@ import javax.swing.border.Border; import javax.swing.border.LineBorder; public class Calculator extends JFrame{ - private JTextField display1,display2,display3; + private JTextField d1,d2,d3; private JButton num[],operate[]; private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"}; //定义了只能在该类内部才能访问的名叫name的字符串变量 @@ -40,72 +40,72 @@ public class Calculator extends JFrame{ this.getContentPane().setBackground(new Color(238,228,228)); //设置窗口背景色 - /**显示屏**/ - display1=new JTextField(""); - display2=new JTextField("0"); - display3=new JTextField(""); + /**显示屏**/ + d1=new JTextField(""); + d2=new JTextField("0"); + d3=new JTextField(""); //新建文本框 - display1.setEnabled(false); - display2.setEnabled(false); - display3.setEnabled(false); + d1.setEnabled(false); + d2.setEnabled(false); + d3.setEnabled(false); //设置控件可否使用 - display1.setBounds(0, 0, 204, 26); - display2.setBounds(20, 20, 184, 34); - display3.setBounds(0, 20, 20, 34); + d1.setBounds(0, 0, 204, 26); + d2.setBounds(20, 20, 184, 34); + d3.setBounds(0, 20, 20, 34); //设置按钮的位置 - display1.setHorizontalAlignment(JLabel.RIGHT); - display2.setHorizontalAlignment(JLabel.RIGHT); - display3.setHorizontalAlignment(JLabel.CENTER); + d1.setHorizontalAlignment(JLabel.RIGHT); + d2.setHorizontalAlignment(JLabel.RIGHT); + d3.setHorizontalAlignment(JLabel.CENTER); //左右对齐属性设置 - display1.setFont(new Font("宋体",Font.PLAIN,12)); - display2.setFont(new Font("宋体",Font.BOLD,20)); - display3.setFont(new Font("宋体",Font.PLAIN,20)); + d1.setFont(new Font("宋体",Font.PLAIN,12)); + d2.setFont(new Font("宋体",Font.BOLD,20)); + d3.setFont(new Font("宋体",Font.PLAIN,20)); //设置字体的风格 - display1.setDisabledTextColor(Color.BLACK); - display2.setDisabledTextColor(Color.BLACK); - display3.setDisabledTextColor(Color.BLACK); + d1.setDisabledTextColor(Color.BLACK); + d2.setDisabledTextColor(Color.BLACK); + d3.setDisabledTextColor(Color.BLACK); //设置字体的颜色 - display1.setBorder(new LineBorder(new Color(242,247,252))); - display2.setBorder(new LineBorder(new Color(242,247,252))); - display3.setBorder(new LineBorder(new Color(242,247,252))); + d1.setBorder(new LineBorder(new Color(242,247,252))); + d2.setBorder(new LineBorder(new Color(242,247,252))); + d3.setBorder(new LineBorder(new Color(242,247,252))); //设置线条边框 - display1.setBackground(new Color(228,228,252)); - display2.setBackground(new Color(228,228,252)); - display3.setBackground(new Color(228,228,252)); + d1.setBackground(new Color(228,228,252)); + d2.setBackground(new Color(228,228,252)); + d3.setBackground(new Color(228,228,252)); //设置按钮背景色 /**按钮 35px*29px 28个***/ int i; operate=new JButton[28]; - /**前24个**/ + /**前24个按钮**/ for(i=0;i<24;i++) { operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35, 29); + operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35, 30); jp.add(operate[i]); } - /***=号***/ + /***=号按钮***/ operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35, 29*2+5); + operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35, 30*2+5); jp.add(operate[i]); i++; - /***0号***/ + /***0号按钮***/ operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(29+5)+5, 35*2+7, 29); + operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35*2+7, 30); jp.add(operate[i]); - /**前24个**/ + /**前24个按钮**/ for(i=i+1;i Date: Tue, 8 Jun 2021 23:58:02 +0800 Subject: [PATCH 15/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 106 +++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 66 deletions(-) diff --git a/src/Calculator b/src/Calculator index c8fdd9d..183f6da 100644 --- a/src/Calculator +++ b/src/Calculator @@ -1,15 +1,15 @@ package java2020spring; import java.awt.*; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.math.BigDecimal; +import java.awt.event.KeyAdapter;//接收键盘事件 +import java.awt.event.KeyEvent;//按键键盘事件 +import java.awt.event.MouseAdapter;//鼠标事件监听 +import java.awt.event.MouseEvent;//鼠标触发事件 +import java.math.BigDecimal;//加减乘除运算 import javax.swing.*; -import javax.swing.border.Border; -import javax.swing.border.LineBorder; +import javax.swing.border.Border;//边框属性 +import javax.swing.border.LineBorder;//边框边界线 public class Calculator extends JFrame{ private JTextField d1,d2,d3; @@ -40,7 +40,7 @@ public class Calculator extends JFrame{ this.getContentPane().setBackground(new Color(238,228,228)); //设置窗口背景色 - /**显示屏**/ + //显示屏 d1=new JTextField(""); d2=new JTextField("0"); d3=new JTextField(""); @@ -74,11 +74,11 @@ public class Calculator extends JFrame{ d3.setBackground(new Color(228,228,252)); //设置按钮背景色 - /**按钮 35px*29px 28个***/ + //设置28个按钮大小 int i; operate=new JButton[28]; - /**前24个按钮**/ + //前24个按钮 for(i=0;i<24;i++) { operate[i]=new JButton(name[i]); @@ -87,20 +87,20 @@ public class Calculator extends JFrame{ jp.add(operate[i]); } - /***=号按钮***/ + //=号按钮 operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35, 30*2+5); jp.add(operate[i]); i++; - /***0号按钮***/ + //0号按钮 operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35*2+7, 30); jp.add(operate[i]); - /**前24个按钮**/ + //前24个按钮 for(i=i+1;i Date: Wed, 9 Jun 2021 22:49:50 +0800 Subject: [PATCH 16/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 67 +++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/src/Calculator b/src/Calculator index 183f6da..89894e2 100644 --- a/src/Calculator +++ b/src/Calculator @@ -10,7 +10,6 @@ import java.math.BigDecimal;//加减乘除运算 import javax.swing.*; import javax.swing.border.Border;//边框属性 import javax.swing.border.LineBorder;//边框边界线 - public class Calculator extends JFrame{ private JTextField d1,d2,d3; private JButton num[],operate[]; @@ -22,13 +21,13 @@ public class Calculator extends JFrame{ private String sign="";//运算符,默认为空 private String s1,s2; private boolean Disable=false; - - public Calculator() + + public Calculator() { super("计算器"); this.setLayout(null); //设置布局管理器为默认值 - this.setSize(228, 324); + this.setSize(230, 330); //设置显示框架的宽和高 this.setResizable(false); //设置窗口固定大小不可改变 @@ -39,7 +38,7 @@ public class Calculator extends JFrame{ jp.setBackground(new Color(228,228,228)); this.getContentPane().setBackground(new Color(238,228,228)); //设置窗口背景色 - + //显示屏 d1=new JTextField(""); d2=new JTextField("0"); @@ -57,9 +56,9 @@ public class Calculator extends JFrame{ d2.setHorizontalAlignment(JLabel.RIGHT); d3.setHorizontalAlignment(JLabel.CENTER); //左右对齐属性设置 - d1.setFont(new Font("宋体",Font.PLAIN,12)); - d2.setFont(new Font("宋体",Font.BOLD,20)); - d3.setFont(new Font("宋体",Font.PLAIN,20)); + d1.setFont(new Font("黑体",Font.PLAIN,12)); + d2.setFont(new Font("黑体",Font.BOLD,20)); + d3.setFont(new Font("黑体",Font.PLAIN,20)); //设置字体的风格 d1.setDisabledTextColor(Color.BLACK); d2.setDisabledTextColor(Color.BLACK); @@ -73,8 +72,8 @@ public class Calculator extends JFrame{ d2.setBackground(new Color(228,228,252)); d3.setBackground(new Color(228,228,252)); //设置按钮背景色 - - //设置28个按钮大小 + + //设置28个按钮大小 int i; operate=new JButton[28]; @@ -128,7 +127,7 @@ public class Calculator extends JFrame{ this.add(jp); this.setVisible(true); } - class mouseAdapter extends MouseAdapter{ + class mouseAdapter extends MouseAdapter{ public void mouseClicked(MouseEvent e) { @@ -142,7 +141,7 @@ public class Calculator extends JFrame{ else return; } - + //先计算表达式的值,然后用表达式去匹配常量表达式的值 //匹配到时:从当前的case语句块开始执行,执行完剩余的case语句块; //如果有return、break语句块,则跳出switch{........}。 @@ -169,7 +168,7 @@ public class Calculator extends JFrame{ case "1": case "0":read(Integer.parseInt(operate.getText()));break;//将按键上的文本转化为Int型 case "/":divide();break; - case "%":mo();break; + case "%":yu();break; case "*":mul();break; case "1/x":inverted();break; case "-":cut();break; @@ -187,8 +186,8 @@ public class Calculator extends JFrame{ ((JButton)e.getSource()).setBackground(new Color(233,240,247));//设置鼠标离开键盘时变色 } } - - // MC 取消存储区,清空存储区数据 + + //MC 取消存储区,清空存储区数据 public void mc() { storage.storageNum=0; @@ -225,7 +224,7 @@ public class Calculator extends JFrame{ { storage.storageNum=storage.storageNum-t1.value; } - + //← 输入的数去掉尾数 public void cutEnd() { @@ -272,7 +271,7 @@ public class Calculator extends JFrame{ d2.setText("0");//初始化显示屏2 } - //C 归零 重置计算器 + // C 归零 重置计算器 public void clear() { t1.reset(); @@ -301,7 +300,7 @@ public class Calculator extends JFrame{ d2.setText(s2); } - //√ 根号 + // √ 根号 public void sqrt() { double x;//临时变量 @@ -326,11 +325,11 @@ public class Calculator extends JFrame{ } d2.setText(s2); } - + //按下数字键 public void read(int value)//读取数据 { - d2.setFont(new Font("宋体",Font.BOLD,20));//默认字体大小 + d2.setFont(new Font("黑体",Font.BOLD,20));//默认字体大小 d2.setText(null);//清屏 if(t1.clear==true) t1.reset(); @@ -391,7 +390,7 @@ public class Calculator extends JFrame{ { run("/"); } - public void mo() + public void yu() { run("%"); } @@ -399,7 +398,6 @@ public class Calculator extends JFrame{ { run("*"); } - //1/x 倒数 public void inverted() { @@ -421,7 +419,7 @@ public class Calculator extends JFrame{ str=num+""; if(str.length()>=16)//计算器屏幕所能显示数据的最大长度 { - d2.setFont(new Font("宋体",Font.BOLD,14));//缩小字体输出 + d2.setFont(new Font("黑体",Font.BOLD,14));//缩小字体输出 d2.setText(str.substring(0, 16)); } else @@ -437,7 +435,7 @@ public class Calculator extends JFrame{ } public void sum() { - d2.setFont(new Font("宋体",Font.BOLD,20)); + d2.setFont(new Font("黑体",Font.BOLD,20)); int a1=t1.dotWei,a2=t2.dotWei,i; switch(sign) { //运算后 结果保存到t1中 @@ -463,7 +461,7 @@ public class Calculator extends JFrame{ } else{//结果为小数 String str=t1.value+""; - //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位 + //准确控制算术运算结果的精度,加,减,求余运算,小数点后的有效数字最多为max(d1,d2)位 if(sign.equals("+")||sign.equals("-")||sign.equals("%")) { i=a1>a2?a1:a2; @@ -481,7 +479,7 @@ public class Calculator extends JFrame{ //结果超过显示数据的最大长度 if(str.length()>=16) { - d2.setFont(new Font("宋体",Font.BOLD,14)); + d2.setFont(new Font("黑体",Font.BOLD,14)); str=str.substring(0, 16); } d2.setText(str); @@ -504,7 +502,7 @@ public class Calculator extends JFrame{ } public void run(String SIGN) { - d2.setFont(new Font("宋体",Font.BOLD,20)); + d2.setFont(new Font("黑体",Font.BOLD,20)); action="t2"; int a1=t1.dotWei,a2=t2.dotWei,i; if(!sign.equals(""))//检测是否为以为表达式的第一运算 @@ -520,7 +518,7 @@ public class Calculator extends JFrame{ } String temp=isSpecileHandle(); if(temp==null) - temp=d2.getText();//先保存d2文本框里的数据 + temp=d2.getText();//先保存display2文本框里的数据 if(t2.value==0&&sign=="/")//除数为0 { Disable=true; @@ -535,7 +533,7 @@ public class Calculator extends JFrame{ } else{ String str=t1.value+""; - //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位 + //准确控制算术运算结果的精度,加,减,求余运算,小数点后的有效数字最多为max(d1,d2)位 if(sign.equals("+")||sign.equals("-")||sign.equals("%")) { i=a1>a2?a1:a2; @@ -554,7 +552,7 @@ public class Calculator extends JFrame{ //结果超过显示数据的最大长度 if(str.length()>=16) { - d2.setFont(new Font("宋体",Font.BOLD,14)); + d2.setFont(new Font("黑体",Font.BOLD,14)); str=str.substring(0, 16); } d2.setText(str); @@ -566,8 +564,8 @@ public class Calculator extends JFrame{ t2.clear=true;//下一次键入数据,t2要重置 t1.clear=false;//下一次键入数据,t1不要重置 } - - //操作数是否sqrt()或1/x过,如果有,则返回"sqrt(x)"或"1/x"字符串 + + //操作数是否sqrt()或1/x过,则返回"sqrt(x)"或"1/x"字符串 public String isSpecileHandle() { String temp=null; @@ -593,7 +591,6 @@ public class Calculator extends JFrame{ } return temp; } - //参照keycode对照表 设置数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e) @@ -618,8 +615,7 @@ public class Calculator extends JFrame{ case 57:read(9);break; } } - - //参照keycode对照表 设置运算符键+ - * / =和. 以及退格键 + //参照keycode对照表 设置运算符键+ - * / =和. 以及退格键(* =号) else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8) { if((keycode==110||keycode==46)) @@ -639,7 +635,6 @@ public class Calculator extends JFrame{ } } } - public static void main(String[] args) { Calculator app=new Calculator(); } -- Gitee From 171f392c122403f77d32d208683b6b3eafd53a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 11:27:23 +0800 Subject: [PATCH 17/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 58 ++++++++++++++++++++++++------------------------ src/operateNum | 2 +- src/storageField | 4 ++-- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Calculator b/src/Calculator index 89894e2..e081e65 100644 --- a/src/Calculator +++ b/src/Calculator @@ -33,10 +33,10 @@ public class Calculator extends JFrame{ //设置窗口固定大小不可改变 JPanel jp=new JPanel(null); //设置布局管理器为空,就可以自己手动的设置组件的坐标位置和大小 - jp.setBounds(10,15,getWidth()-24,getHeight()); + jp.setBounds(10,15,getWidth()-25,getHeight()); //设置窗口坐标,宽和高 - jp.setBackground(new Color(228,228,228)); - this.getContentPane().setBackground(new Color(238,228,228)); + jp.setBackground(new Color(230,230,230)); + this.getContentPane().setBackground(new Color(240,230,230)); //设置窗口背景色 //显示屏 @@ -48,9 +48,9 @@ public class Calculator extends JFrame{ d2.setEnabled(false); d3.setEnabled(false); //设置控件可否使用 - d1.setBounds(0, 0, 204, 26); - d2.setBounds(20, 20, 184, 34); - d3.setBounds(0, 20, 20, 34); + d1.setBounds(0, 0, 205, 25); + d2.setBounds(20, 20, 185, 35); + d3.setBounds(0, 20, 20, 35); //设置按钮的位置 d1.setHorizontalAlignment(JLabel.RIGHT); d2.setHorizontalAlignment(JLabel.RIGHT); @@ -64,39 +64,39 @@ public class Calculator extends JFrame{ d2.setDisabledTextColor(Color.BLACK); d3.setDisabledTextColor(Color.BLACK); //设置字体的颜色 - d1.setBorder(new LineBorder(new Color(242,247,252))); - d2.setBorder(new LineBorder(new Color(242,247,252))); - d3.setBorder(new LineBorder(new Color(242,247,252))); + d1.setBorder(new LineBorder(new Color(240,250,250))); + d2.setBorder(new LineBorder(new Color(240,250,250))); + d3.setBorder(new LineBorder(new Color(240,250,250))); //设置线条边框 - d1.setBackground(new Color(228,228,252)); - d2.setBackground(new Color(228,228,252)); - d3.setBackground(new Color(228,228,252)); + d1.setBackground(new Color(230,230,250)); + d2.setBackground(new Color(230,230,250)); + d3.setBackground(new Color(230,230,250)); //设置按钮背景色 //设置28个按钮大小 int i; operate=new JButton[28]; - //前24个按钮 + //前24个按钮 for(i=0;i<24;i++) { operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35, 30); + operate[i].setBounds(i%5*42, 60+i/5*35+5, 35, 30); jp.add(operate[i]); } //=号按钮 operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35, 30*2+5); + operate[i].setBounds(i%5*42, 60+i/5*35+5, 35, 65); jp.add(operate[i]); i++; //0号按钮 operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*(35+7), 60+i/5*(30+5)+5, 35*2+7, 30); + operate[i].setBounds(i%5*40, 60+i/5*35+5, 80, 30); jp.add(operate[i]); //前24个按钮 @@ -104,7 +104,7 @@ public class Calculator extends JFrame{ { operate[i]=new JButton(name[i]); operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds((i+1)%5*(35+7), 60+i/5*(30+5)+5, 35, 30); + operate[i].setBounds((i+1)%5*40, 60+i/5*35+5, 35, 30); jp.add(operate[i]); } @@ -116,8 +116,8 @@ public class Calculator extends JFrame{ { operate[i].addMouseListener(ml); operate[i].addKeyListener(kl); - operate[i].setBackground(new Color(233,240,247)); - operate[i].setForeground(new Color(30,57,91)); + operate[i].setBackground(new Color(230,240,250)); + operate[i].setForeground(new Color(30,60,90)); } //设置监听器和背景色 jp.add(d1); @@ -386,20 +386,20 @@ public class Calculator extends JFrame{ } } - public void divide() + public void divide()//除法 { run("/"); } - public void yu() + public void yu()//求余 { run("%"); } - public void mul() + public void mul()//乘法 { run("*"); } //1/x 倒数 - public void inverted() + public void inverted()//倒数 { double num; String str; @@ -425,15 +425,15 @@ public class Calculator extends JFrame{ else d2.setText(str); } - public void cut() + public void cut()//减法 { run("-"); } - public void add() + public void add()//加法 { run("+"); } - public void sum() + public void sum()//等于 { d2.setFont(new Font("黑体",Font.BOLD,20)); int a1=t1.dotWei,a2=t2.dotWei,i; @@ -492,7 +492,7 @@ public class Calculator extends JFrame{ t2.clear=true;//开始新的表达式运算时,t2要先重置 sign=""; } - public void dot() + public void dot()//小数点 { if(action=="t1") t1.isFloat=true; @@ -518,7 +518,7 @@ public class Calculator extends JFrame{ } String temp=isSpecileHandle(); if(temp==null) - temp=d2.getText();//先保存display2文本框里的数据 + temp=d2.getText();//先保存d2文本框里的数据 if(t2.value==0&&sign=="/")//除数为0 { Disable=true; @@ -593,7 +593,7 @@ public class Calculator extends JFrame{ } //参照keycode对照表 设置数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ - public void keyPressed(KeyEvent e) + public void keyPressed(KeyEvent e)//键盘按键函数 { int keycode=e.getKeyCode(); diff --git a/src/operateNum b/src/operateNum index 673245f..3fac672 100644 --- a/src/operateNum +++ b/src/operateNum @@ -19,7 +19,7 @@ public class operateNum {//操作数类 clear=false; isFloat=false; } - public void reset()//重置操作数 + public void reset()//重置各种操作数 { value=0; dotWei=0; diff --git a/src/storageField b/src/storageField index 90c2fbe..c762440 100644 --- a/src/storageField +++ b/src/storageField @@ -5,7 +5,7 @@ public class storageField {//存储区 public boolean storageMode;//是否启动存储区 public storageField() { - storageNum=0; - storageMode=false; + storageNum=0;//存储数据清除 + storageMode=false;//禁用存储模式 } } \ No newline at end of file -- Gitee From 6eb9e5c9cd1baf852fc4cd2d68b2bc238f4d3f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 12:11:03 +0800 Subject: [PATCH 18/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/src/Calculator b/src/Calculator index e081e65..de7a4dd 100644 --- a/src/Calculator +++ b/src/Calculator @@ -516,9 +516,7 @@ public class Calculator extends JFrame{ case "%":t1.value=t1.value%t2.value;break; } } - String temp=isSpecileHandle(); - if(temp==null) - temp=d2.getText();//先保存d2文本框里的数据 + if(t2.value==0&&sign=="/")//除数为0 { Disable=true; @@ -559,38 +557,13 @@ public class Calculator extends JFrame{ t1.dotWei=str.length()-str.indexOf(".")-1;//每次加完后,如果结果t1的值为小数则更新t1w的值 } sign=SIGN; - d1.setText(d1.getText()+temp+sign); + d1.setText(d1.getText()+sign); t2.value=t1.value;//运算后,操作数t2默认的值为t1的值 t2.clear=true;//下一次键入数据,t2要重置 t1.clear=false;//下一次键入数据,t1不要重置 } - //操作数是否sqrt()或1/x过,则返回"sqrt(x)"或"1/x"字符串 - public String isSpecileHandle() - { - String temp=null; - if(t1.isSqrted) - { - temp=t1.sqrtedString; - t1.isSqrted=false; - } - else if(t2.isSqrted) - { - temp=t2.sqrtedString; - t2.isSqrted=false; - } - if(t1.isInverted) - { - temp=t1.invertedString; - t1.isInverted=false; - } - else if(t2.isInverted) - { - temp=t2.invertedString; - t2.isInverted=false; - } - return temp; - } + //参照keycode对照表 设置数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e)//键盘按键函数 @@ -638,4 +611,5 @@ public class Calculator extends JFrame{ public static void main(String[] args) { Calculator app=new Calculator(); } + } \ No newline at end of file -- Gitee From bf202b8b3de75702b5abf4cca99b50ee255ef679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 13:12:29 +0800 Subject: [PATCH 19/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20sr?= =?UTF-8?q?c/storageField?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/storageField | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/storageField diff --git a/src/storageField b/src/storageField deleted file mode 100644 index c762440..0000000 --- a/src/storageField +++ /dev/null @@ -1,11 +0,0 @@ -package java2020spring; - -public class storageField {//存储区 - public double storageNum;//存储区存储的数据 - public boolean storageMode;//是否启动存储区 - public storageField() - { - storageNum=0;//存储数据清除 - storageMode=false;//禁用存储模式 - } -} \ No newline at end of file -- Gitee From 2769c9fdfdb9cfed187559df84387023c56abe1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 13:33:23 +0800 Subject: [PATCH 20/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 169 +++++++++---------------------------------------- src/operateNum | 13 ++-- 2 files changed, 33 insertions(+), 149 deletions(-) diff --git a/src/Calculator b/src/Calculator index de7a4dd..f316e35 100644 --- a/src/Calculator +++ b/src/Calculator @@ -8,15 +8,13 @@ import java.awt.event.MouseEvent;//鼠标触发事件 import java.math.BigDecimal;//加减乘除运算 import javax.swing.*; -import javax.swing.border.Border;//边框属性 -import javax.swing.border.LineBorder;//边框边界线 public class Calculator extends JFrame{ private JTextField d1,d2,d3; private JButton num[],operate[]; private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"}; //定义了只能在该类内部才能访问的名叫name的字符串变量 private operateNum t1=new operateNum(),t2=new operateNum();//操作数1,操作数2 - private storageField storage=new storageField();//存储区 + private String action="t1";//表示要操作的对象 "t1"操作第一个操作数,"t2"操作第二个操作数 private String sign="";//运算符,默认为空 private String s1,s2; @@ -29,16 +27,11 @@ public class Calculator extends JFrame{ //设置布局管理器为默认值 this.setSize(230, 330); //设置显示框架的宽和高 - this.setResizable(false); - //设置窗口固定大小不可改变 JPanel jp=new JPanel(null); //设置布局管理器为空,就可以自己手动的设置组件的坐标位置和大小 jp.setBounds(10,15,getWidth()-25,getHeight()); //设置窗口坐标,宽和高 - jp.setBackground(new Color(230,230,230)); - this.getContentPane().setBackground(new Color(240,230,230)); - //设置窗口背景色 - + //显示屏 d1=new JTextField(""); d2=new JTextField("0"); @@ -52,32 +45,12 @@ public class Calculator extends JFrame{ d2.setBounds(20, 20, 185, 35); d3.setBounds(0, 20, 20, 35); //设置按钮的位置 - d1.setHorizontalAlignment(JLabel.RIGHT); - d2.setHorizontalAlignment(JLabel.RIGHT); - d3.setHorizontalAlignment(JLabel.CENTER); - //左右对齐属性设置 - d1.setFont(new Font("黑体",Font.PLAIN,12)); - d2.setFont(new Font("黑体",Font.BOLD,20)); - d3.setFont(new Font("黑体",Font.PLAIN,20)); - //设置字体的风格 - d1.setDisabledTextColor(Color.BLACK); - d2.setDisabledTextColor(Color.BLACK); - d3.setDisabledTextColor(Color.BLACK); - //设置字体的颜色 - d1.setBorder(new LineBorder(new Color(240,250,250))); - d2.setBorder(new LineBorder(new Color(240,250,250))); - d3.setBorder(new LineBorder(new Color(240,250,250))); - //设置线条边框 - d1.setBackground(new Color(230,230,250)); - d2.setBackground(new Color(230,230,250)); - d3.setBackground(new Color(230,230,250)); - //设置按钮背景色 //设置28个按钮大小 int i; operate=new JButton[28]; - //前24个按钮 + //前24个按钮 for(i=0;i<24;i++) { operate[i]=new JButton(name[i]); @@ -108,7 +81,7 @@ public class Calculator extends JFrame{ jp.add(operate[i]); } - //给按钮注册鼠标监听器,键盘监听器和背景 + //给按钮注册鼠标监听器,键盘监听器 mouseAdapter ml=new mouseAdapter(); keyAdapter kl=new keyAdapter(); @@ -116,10 +89,7 @@ public class Calculator extends JFrame{ { operate[i].addMouseListener(ml); operate[i].addKeyListener(kl); - operate[i].setBackground(new Color(230,240,250)); - operate[i].setForeground(new Color(30,60,90)); } - //设置监听器和背景色 jp.add(d1); jp.add(d2); jp.add(d3); @@ -128,8 +98,7 @@ public class Calculator extends JFrame{ this.setVisible(true); } class mouseAdapter extends MouseAdapter{ - public void mouseClicked(MouseEvent e) - { + public void mouseClicked(MouseEvent e){ JButton operate=(JButton)e.getSource(); if(Disable)//禁用按钮,点击C 恢复计算器 @@ -147,11 +116,7 @@ public class Calculator extends JFrame{ //如果有return、break语句块,则跳出switch{........}。 switch(operate.getText()) { - case "MC":mc();break; - case "MR":mr();break; - case "MS":ms();break; - case "M+":mAdd();break; - case "M-":mCut();break; + case "←":cutEnd();break; case "CE":cutNum();break; case "C":clear();break; @@ -177,57 +142,11 @@ public class Calculator extends JFrame{ case ".":dot();break; } } - public void mouseEntered(MouseEvent e)//鼠标进入组件 - { - ((JButton)e.getSource()).setBackground(new Color(255,211,113));//设置鼠标接触键盘时变色 - } - public void mouseExited(MouseEvent e)//鼠标退出组件 - { - ((JButton)e.getSource()).setBackground(new Color(233,240,247));//设置鼠标离开键盘时变色 - } - } - - //MC 取消存储区,清空存储区数据 - public void mc() - { - storage.storageNum=0; - storage.storageMode=false; - d3.setText(null); - } - - //MR 读取存储器存储的数据 - public void mr() - { - t1.value=storage.storageNum; - s2=""+t1.value; - if(t1.value==Math.floor(t1.value)) - s2=""+(int)t1.value; - d2.setText(s2); - t1.clear=true; - } - - //MS 保存数据到存储器 - public void ms() - { - storage.storageNum=t1.value; - d3.setText("M");//屏幕左下角显示M标志 - } - - //M+ 已经储存的数加上当前计算结果并将和存入存储器 - public void mAdd() - { - storage.storageNum=storage.storageNum+t1.value; - } - - //M- 已经储存的数减去当前计算结果并将差存入存储器 - public void mCut() - { - storage.storageNum=storage.storageNum-t1.value; } + //← 输入的数去掉尾数 - public void cutEnd() - { + public void cutEnd(){ if(action=="t1"&&t1.value!=0)//表示对t1进行操作 { if(t1.isFloat==false)//如果t1为整数 @@ -237,9 +156,9 @@ public class Calculator extends JFrame{ } else{//如果t1为小数 BigDecimal bd=new BigDecimal(t1.value); - t1.value=bd.setScale(--t1.dotWei,BigDecimal.ROUND_DOWN).doubleValue(); + t1.value=bd.setScale(--t1.dot,BigDecimal.ROUND_DOWN).doubleValue(); s2=""+t1.value; - if(t1.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 + if(t1.dot==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 t1.isFloat=false; } } @@ -252,9 +171,9 @@ public class Calculator extends JFrame{ } else{//如果t2为小数 BigDecimal bd=new BigDecimal(t2.value); - t2.value=bd.setScale(--t2.dotWei,BigDecimal.ROUND_DOWN ).doubleValue(); + t2.value=bd.setScale(--t2.dot,BigDecimal.ROUND_DOWN ).doubleValue(); s2=""+t2.value; - if(t2.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 + if(t2.dot==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记 t2.isFloat=false; } } @@ -308,14 +227,14 @@ public class Calculator extends JFrame{ { t1.sqrtedString="sqrt("+t1.value+")"; t1.value=Math.sqrt(t1.value); - t1.isSqrted=true; + x=t1.value; } else { t2.sqrtedString="sqrt("+t2.value+")"; t2.value=Math.sqrt(t2.value); - t2.isSqrted=true; + x=t2.value; } s2=x+""; @@ -329,7 +248,6 @@ public class Calculator extends JFrame{ //按下数字键 public void read(int value)//读取数据 { - d2.setFont(new Font("黑体",Font.BOLD,20));//默认字体大小 d2.setText(null);//清屏 if(t1.clear==true) t1.reset(); @@ -342,17 +260,17 @@ public class Calculator extends JFrame{ { int i=1; double num=value; - ++t1.dotWei; - while(i<=t1.dotWei) + ++t1.dot; + while(i<=t1.dot) { num*=0.1; i++; } t1.value=t1.value+num;//将新的小数点位添加到操作数t1中 //因为双精度浮点数 其精度比较高,而我们只需取它的t1.dotWei保存 - t1.value=Double.parseDouble(String.format("%."+t1.dotWei+"f",t1.value)); + t1.value=Double.parseDouble(String.format("%."+t1.dot+"f",t1.value)); //因为双精度浮点数 其精度比较高,而我们只需取它的t1.dotWei显示在屏幕上 - d2.setText(String.format("%."+t1.dotWei+"f", t1.value)); + d2.setText(String.format("%."+t1.dot+"f", t1.value)); } else//t1为整数 { @@ -366,17 +284,17 @@ public class Calculator extends JFrame{ { int i=1; double num=value; - ++t2.dotWei; - while(i<=t2.dotWei) + ++t2.dot; + while(i<=t2.dot) { num*=0.1; i++; } t2.value=t2.value+num;//将新的小数点位添加到操作数t2中 //因为双精度浮点数 其精度比较高,而我们只需取它的t2.dotWei保存 - t2.value=Double.parseDouble(String.format("%."+t2.dotWei+"f",t2.value)); + t2.value=Double.parseDouble(String.format("%."+t2.dot+"f",t2.value)); //因为双精度浮点数 其精度比较高,而我们只需取它的t2.dotWei显示在屏幕上 - d2.setText(String.format("%."+t2.dotWei+"f", t2.value)); + d2.setText(String.format("%."+t2.dot+"f", t2.value)); } else//t2为整数 { @@ -407,19 +325,18 @@ public class Calculator extends JFrame{ { t1.invertedString="1/"+t1.value;//1/x形式字符串 t1.value=1/t1.value; - t1.isInverted=true; + num=t1.value; } else{ t2.invertedString="1/"+t2.value;//1/x形式字符串 t2.value=1/t2.value; - t1.isInverted=true; + num=t2.value; } str=num+""; if(str.length()>=16)//计算器屏幕所能显示数据的最大长度 { - d2.setFont(new Font("黑体",Font.BOLD,14));//缩小字体输出 d2.setText(str.substring(0, 16)); } else @@ -435,8 +352,7 @@ public class Calculator extends JFrame{ } public void sum()//等于 { - d2.setFont(new Font("黑体",Font.BOLD,20)); - int a1=t1.dotWei,a2=t2.dotWei,i; + int a1=t1.dot,a2=t2.dot,i; switch(sign) { //运算后 结果保存到t1中 case "+":t1.value=t1.value+t2.value;break; @@ -456,7 +372,7 @@ public class Calculator extends JFrame{ if(t1.value==Math.floor(t1.value))//结果为整数 { d2.setText((int)t1.value+""); - t1.dotWei=0; + t1.dot=0; t1.isFloat=false; } else{//结果为小数 @@ -483,7 +399,7 @@ public class Calculator extends JFrame{ str=str.substring(0, 16); } d2.setText(str); - t1.dotWei=str.length()-str.indexOf(".")-1;//更新t1w值 + t1.dot=str.length()-str.indexOf(".")-1;//更新t1w值 } d1.setText(null); @@ -502,9 +418,8 @@ public class Calculator extends JFrame{ } public void run(String SIGN) { - d2.setFont(new Font("黑体",Font.BOLD,20)); action="t2"; - int a1=t1.dotWei,a2=t2.dotWei,i; + int a1=t1.dot,a2=t2.dot,i; if(!sign.equals(""))//检测是否为以为表达式的第一运算 { switch(sign) @@ -526,35 +441,11 @@ public class Calculator extends JFrame{ if(t1.value==Math.floor(t1.value))//结果为整数 { d2.setText((int)t1.value+""); - t1.dotWei=0; + t1.dot=0; t1.isFloat=false; } else{ String str=t1.value+""; - //准确控制算术运算结果的精度,加,减,求余运算,小数点后的有效数字最多为max(d1,d2)位 - if(sign.equals("+")||sign.equals("-")||sign.equals("%")) - { - i=a1>a2?a1:a2; - BigDecimal bd=new BigDecimal(t1.value); - t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); - str=t1.value+"";//更新修改后的str - } - //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位 - else if(sign.equals("*")) - { - i=a1+a2; - BigDecimal bd=new BigDecimal(t1.value); - t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); - str=t1.value+"";//更新修改后的str - } - //结果超过显示数据的最大长度 - if(str.length()>=16) - { - d2.setFont(new Font("黑体",Font.BOLD,14)); - str=str.substring(0, 16); - } - d2.setText(str); - t1.dotWei=str.length()-str.indexOf(".")-1;//每次加完后,如果结果t1的值为小数则更新t1w的值 } sign=SIGN; d1.setText(d1.getText()+sign); @@ -563,7 +454,6 @@ public class Calculator extends JFrame{ t1.clear=false;//下一次键入数据,t1不要重置 } - //参照keycode对照表 设置数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e)//键盘按键函数 @@ -611,5 +501,4 @@ public class Calculator extends JFrame{ public static void main(String[] args) { Calculator app=new Calculator(); } - } \ No newline at end of file diff --git a/src/operateNum b/src/operateNum index 3fac672..15d8562 100644 --- a/src/operateNum +++ b/src/operateNum @@ -2,32 +2,27 @@ package java2020spring; public class operateNum {//操作数类 public double value;//操作数的实际值 - public int dotWei;//操作数如果是小数,记录小数点后的位数 + public int dot;//操作数如果是小数,记录小数点后的位数 public String invertedString;//1/x字符串 public String sqrtedString;//sqrt(x)字符串 - public boolean isSqrted,isInverted;//做标记是否√,1/x过,用于后续判断 public boolean clear;//clear为真表示是否重置操作数 public boolean isFloat;//isFloat为真,表示操作数是小数 public operateNum() { value=0; - dotWei=0; + dot=0; sqrtedString=null; invertedString=null; - isSqrted=false; - isInverted=false; clear=false; isFloat=false; } public void reset()//重置各种操作数 { value=0; - dotWei=0; + dot=0; sqrtedString=null; invertedString=null; - isSqrted=false; - isInverted=false; clear=false; isFloat=false; } -} +} \ No newline at end of file -- Gitee From 4f91760276d73bc57de3052ef5a66f045cf1f2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=B9=90=E7=90=AA?= <9209132+zeng-leqi@user.noreply.gitee.com> Date: Thu, 10 Jun 2021 14:19:18 +0800 Subject: [PATCH 21/21] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Calculator | 217 ++++++++++++++++--------------------------------- src/operateNum | 6 +- 2 files changed, 74 insertions(+), 149 deletions(-) diff --git a/src/Calculator b/src/Calculator index f316e35..d54a714 100644 --- a/src/Calculator +++ b/src/Calculator @@ -6,7 +6,6 @@ import java.awt.event.KeyEvent;//按键键盘事件 import java.awt.event.MouseAdapter;//鼠标事件监听 import java.awt.event.MouseEvent;//鼠标触发事件 import java.math.BigDecimal;//加减乘除运算 - import javax.swing.*; public class Calculator extends JFrame{ private JTextField d1,d2,d3; @@ -14,15 +13,12 @@ public class Calculator extends JFrame{ private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"}; //定义了只能在该类内部才能访问的名叫name的字符串变量 private operateNum t1=new operateNum(),t2=new operateNum();//操作数1,操作数2 - private String action="t1";//表示要操作的对象 "t1"操作第一个操作数,"t2"操作第二个操作数 private String sign="";//运算符,默认为空 private String s1,s2; private boolean Disable=false; - public Calculator() - { - super("计算器"); + { super("计算器"); this.setLayout(null); //设置布局管理器为默认值 this.setSize(230, 330); @@ -50,13 +46,12 @@ public class Calculator extends JFrame{ int i; operate=new JButton[28]; - //前24个按钮 + //前24个按钮 for(i=0;i<24;i++) - { - operate[i]=new JButton(name[i]); - operate[i].setMargin(new java.awt.Insets(0,0,0,0)); - operate[i].setBounds(i%5*42, 60+i/5*35+5, 35, 30); - jp.add(operate[i]); + {operate[i]=new JButton(name[i]); + operate[i].setMargin(new java.awt.Insets(0,0,0,0)); + operate[i].setBounds(i%5*42, 60+i/5*35+5, 35, 30); + jp.add(operate[i]); } //=号按钮 @@ -74,8 +69,7 @@ public class Calculator extends JFrame{ //前24个按钮 for(i=i+1;i=16)//计算器屏幕所能显示数据的最大长度 - { - d2.setText(str.substring(0, 16)); + {2.setText(str.substring(0, 16)); } - else - d2.setText(str); + else d2.setText(str); } public void cut()//减法 - { - run("-"); + {run("-"); } public void add()//加法 - { - run("+"); + {run("+"); } public void sum()//等于 - { - int a1=t1.dot,a2=t2.dot,i; - switch(sign) - { //运算后 结果保存到t1中 + {int a1=t1.dot,a2=t2.dot,i; + switch(sign){ //运算后 结果保存到t1中 case "+":t1.value=t1.value+t2.value;break; case "-":t1.value=t1.value-t2.value;break; case "*":t1.value=t1.value*t2.value;break; @@ -362,16 +313,14 @@ public class Calculator extends JFrame{ case "%":t1.value=t1.value%t2.value;break; } if(t2.value==0&&sign=="/")//除数为0 - { - Disable=true; + {Disable=true; d2.setText(t1.value+""); d1.setText(null); action="t1"; return ; } if(t1.value==Math.floor(t1.value))//结果为整数 - { - d2.setText((int)t1.value+""); + {d2.setText((int)t1.value+""); t1.dot=0; t1.isFloat=false; } @@ -379,24 +328,20 @@ public class Calculator extends JFrame{ String str=t1.value+""; //准确控制算术运算结果的精度,加,减,求余运算,小数点后的有效数字最多为max(d1,d2)位 if(sign.equals("+")||sign.equals("-")||sign.equals("%")) - { - i=a1>a2?a1:a2; + {i=a1>a2?a1:a2; str=t1.value+""; str=str.substring(0, str.indexOf(".")+i+1);//取i位输出 } //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位 else if(sign.equals("*")) - { - i=a1+a2; + {i=a1+a2; BigDecimal bd=new BigDecimal(t1.value); t1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue(); str=t1.value+"";//更新修改后的str } //结果超过显示数据的最大长度 if(str.length()>=16) - { - d2.setFont(new Font("黑体",Font.BOLD,14)); - str=str.substring(0, 16); + {str=str.substring(0, 16); } d2.setText(str); t1.dot=str.length()-str.indexOf(".")-1;//更新t1w值 @@ -409,20 +354,17 @@ public class Calculator extends JFrame{ sign=""; } public void dot()//小数点 - { - if(action=="t1") + {if(action=="t1") t1.isFloat=true; else t2.isFloat=true; d2.setText(d2.getText()+"."); } public void run(String SIGN) - { - action="t2"; + {action="t2"; int a1=t1.dot,a2=t2.dot,i; if(!sign.equals(""))//检测是否为以为表达式的第一运算 - { - switch(sign) + {switch(sign) {//运算后 结果保存到t1中 case "+":t1.value=t1.value+t2.value;break; case "-":t1.value=t1.value-t2.value;break; @@ -431,21 +373,17 @@ public class Calculator extends JFrame{ case "%":t1.value=t1.value%t2.value;break; } } - if(t2.value==0&&sign=="/")//除数为0 - { - Disable=true; + {Disable=true; d2.setText(t1.value+""); d1.setText(d1.getText()+t1.value); } if(t1.value==Math.floor(t1.value))//结果为整数 - { - d2.setText((int)t1.value+""); + {d2.setText((int)t1.value+""); t1.dot=0; t1.isFloat=false; } - else{ - String str=t1.value+""; + else{String str=t1.value+""; } sign=SIGN; d1.setText(d1.getText()+sign); @@ -457,15 +395,11 @@ public class Calculator extends JFrame{ //参照keycode对照表 设置数字键“0、1、2、3、4、5、6、7、8、9” class keyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e)//键盘按键函数 - { - - int keycode=e.getKeyCode(); + {int keycode=e.getKeyCode(); if((keycode>=96&&keycode<=105)||(keycode>=48&&keycode<=57))//数字键 - { - if(keycode>=96) + {if(keycode>=96) keycode-=48; - switch(keycode) - { + switch(keycode){ case 48:read(0);break; case 49:read(1);break; case 50:read(2);break; @@ -480,21 +414,14 @@ public class Calculator extends JFrame{ } //参照keycode对照表 设置运算符键+ - * / =和. 以及退格键(* =号) else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8) - { - if((keycode==110||keycode==46)) + {if((keycode==110||keycode==46)) dot(); - else if(keycode==111||keycode==47) - run("/"); - else if(keycode==45||keycode==109) - run("-"); - else if(keycode==107) - run("+"); - else if(keycode==106) - run("*"); - else if(keycode==61||keycode==10)//=号 - sum(); - else if(keycode==8) - cutEnd(); + else if(keycode==111||keycode==47) run("/"); + else if(keycode==45||keycode==109) run("-"); + else if(keycode==107) run("+"); + else if(keycode==106) run("*"); + else if(keycode==61||keycode==10)sum();//=号 + else if(keycode==8) cutEnd(); } } } diff --git a/src/operateNum b/src/operateNum index 15d8562..a06e2ea 100644 --- a/src/operateNum +++ b/src/operateNum @@ -8,8 +8,7 @@ public class operateNum {//操作数类 public boolean clear;//clear为真表示是否重置操作数 public boolean isFloat;//isFloat为真,表示操作数是小数 public operateNum() - { - value=0; + {value=0; dot=0; sqrtedString=null; invertedString=null; @@ -17,8 +16,7 @@ public class operateNum {//操作数类 isFloat=false; } public void reset()//重置各种操作数 - { - value=0; + {value=0; dot=0; sqrtedString=null; invertedString=null; -- Gitee