diff --git a/README.md b/README.md index 40fc824a82f8ab822a979d81b19d7815da7cfdc8..54211e68dd26e022f999f30adab53207f74477ed 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,15 @@ -# 科学计算器 +# 计算器DH #### 系统目标 简要介绍系统的用途 +能够进行简单的加减乘除的计算,字体比较大,界面简洁,方便老年人使用。 #### 基本功能 详细介绍系统各个模块的功能。 -1. xxxx -2. xxxx -3. xxxx - - -#### 码云特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 -5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +1. “ 0123456789+-*/= ” 键可以进行简单的两个数的计算; +2. “ 正负 ” 键进行正负数互换; +3. “ 删除 ” 键进行退格删除; +4. “ 开方 ” 键进行开方运算; +5. “ 倒数 ” 键进行倒数运算; +6. “ C ” 键进行清零操作; +7. “ CE ” 键进行清空操作。 diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java deleted file mode 100644 index fc09c195f566d0e707f5e8a2f05fd168069c73a7..0000000000000000000000000000000000000000 --- a/src/java2020spring/Test.java +++ /dev/null @@ -1,10 +0,0 @@ -package java2020spring; - -public class Test { - - public static void main(String[] args) { - System.out.println("Hello world!"); - - } - -} diff --git a/src/jsq_zsl/Jservice.java b/src/jsq_zsl/Jservice.java new file mode 100644 index 0000000000000000000000000000000000000000..b9a784a3efe9ab340b6f2a960f5ebe7cb13fb6ab --- /dev/null +++ b/src/jsq_zsl/Jservice.java @@ -0,0 +1,140 @@ +package jsq_zsl; + + +public class Jservice { + private boolean theSecondNum = false; + private String d; + private String firstNum = "0"; + private String secondNum = "null"; + private double s; + private String numString = "0123456789."; + private String fhString = "+-*/"; + + public String catNum(String cmd, String text) { + String result = cmd; + // 如果text不等于0 + if (!"0".equals(text)) { + if (theSecondNum) { + theSecondNum = false; + } + else { + result = text + cmd; + } + } + if (result.indexOf(".") == 0) { + result = "0" + result; + } + return result; + } + + public String setOp(String cmd, String text) { + this.d = cmd; + this.firstNum = text; + this.secondNum = null; + this.theSecondNum = true; + return null; + } + + public String call(String text) { + double secondResult = secondNum == null ? Double.valueOf(text). + doubleValue() : Double.valueOf(secondNum).doubleValue(); + + //除数为0的容错处理 + if(secondResult == 0 && this.d.equals("/")){ + return "除数不能为0"; + } + //加减乘除 + if(this.d.equals("+")){ + firstNum = String.valueOf(MyMath.add(Double.valueOf(firstNum),secondResult)); + } + else if (this.d.equals("-")) { + firstNum = String.valueOf(MyMath.subtract(Double.valueOf(firstNum),secondResult)); + } + else if (this.d.equals("*")) { + firstNum = String.valueOf(MyMath.multiply(Double.valueOf(firstNum),secondResult)); + } + else if (this.d.equals("/")) { + firstNum = String.valueOf(MyMath.divide(Double.valueOf(firstNum),secondResult)); + } + + secondNum = secondNum == null ? text :secondNum; + this.theSecondNum = true; + return firstNum; + } + //求开方 + public String sqrt(String text){ + this.theSecondNum = true; + return String.valueOf(Math.sqrt(Double.valueOf(text))); + } + //求倒数 + public String setReciprocal(String text){ + if (text.equals("0")){ + return text; + }else{ + this.theSecondNum = true; + return String.valueOf(MyMath.divide(1, Double.valueOf(text))); + } + } + //关于M键的存储 + public String mCmd(String cmd,String text){ + if(cmd.equals("M+")){ + s = MyMath.add(s, Double.valueOf(text)); + }else if (cmd.equals("MC")) { + s = 0; + }else if (cmd.equals("MR")) { + theSecondNum = true; + return String.valueOf(s); + }else if (cmd.equals("MS")) { + s = Double.valueOf(text).doubleValue(); + } + return null; + } + //删除键实现退格 + public String backSpace(String text){ + return text.equals("0") || text.equals("") ? "0" :text.substring(0,text.length()-1); + } + //正负号转换 + public String setNegative(String text){ + if(text.indexOf("-") == 0){ + return text.substring(1,text.length()); + } + else{ + return "-" + text; + } + } + //C清零 + public String clearAll(){ + this.firstNum = "0"; + this.secondNum = null; + return this.firstNum; + } + //CE清空 + public String clear(String text){ + return "0"; + } + + //判断各个按钮,调用对应实现的方法并返回值 + public String callMethod(String cmd, String text){ + if(cmd.equals("C")){ + return clearAll(); + }else if(cmd.equals("CE")){ + return clear(text); + }else if (cmd.equals("删除")) { + return backSpace(text); + }else if (numString.indexOf(cmd) != -1) { + return catNum(cmd, text); + }else if (fhString.indexOf(cmd) != -1) { + return setOp(cmd, text); + }else if (cmd.equals("正负")) { + return setNegative(text); + }else if(cmd.equals("倒数")){ + return setReciprocal(text); + }else if (cmd.equals("开方")) { + return sqrt(text); + }else if(cmd.equals("=")){ + return call(text); + }else { + return mCmd(cmd, text); + } + } +} diff --git a/src/jsq_zsl/MyMath.java b/src/jsq_zsl/MyMath.java new file mode 100644 index 0000000000000000000000000000000000000000..618eabe5315e9c9c7aceca022554b728704b6721 --- /dev/null +++ b/src/jsq_zsl/MyMath.java @@ -0,0 +1,33 @@ +package jsq_zsl; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class MyMath { + /** + * 为一个double类型创建BigDecimal对象,实现精确度计算 + */ + private static BigDecimal getBigDecimal(double number){ + return new BigDecimal(number); + } + public static double add(double num1, double num2) { //加法 + BigDecimal first = getBigDecimal(num1); + BigDecimal second = getBigDecimal(num2); + return first.add(second).doubleValue(); + } + public static double subtract(double num1, double num2) { //减法 + BigDecimal first = getBigDecimal(num1); + BigDecimal second = getBigDecimal(num2); + return first.subtract(second).doubleValue(); + } + public static double multiply(double num1, double num2) { //乘法 + BigDecimal first = getBigDecimal(num1); + BigDecimal second = getBigDecimal(num2); + return first.multiply(second).doubleValue(); + } + public static double divide(double num1, double num2) { //除法 + BigDecimal first = getBigDecimal(num1); + BigDecimal second = getBigDecimal(num2); + return first.divide(second,3,RoundingMode.HALF_UP).doubleValue(); + } +} diff --git a/src/jsq_zsl/Test.java b/src/jsq_zsl/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..88b4b47482e97e5766d53eee454de69020910019 --- /dev/null +++ b/src/jsq_zsl/Test.java @@ -0,0 +1,165 @@ +package jsq_zsl; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +public class Test extends JFrame { + public static void main(String[] args) { + new Test(); + } + /** + * 设计计算器的窗口外观 + */ + private static final long serialVersionUID=1L;//实现序列化类的不同版本间的兼容性 + private final static int MR_WIDTH = 500; + private final static int MR_HEIGHT = 600; //默认窗口宽度和高度 + + private JTextField text = null; + private JButton button = null; //存储标记 + + private String[] a = {"7","8","9","/","开方","4","5","6","*","正负","1","2","3","-","倒数",".","0","+","=","删除"}; + private String[] m = {"MC","MR","MS","M+"}; + private String[] c = {"CE","C"};//私有字符串按钮符号 + + private Jservice service=new Jservice(); + + public Test(){ + this.setTitle("计算器DH"); //标题 + this.setSize(MR_WIDTH, MR_HEIGHT); + this.setLocationRelativeTo(null); + this.setResizable(false); + + //添加底层 + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout(10,1)); + panel.add(getTextField(), BorderLayout.NORTH); + panel.setPreferredSize(new Dimension(MR_WIDTH, MR_HEIGHT)); + + //按钮符号设计与放置 + + JButton[] mButton=getMButton(); + JPanel panel1=new JPanel(); + panel1.setLayout(new GridLayout(5,1,0,5)); + for(JButton b : mButton) { + panel1.add(b); + } + panel.add(panel1,BorderLayout.WEST); + + JButton[] rButton=getRButton(); + JPanel panel2=new JPanel(); + panel2.setLayout(new BorderLayout(1,5)); + JPanel panel21=new JPanel(); + panel21.setLayout(new GridLayout(1,3,3,3)); + for(JButton b : rButton) { + panel21.add(b); + } + panel2.add(panel21,BorderLayout.SOUTH); + + JButton[] nButton=getNButton(); + JPanel panel22=new JPanel(); + panel22.setLayout(new GridLayout(4,5,3,5)); + for(JButton b : nButton) { + panel22.add(b); + } + panel2.add(panel22,BorderLayout.CENTER); + panel.add(panel2,BorderLayout.CENTER); + + this.add(panel); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.setVisible(true); + } + + //文本显示框 + private JTextField getTextField() { + text = new JTextField("0",50); + text.setSize(500, 100); + Font font = new Font("微软雅黑", Font.PLAIN, 20); + text.setFont(font); + return text; + } + + //数字、计算a按钮 + private JButton[] getNButton(){ + String[] fButton= {"/","*","-","+","="}; + JButton[] nbutton=new JButton[a.length]; + for(int i=0;i=0) { + b.setForeground(Color.red); + Font font = new Font("微软雅黑", Font.BOLD, 22);//创建1个字体实例 + b.setFont(font); + } + else { + b.setForeground(Color.black); + Font font = new Font("微软雅黑", Font.BOLD, 20);//创建1个字体实例 + b.setFont(font); + } + nbutton[i]=b; + } + return nbutton; + } + //操作健 + private JButton[] getMButton(){ + JButton[] mbutton = new JButton[m.length + 1]; + mbutton[0] = getButton(); + for(int i = 0; i < this.m.length; i++){ + JButton b = new JButton(this.m[i]); + b.addActionListener(getActionListener()); + b.setForeground(Color.BLUE); + Font font = new Font("微软雅黑", Font.PLAIN, 20);//创建1个字体实例 + b.setFont(font); + mbutton[i+1] = b; + } + return mbutton; + } + private JButton[] getRButton(){ + JButton[] rbutton = new JButton[c.length]; + for(int i = 0; i < this.c.length; i++){ + JButton b = new JButton(this.c[i]); + b.addActionListener(getActionListener()); + b.setForeground(Color.BLACK); + Font font = new Font("微软雅黑", Font.PLAIN, 20);//创建1个字体实例 + b.setFont(font); + rbutton[i] = b; + } + return rbutton; + } + private JButton getButton(){ + button = new JButton(); + return button; + } + private ActionListener getActionListener(){ //监听器构造 + ActionListener actionListener = new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + String cmd = e.getActionCommand(); + String result = null; + try { + result = service.callMethod(cmd, text.getText()); + } catch (Exception e1) { + System.out.println(e1.getMessage()); + } + if(cmd.indexOf("MC") == 0){ + button.setText(""); + }else if(cmd.indexOf("M") == 0){ + button.setText("M"); + } + //显示计算结果 + if(result != null){ + text.setText(result); + } + } + }; + return actionListener; + } +} + + +