From 338a8c11319a00c3f89934276acb2ee9b27b2c0d Mon Sep 17 00:00:00 2001 From: liusimin <2454017533@qq.com> Date: Sat, 28 May 2022 13:26:56 +0800 Subject: [PATCH 1/5] 1 --- src/java2022spring/GoCaculator.java | 119 ++++++++++++++++++++++++++++ src/java2022spring/Math.java | 36 +++++++++ src/java2022spring/Test.java | 7 +- 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/java2022spring/GoCaculator.java create mode 100644 src/java2022spring/Math.java diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java new file mode 100644 index 0000000..61d01e6 --- /dev/null +++ b/src/java2022spring/GoCaculator.java @@ -0,0 +1,119 @@ +package java2022spring; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +public class GoCaculator implements ActionListener{ + + + JTextField f1;//定义文本框 + JButton b1;//定义 + String btName[]= {"del","cls","","pow", + "7","8","9","÷", + "4","5","6","×", + "1","2","3","-", + "0",".","=","+"}; + String str1=""; + String str2=""; + char operate='0'; + + + + + public void go() { + //创建窗口 + JFrame frame = new JFrame("计算器"); + //设置frame的大小、标题 + frame.setSize(300,450); + frame.setTitle("Smin's Calculator"); + frame.setResizable(false);//禁用窗口大小调整 + + + //创建文本框 + f1=new JTextField("0"); + f1.setPreferredSize(new Dimension(400,60));//调整输入框的大小 + //设置文本框字体、颜色 + f1.setFont(new Font("黑体",Font.BOLD,35)); + f1.setBackground(new Color(200,200,200)); + frame.add(f1,BorderLayout.NORTH); + + //创建面板 + JPanel p = new JPanel(); + p.setLayout(new GridLayout(5,4));//4行4列 + + + //创建按键 + b1=new JButton(); + frame.add(b1,BorderLayout.CENTER); + for(int i=0;i='0' && m <= '9') { + //字符串处理 + if(operate == '0') + {str1 += action;//实现 每次点的数字可以保留下来 + f1.setText(str1);}//放入文本框中 + else if(m=='+' || m =='-' || m=='×' || m=='÷' ){ + str2 += action;//切换下一个数 + f1.setText(str2);//显示下一个数 + + operate ='0'; + + } + } + + + else if(m=='+' || m =='-' || m=='×' || m=='÷' ) + { operate = m; + } + + + else if(m=='=') { + double a = Integer.parseInt(str1); + double b = Integer.parseInt(str2); + if(operate=='+') { + double c=Math.add(a,b); + f1.setText(c+""); + } + else if(operate=='-') { + double c=Math.subtract(a,b); + f1.setText(c+""); + } + else if(operate=='×') {; + double c= Math.multiply(a,b); + f1.setText(c+""); + } + else if(operate=='÷') { + double c=Math.divide(a,b); + f1.setText(c+""); + } + /* else if(operate=='sqr') { + double c= + f1.setText(c+""); + } + */ + + } + } +} + diff --git a/src/java2022spring/Math.java b/src/java2022spring/Math.java new file mode 100644 index 0000000..3ba0ec0 --- /dev/null +++ b/src/java2022spring/Math.java @@ -0,0 +1,36 @@ +package java2022spring; +import java.math.*; +//进行加减乘除计算 +public class Math { +//为一个double类型创建对象,精准小数 +private static BigDecimal getBigDecimal(double number) { + return new BigDecimal(number); +} +//加法 +public static double add(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.add(right).doubleValue(); +} +//减法 +public static double subtract(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.subtract(right).doubleValue(); +} +//乘法 +public static double multiply(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.multiply(right).doubleValue(); +} +//除法 +public static double divide(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.divide(right).doubleValue(); +} +//取余 +//乘方 +} + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..3b3cab8 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,10 @@ -package java2022spring; +package java2022spring; +import java.awt.*; +import javax.swing.*; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + GoCaculator use = new GoCaculator(); + use.go(); } } -- Gitee From aa9bc5a2f371a5c1f7ea3608d659d2c2b96e7e35 Mon Sep 17 00:00:00 2001 From: liusimin <2454017533@qq.com> Date: Wed, 1 Jun 2022 21:33:33 +0800 Subject: [PATCH 2/5] 2 --- src/java2022spring/GoCaculator.java | 118 ++++++++++++++++++++-------- src/java2022spring/Math1.java | 40 ++++++++++ src/java2022spring/Test.java | 1 - 3 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 src/java2022spring/Math1.java diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java index 61d01e6..9d6d2d8 100644 --- a/src/java2022spring/GoCaculator.java +++ b/src/java2022spring/GoCaculator.java @@ -3,21 +3,26 @@ package java2022spring; import java.awt.*; import java.awt.event.*; import javax.swing.*; +import java.math.*; public class GoCaculator implements ActionListener{ JTextField f1;//定义文本框 JButton b1;//定义 - String btName[]= {"del","cls","","pow", + String btName[]= {"del","CE","sqrt","^2", "7","8","9","÷", "4","5","6","×", "1","2","3","-", "0",".","=","+"}; String str1=""; String str2=""; - char operate='0'; - + String a1=""; + String b2=""; + char operate='~'; + String djgs=""; + double c; + boolean place; @@ -49,7 +54,7 @@ public class GoCaculator implements ActionListener{ for(int i=0;i='0' && m <= '9') { + if((m >='0' && m <= '9') || m=='.') { //字符串处理 - if(operate == '0') - {str1 += action;//实现 每次点的数字可以保留下来 - f1.setText(str1);}//放入文本框中 - else if(m=='+' || m =='-' || m=='×' || m=='÷' ){ + if(operate == '~') + { place = true; + str1 += action;//实现 每次点的数字可以保留下来 + f1.setText(str1);//放入文本框中 + + } + else { + place = false; str2 += action;//切换下一个数 - f1.setText(str2);//显示下一个数 - - operate ='0'; - + f1.setText(str2);//显示下一个数 } } - else if(m=='+' || m =='-' || m=='×' || m=='÷' ) { operate = m; - } - + } else if(m=='=') { - double a = Integer.parseInt(str1); - double b = Integer.parseInt(str2); + double a = Double.parseDouble(str1); + double b = Double.parseDouble(str2); if(operate=='+') { - double c=Math.add(a,b); + c=Math1.add(a,b); f1.setText(c+""); + } else if(operate=='-') { - double c=Math.subtract(a,b); + c=Math1.subtract(a,b); f1.setText(c+""); + } - else if(operate=='×') {; - double c= Math.multiply(a,b); + else if(operate=='×') { + c= Math1.multiply(a,b); f1.setText(c+""); + } else if(operate=='÷') { - double c=Math.divide(a,b); - f1.setText(c+""); - } - /* else if(operate=='sqr') { - double c= + c=Math1.divide(a,b); f1.setText(c+""); + } - */ - + str1=(c+""); + str2=""; } + + + //其他按键,字符串 + else if(action =="del" || action == "CE" ||action =="1/x"||action == "^2") { + //清空 + if(action=="CE") { + f1.setText("0"); + str1=""; + str2=""; + operate='~';//为下一次计算做准备 + } + //回删 + else if(action=="del") { + if(place) { + str1=str1.substring(0,str1.length()-1); + f1.setText(str1); + } + else + {str2=str2.substring(0,str2.length()-1); + f1.setText(str2);} + } + //开方 + else if(action=="sqrt") { + if(place) { + double a = Double.parseDouble(str1); + f1.setText(Math.sqrt(a)+""); + } + else + { + double a = Double.parseDouble(str2); + f1.setText(Math.sqrt(a)+""); + } + + } + //平方 + else if(action=="^2") { + if(place) { + double a = Double.parseDouble(str1); + double result=a*a; + str1=result+""; + f1.setText(str1); + } + else + {double a = Double.parseDouble(str2); + double result=a*a; + str2=result+""; + f1.setText(str2);} + } + } + } } -} + + + diff --git a/src/java2022spring/Math1.java b/src/java2022spring/Math1.java new file mode 100644 index 0000000..3afc90e --- /dev/null +++ b/src/java2022spring/Math1.java @@ -0,0 +1,40 @@ + package java2022spring; + import java.math.*; + //进行加减乘除计算 + +public class Math1 { + + + //为一个double类型创建对象,精准小数 + private static BigDecimal getBigDecimal(double number) { + return new BigDecimal(number); + } + //加法 + public static double add(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.add(right).doubleValue(); + } + //减法 + public static double subtract(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.subtract(right).doubleValue(); + } + //乘法 + public static double multiply(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.multiply(right).doubleValue(); + } + //除法 + public static double divide(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.divide(right).doubleValue(); + } + //开根号 + + + } + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 3b3cab8..c5b8b37 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,4 +1,3 @@ - package java2022spring; import java.awt.*; import javax.swing.*; -- Gitee From d111758dadc467a590bcd2aebc4b7f1eae3f0d27 Mon Sep 17 00:00:00 2001 From: liusimin <2454017533@qq.com> Date: Fri, 3 Jun 2022 19:26:21 +0800 Subject: [PATCH 3/5] 3 --- src/java2022spring/GoCaculator.java | 64 +++++++++++------------------ src/java2022spring/Math1.java | 13 ++---- src/java2022spring/Test.java | 1 + 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java index 9d6d2d8..f321cf9 100644 --- a/src/java2022spring/GoCaculator.java +++ b/src/java2022spring/GoCaculator.java @@ -1,13 +1,10 @@ package java2022spring; - import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.math.*; -public class GoCaculator implements ActionListener{ - - +public class GoCaculator implements ActionListener{ JTextField f1;//定义文本框 JButton b1;//定义 String btName[]= {"del","CE","sqrt","^2", @@ -23,9 +20,7 @@ public class GoCaculator implements ActionListener{ String djgs=""; double c; boolean place; - - - + public void go() { //创建窗口 JFrame frame = new JFrame("计算器"); @@ -34,20 +29,18 @@ public class GoCaculator implements ActionListener{ frame.setTitle("Smin's Calculator"); frame.setResizable(false);//禁用窗口大小调整 - //创建文本框 f1=new JTextField("0"); f1.setPreferredSize(new Dimension(400,60));//调整输入框的大小 //设置文本框字体、颜色 - f1.setFont(new Font("黑体",Font.BOLD,35)); + f1.setFont(new Font("黑体",Font.BOLD,25)); f1.setBackground(new Color(200,200,200)); frame.add(f1,BorderLayout.NORTH); //创建面板 JPanel p = new JPanel(); p.setLayout(new GridLayout(5,4));//4行4列 - - + //创建按键 b1=new JButton(); frame.add(b1,BorderLayout.CENTER); @@ -58,11 +51,9 @@ public class GoCaculator implements ActionListener{ p.add(bt); bt.addActionListener(this); } - + frame.add(p,BorderLayout.CENTER); - frame.setVisible(true); - - + frame.setVisible(true); } @@ -71,27 +62,23 @@ public class GoCaculator implements ActionListener{ String action = e.getActionCommand(); //String result = null; - char m = action.charAt(0); - + char m = action.charAt(0); if((m >='0' && m <= '9') || m=='.') { //字符串处理 if(operate == '~') { place = true; str1 += action;//实现 每次点的数字可以保留下来 - f1.setText(str1);//放入文本框中 - + f1.setText(str1);//放入文本框中 } else { place = false; str2 += action;//切换下一个数 f1.setText(str2);//显示下一个数 } - } - + } else if(m=='+' || m =='-' || m=='×' || m=='÷' ) { operate = m; } - else if(m=='=') { double a = Double.parseDouble(str1); double b = Double.parseDouble(str2); @@ -118,10 +105,9 @@ public class GoCaculator implements ActionListener{ str1=(c+""); str2=""; } - - + //其他按键,字符串 - else if(action =="del" || action == "CE" ||action =="1/x"||action == "^2") { + else if(action =="del" || action == "CE" ||action =="sqrt"||action == "^2") { //清空 if(action=="CE") { f1.setText("0"); @@ -139,19 +125,7 @@ public class GoCaculator implements ActionListener{ {str2=str2.substring(0,str2.length()-1); f1.setText(str2);} } - //开方 - else if(action=="sqrt") { - if(place) { - double a = Double.parseDouble(str1); - f1.setText(Math.sqrt(a)+""); - } - else - { - double a = Double.parseDouble(str2); - f1.setText(Math.sqrt(a)+""); - } - - } + //平方 else if(action=="^2") { if(place) { @@ -166,9 +140,21 @@ public class GoCaculator implements ActionListener{ str2=result+""; f1.setText(str2);} } + //开方 + else if(action=="sqrt") { + if(place) { + double a = Double.parseDouble(str1); + f1.setText(Math.sqrt(a)+""); + } + else + { + double a = Double.parseDouble(str2); + f1.setText(Math.sqrt(a)+""); + } + } } } - } + } diff --git a/src/java2022spring/Math1.java b/src/java2022spring/Math1.java index 3afc90e..1b605d3 100644 --- a/src/java2022spring/Math1.java +++ b/src/java2022spring/Math1.java @@ -1,10 +1,8 @@ - package java2022spring; - import java.math.*; - //进行加减乘除计算 +package java2022spring; +import java.math.*; +//进行加减乘除计算 -public class Math1 { - - +public class Math1 { //为一个double类型创建对象,精准小数 private static BigDecimal getBigDecimal(double number) { return new BigDecimal(number); @@ -33,8 +31,5 @@ public class Math1 { BigDecimal right = getBigDecimal(b); return left.divide(right).doubleValue(); } - //开根号 - - } diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index c5b8b37..de58acf 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,5 +1,6 @@ package java2022spring; import java.awt.*; + import javax.swing.*; public class Test { public static void main(String[] args) { -- Gitee From ae5738f27ddd780f7cbb0d66219c462ba0574558 Mon Sep 17 00:00:00 2001 From: liusimin <2454017533@qq.com> Date: Mon, 6 Jun 2022 15:41:11 +0800 Subject: [PATCH 4/5] 4 --- src/java2022spring/GoCaculator.java | 1 - src/java2022spring/Math.java | 36 ----------------------------- 2 files changed, 37 deletions(-) delete mode 100644 src/java2022spring/Math.java diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java index f321cf9..38eafcd 100644 --- a/src/java2022spring/GoCaculator.java +++ b/src/java2022spring/GoCaculator.java @@ -17,7 +17,6 @@ public class GoCaculator implements ActionListener{ String a1=""; String b2=""; char operate='~'; - String djgs=""; double c; boolean place; diff --git a/src/java2022spring/Math.java b/src/java2022spring/Math.java deleted file mode 100644 index 3ba0ec0..0000000 --- a/src/java2022spring/Math.java +++ /dev/null @@ -1,36 +0,0 @@ -package java2022spring; -import java.math.*; -//进行加减乘除计算 -public class Math { -//为一个double类型创建对象,精准小数 -private static BigDecimal getBigDecimal(double number) { - return new BigDecimal(number); -} -//加法 -public static double add(double a,double b) { - BigDecimal left = getBigDecimal(a); - BigDecimal right = getBigDecimal(b); - return left.add(right).doubleValue(); -} -//减法 -public static double subtract(double a,double b) { - BigDecimal left = getBigDecimal(a); - BigDecimal right = getBigDecimal(b); - return left.subtract(right).doubleValue(); -} -//乘法 -public static double multiply(double a,double b) { - BigDecimal left = getBigDecimal(a); - BigDecimal right = getBigDecimal(b); - return left.multiply(right).doubleValue(); -} -//除法 -public static double divide(double a,double b) { - BigDecimal left = getBigDecimal(a); - BigDecimal right = getBigDecimal(b); - return left.divide(right).doubleValue(); -} -//取余 -//乘方 -} - -- Gitee From c9616fd64acf2d952f3ddf9812030ce3d0c9890f Mon Sep 17 00:00:00 2001 From: liusimin <2454017533@qq.com> Date: Mon, 6 Jun 2022 16:20:33 +0800 Subject: [PATCH 5/5] 5 --- src/java2022spring/GoCaculator.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java index 38eafcd..c6422f9 100644 --- a/src/java2022spring/GoCaculator.java +++ b/src/java2022spring/GoCaculator.java @@ -38,7 +38,7 @@ public class GoCaculator implements ActionListener{ //创建面板 JPanel p = new JPanel(); - p.setLayout(new GridLayout(5,4));//4行4列 + p.setLayout(new GridLayout(5,4));//5行4列 //创建按键 b1=new JButton(); @@ -143,12 +143,14 @@ public class GoCaculator implements ActionListener{ else if(action=="sqrt") { if(place) { double a = Double.parseDouble(str1); - f1.setText(Math.sqrt(a)+""); + str1=Math.sqrt(a)+""; + f1.setText(str1); } else { double a = Double.parseDouble(str2); - f1.setText(Math.sqrt(a)+""); + str2=Math.sqrt(a)+""; + f1.setText(str2); } } } -- Gitee