From 67c025a22f82eb8afeade1d6008bcf86d9d515fa Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 15 May 2022 20:29:15 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=BB=BA=E7=AB=8B?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=E6=A1=86=E6=9E=B6=EF=BC=8C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E8=83=8C=E6=99=AF=E9=9D=A2=E6=9D=BF=EF=BC=88=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=E8=83=8C=E6=99=AF=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E5=90=91=E7=AA=97=E5=8F=A3=E4=B8=8A=E6=96=B9=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=B8=A4=E4=B8=AA=E6=96=87=E6=9C=AC=E6=A1=86=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E9=9D=A2=E6=B7=BB=E5=8A=A0=E6=8C=89=E9=92=AE=E7=AD=89?= =?UTF-8?q?=E7=BB=84=E5=BB=BA=E3=80=82--=E6=8F=92=E5=85=A5=E8=83=8C?= =?UTF-8?q?=E6=99=AF=E9=9D=A2=E6=9D=BF=E8=AE=BE=E7=BD=AE=E5=80=9F=E9=89=B4?= =?UTF-8?q?CSDN=E5=8E=9F=E5=88=9B=E4=BD=9C=E8=80=85=E6=96=AF=E6=9B=A6?= =?UTF-8?q?=E5=B7=8D=E5=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 140 +++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..b7ab017 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,137 @@ package java2022spring; -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import java.util.ArrayList; +import java.util.Stack; +import javax.swing.*; + +public class Test implements ActionListener { + JFrame c; + JPanel mp;//面板 + JTextField show1,show2; //定义两个文本框用来显示算式和结果 + JButton zero,one,two,three,four,five,six,seven,eight,nine;//定义数字按钮 + JButton div,mul,plus,minus,eql;//定义算式的字符串表示 + JButton CE,C,backspace,dpoint,sign;//定义删除、小数点、正负号功能 + String exp,outcome;//定义运算符号按钮 + public static void main(String[] args) { + Test cal=new Test(); + cal.display(); + + } + //在构造函数中初始化控件和变量 + Test() + { + c=new JFrame("计算器"); + Image image=new ImageIcon("D:\\图片\\R-C (1).jpg").getImage(); //添加背景图片,将Image对象与图像文件联系起来 + mp=new BackgroundPanel(image);//背景面板设置 + show1=new JTextField("0");//创建单行文本控件 + show2=new JTextField(); + zero=new JButton("0");//创建按钮 + one=new JButton("1"); + two=new JButton("2"); + three=new JButton("3"); + four=new JButton("4"); + five=new JButton("5"); + six=new JButton("6"); + seven=new JButton("7"); + eight=new JButton("8"); + nine=new JButton("9"); + div=new JButton("/"); + mul=new JButton("*"); + plus=new JButton("+"); + minus=new JButton("-"); + CE=new JButton("CE"); + C=new JButton("C"); + backspace=new JButton("Back"); + sign=new JButton("+/-"); + dpoint=new JButton("."); + eql=new JButton("="); + exp=outcome="";//初始设置存储算式和结果的字符串为空串 + c.setSize(400, 580);//设置窗口大小 + c.setLocationRelativeTo(null);//设置窗口默认居中位置 + c.setContentPane(mp);//向窗口添加背景面板 + c.setVisible(true);//设置窗口可见 + c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭就退出程序 + + } + public void display() + { + //设置显示结果的单行文本框相关属性 + show1.setHorizontalAlignment(SwingConstants.RIGHT);//设置文本靠右显示 + show1.setFont(new Font(Font.MONOSPACED, Font.CENTER_BASELINE,50));//设置字体样式 + show1.setBorder(BorderFactory.createEmptyBorder());//设置单行文本控件无边框 + show1.setEnabled(false);//设置单行文本框不能点击 + show2.setHorizontalAlignment(SwingConstants.RIGHT);//设置文本靠右显示 + show2.setFont(new Font(Font.MONOSPACED, Font.PLAIN,30));//设置字体样式 + show2.setBorder(BorderFactory.createEmptyBorder());//设置单行文本控件无边框 + show2.setEnabled(false); + //设置按钮字体颜色为蓝色 + //设置按钮字体样式 + //设置按钮为透明效果 + CE.setForeground(Color.BLACK); + CE.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + CE.setContentAreaFilled(false); + C.setForeground(Color.BLACK); + C.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + C.setContentAreaFilled(false); + backspace.setForeground(Color.BLACK); + backspace.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + backspace.setContentAreaFilled(false); + div.setForeground(Color.black); + div.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18)); + div.setContentAreaFilled(false); + mul.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18)); + mul.setForeground(Color.black); + mul.setContentAreaFilled(false); + plus.setForeground(Color.black); + plus.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18)); + plus.setContentAreaFilled(false); + minus.setForeground(Color.black); + minus.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18)); + minus.setContentAreaFilled(false); + eql.setForeground(Color.black); + eql.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18)); + eql.setContentAreaFilled(false); + dpoint.setForeground(Color.black); + dpoint.setFont(new Font(Font.SERIF, Font.BOLD, 18)); + dpoint.setContentAreaFilled(false); + zero.setForeground(Color.black); + zero.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + zero.setContentAreaFilled(false); + one.setForeground(Color.black); + one.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + one.setContentAreaFilled(false); + two.setForeground(Color.black); + two.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + two.setContentAreaFilled(false); + three.setForeground(Color.black); + three.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + three.setContentAreaFilled(false); + four.setForeground(Color.black); + four.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + four.setContentAreaFilled(false); + five.setForeground(Color.black); + five.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + five.setContentAreaFilled(false); + six.setForeground(Color.black); + six.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + six.setContentAreaFilled(false); + seven.setForeground(Color.black); + seven.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + seven.setContentAreaFilled(false); + eight.setForeground(Color.black); + eight.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + eight.setContentAreaFilled(false); + nine.setForeground(Color.black); + nine.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + nine.setContentAreaFilled(false); + sign.setForeground(Color.black); + sign.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); + sign.setContentAreaFilled(false); + } + } + + -- Gitee From 341aa6d3ec881367c176bfb152ee8a1ec08a466e Mon Sep 17 00:00:00 2001 From: pandora-box Date: Tue, 17 May 2022 00:54:10 +0800 Subject: [PATCH 02/20] =?UTF-8?q?=E5=90=91=E7=AA=97=E5=8F=A3=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E9=9D=A2=E6=9D=BF=EF=BC=8C=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E4=B8=AD=E9=97=B4=E5=AE=B9=E5=99=A8=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E6=8C=89=E9=92=AE=E3=80=81=E6=96=87=E6=9C=AC=E6=A1=86?= =?UTF-8?q?=E6=8C=89=E6=89=80=E9=9C=80=E6=A0=BC=E5=BC=8F=E6=8E=92=E6=94=BE?= =?UTF-8?q?=E5=A5=BD=EF=BC=8C=E5=90=8C=E6=97=B6=E4=B8=BA=E5=90=84=E4=B8=AA?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=B3=A8=E5=86=8C=E4=BA=86=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 126 ++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index b7ab017..bc079cb 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -12,7 +12,8 @@ public class Test implements ActionListener { JFrame c; JPanel mp;//面板 JTextField show1,show2; //定义两个文本框用来显示算式和结果 - JButton zero,one,two,three,four,five,six,seven,eight,nine;//定义数字按钮 + JButton one,two,three,four,five,six,seven,eight,nine,zero;//定义数字按钮 + GridBagLayout gb=new GridBagLayout();//设置网格布袋布局 JButton div,mul,plus,minus,eql;//定义算式的字符串表示 JButton CE,C,backspace,dpoint,sign;//定义删除、小数点、正负号功能 String exp,outcome;//定义运算符号按钮 @@ -131,7 +132,130 @@ public class Test implements ActionListener { sign.setForeground(Color.black); sign.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); sign.setContentAreaFilled(false); + + + + + GridBagConstraints g=new GridBagConstraints(); + g.fill=GridBagConstraints.BOTH;//当组件格子有剩余空间时,填充空间 + g.gridx=0;//X=0 + g.gridy=0;//Y=0 + g.weightx=0.5;//设置窗口变大时缩放比例 + g.weighty=0.5; + + g.gridwidth=GridBagConstraints.REMAINDER;//从创建的起点开始,一直延伸占满整行 + g.gridheight=1;//列占一个单元格 + g.insets=new Insets(0, 0, 0, 0);//设置该组件与其它组件的距离(上下左右) + gb.setConstraints(show2, g);//将show2设置成g,文本框添加到 + g.gridx=0; //设置show1 + g.gridy=1; + g.gridheight=2; + g.insets=new Insets(0, 0, 0, 0); + gb.setConstraints(show1, g); + + g.gridx=0; + g.gridy=3; + g.insets=new Insets(4,4,4,4); + g.gridwidth=1;//横占一个单元格 + g.gridheight=1;//列占一个单元格 + gb.setConstraints(C, g); + + //Y=3布局 + g.gridx=1; + gb.setConstraints(CE, g); + g.gridx=2; + gb.setConstraints(backspace, g); + g.gridx=3; + gb.setConstraints(div, g); + //Y=4布局 + g.gridy=4; + g.gridx=0; + gb.setConstraints(seven, g); + g.gridx=1; + gb.setConstraints(eight, g); + g.gridx=2; + gb.setConstraints(nine, g); + g.gridx=3; + gb.setConstraints(mul, g); + //Y=5 + g.gridy=5; + g.gridx=0; + gb.setConstraints(four, g); + g.gridx=1; + gb.setConstraints(five, g); + g.gridx=2; + gb.setConstraints(six, g); + g.gridx=3; + gb.setConstraints(minus, g); + //Y=6 + g.gridy=6; + g.gridx=0; + gb.setConstraints(one, g); + g.gridx=1; + gb.setConstraints(two, g); + g.gridx=2; + gb.setConstraints(three, g); + g.gridx=3; + gb.setConstraints(plus, g); + //Y=7 + g.gridy=7; + g.gridx=0; + gb.setConstraints(sign, g); + g.gridx=1; + gb.setConstraints(zero, g); + g.gridx=2; + gb.setConstraints(dpoint, g); + g.gridx=3; + gb.setConstraints(eql, g); + + //向面板添加文本框、按钮 + mp.add(show1); + mp.add(show2); + mp.add(CE); + mp.add(C); + mp.add(backspace); + mp.add(div); + mp.add(seven); + mp.add(eight); + mp.add(nine); + mp.add(mul); + mp.add(four); + mp.add(five); + mp.add(six); + mp.add(minus); + mp.add(one); + mp.add(two); + mp.add(three); + mp.add(plus); + mp.add(sign); + mp.add(zero); + mp.add(dpoint); + mp.add(eql); + //为按钮添加监听事件 + CE.addActionListener(this); + C.addActionListener(this); + backspace.addActionListener(this); + plus.addActionListener(this); + minus.addActionListener(this); + mul.addActionListener(this); + div.addActionListener(this); + dpoint.addActionListener(this); + sign.addActionListener(this); + eql.addActionListener(this); + zero.addActionListener(this); + one.addActionListener(this); + two.addActionListener(this); + three.addActionListener(this); + four.addActionListener(this); + five.addActionListener(this); + six.addActionListener(this); + seven.addActionListener(this); + eight.addActionListener(this); + nine.addActionListener(this); + + } } + -- Gitee From f7006489e4ad4e0209981b48d132f0fca205030a Mon Sep 17 00:00:00 2001 From: pandora-box Date: Fri, 20 May 2022 17:31:59 +0800 Subject: [PATCH 03/20] =?UTF-8?q?=E6=8E=A8=E7=BF=BB=E9=87=8D=E5=81=9A=20?= =?UTF-8?q?=E6=90=AD=E5=BB=BA=E5=9F=BA=E6=9C=AC=E6=A1=86=E6=9E=B6=EF=BC=8C?= =?UTF-8?q?=E5=B8=83=E5=B1=80=E4=B8=BA=E8=AE=BE=E7=BD=AE=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E5=B8=83=E5=B1=80=EF=BC=8C=E5=90=91=E7=AA=97=E5=8F=A3=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=96=87=E6=9C=AC=E6=A1=86=E3=80=81=E6=8C=89=E9=92=AE?= =?UTF-8?q?=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=A4=9A=E7=A7=8D=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 88 ++++++++++ src/java2022spring/MainClass.java | 16 ++ src/java2022spring/Test.java | 261 ----------------------------- 3 files changed, 104 insertions(+), 261 deletions(-) create mode 100644 src/java2022spring/Calculator.java create mode 100644 src/java2022spring/MainClass.java delete mode 100644 src/java2022spring/Test.java diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000..31da6eb --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,88 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Calculator extends JFrame implements ActionListener { + + String[] keysName={"C","BS","e","π","^2","1/x","","(",")","sqr","÷","7","8","9","×","4","5","6","-","1","2","3","+","%","0",".","="}; + JButton keys[]=new JButton[keysName.length]; + JTextField text = new JTextField("0.0"); + String c=""; + public Calculator() { + text.setBounds(18, 5, 258, 45); + text.setHorizontalAlignment(JTextField.RIGHT); + text.setEditable(false); + add(text); + Font font=new Font("Times New Roman",Font.PLAIN,18); + text.setFont(font); + int x=20,y=65; + for (int i=0;i Date: Sat, 21 May 2022 23:51:13 +0800 Subject: [PATCH 04/20] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=A0=87?= =?UTF-8?q?=E5=87=86=E8=AE=A1=E7=AE=97=E5=99=A8=EF=BC=8C=E9=99=A4=E6=AD=A4?= =?UTF-8?q?=E4=B9=8B=E5=A4=96=E8=BF=98=E5=A2=9E=E6=B7=BB=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E7=A7=91=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82=E8=B0=83=E6=95=B4=E4=BA=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E6=8C=89=E9=92=AE=E7=9A=84=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86CE=E5=8A=9F=E8=83=BD--?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=B8=85=E9=99=A4=E4=B8=8A=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E7=AC=A6=E4=B9=8B=E5=90=8E=E7=9A=84=E7=AE=97?= =?UTF-8?q?=E5=BC=8F=E3=80=82=E8=BF=90=E7=AE=97=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=8E=9F=E7=90=86=E5=80=9F=E9=89=B4=E4=BA=86CSDN=E5=8D=9A?= =?UTF-8?q?=E4=B8=BB=E7=8C=AB=E7=8C=AB=E8=99=AB=E7=9A=84=E5=80=9F=E5=8A=A9?= =?UTF-8?q?=E6=A0=88=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82=E8=AF=A5=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E5=8F=AF=E4=BB=A5=E5=AE=9E=E7=8E=B0=E6=A0=B9?= =?UTF-8?q?=E5=8F=B7=E3=80=81=E5=B9=B3=E6=96=B9=E6=95=B0=E3=80=81=E5=80=92?= =?UTF-8?q?=E6=95=B0=E3=80=81=E6=8B=AC=E5=8F=B7=E7=9A=84=E7=AE=97=E6=9C=AF?= =?UTF-8?q?=E8=BF=90=E7=AE=97=EF=BC=8C=E7=9B=AE=E5=89=8D=E7=BC=BA=E9=99=B7?= =?UTF-8?q?=E6=98=AF=E8=BF=98=E4=B8=8D=E8=83=BD=E5=AE=9E=E7=8E=B0=E5=B8=A6?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E8=BF=90=E7=AE=97=EF=BC=8C=E5=90=8E=E7=BB=AD?= =?UTF-8?q?=E5=B0=9D=E8=AF=95=E6=94=B9=E8=BF=9B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 399 +++++++++++++++++++++++------ 1 file changed, 320 insertions(+), 79 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 31da6eb..0258b98 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -4,85 +4,326 @@ import javax.swing.*; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"C","BS","e","π","^2","1/x","","(",")","sqr","÷","7","8","9","×","4","5","6","-","1","2","3","+","%","0",".","="}; - JButton keys[]=new JButton[keysName.length]; - JTextField text = new JTextField("0.0"); - String c=""; - public Calculator() { - text.setBounds(18, 5, 258, 45); - text.setHorizontalAlignment(JTextField.RIGHT); - text.setEditable(false); - add(text); - Font font=new Font("Times New Roman",Font.PLAIN,18); - text.setFont(font); - int x=20,y=65; - for (int i=0;i=0) { + c=c.substring(0, pos+1); + text.setText(c); + } + else + text.setText("0.0"); + } + else if (label=="sqr") { + String s,a,b; + if(c=="") + c=""; + else{ + int pos=posOfLastOperator(c); + if(pos>=0) { + a=c.substring(pos+1); + b=operationTwo(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationTwo(c); + text.setText(s); + c=s; + } + } + } + else if(label=="^2") { + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + if(pos>=0) { + a=c.substring(pos+1); + b=operationThree(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationThree(c); + text.setText(s); + c=s; + } + } + } + else if(label=="1/x"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + if(pos>=0) { + a=c.substring(pos+1); + b=operationFour(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationFour(c); + text.setText(s); + c=s; + } + } + } + else if (label=="e") { + String s=String.valueOf(2.71828182846); + c=c+s; + text.setText(c); + } + else if (label=="π") { + String s=String.valueOf(3.141592653589); + c=c+s; + text.setText(c); + } + else { + c=c+label; + text.setText(c); + } +} + String []operationOne(String str) { + String s=""; + char a[]=new char[200]; + String outcome[]=new String[200]; + int top=-1,j=0; + for (int i=0;i=0) { + s=""; + for (;i=0;i++) { + s=s+str.charAt(i); + } + i--; + outcome[j]=s; + j++; + } + else if ("(".indexOf(str.charAt(i))>=0) { + top++; + a[top]=str.charAt(i); + } + else if (")".indexOf(str.charAt(i))>=0) { + for (;;) { + if (a[top]!='(') { + outcome[j]=a[top]+""; + j++; + top--; + } + else { + top--; + break; + } + } + } + else if ("×%÷".indexOf(str.charAt(i))>=0) { + if (top==-1) { + top++; + a[top]=str.charAt(i); + } + else { + if ("×%÷".indexOf(a[top])>=0) { + outcome[j]=a[top]+""; + j++; + a[top]=str.charAt(i); + } + else if ("(".indexOf(a[top])>=0) { + top++; + a[top]=str.charAt(i); + } + else if ("+-".indexOf(a[top])>=0) { + top++; + a[top]=str.charAt(i); + } + } + } + else if ("+-".indexOf(str.charAt(i))>=0) { + if (top==-1) { + top++; + a[top]=str.charAt(i); + } + else { + if ("%×÷".indexOf(a[top])>=0) { + outcome[j]=a[top]+""; + j++; + a[top]=str.charAt(i); + } + else if ("(".indexOf(a[top])>=0) { + top++; + a[top]=str.charAt(i); + } + else if ("+-".indexOf(a[top])>=0) { + outcome[j]=a[top]+""; + j++; + a[top]=str.charAt(i); + } + } + } + } + for (;top!=-1;) { + outcome[j]=a[top]+""; + j++; + top--; + } + return outcome; + } + public String outCome(String str[]) { + String result[]=new String[200]; + int Top=-1; + Pattern reg = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); + for (int i=0;str[i]!=null;i++) { + if (reg.matcher(str[i]).matches()) { //数字字符进栈 + Top++; + result[Top]=str[i]; + } + else if ("+-×%÷".indexOf(str[i])>=0) { + double x,y,n; + x=Double.parseDouble(result[Top]); + Top--; + y=Double.parseDouble(result[Top]); + Top--; + if ("-".indexOf(str[i])>=0) { + n=y-x; + Top++; + result[Top]=String.valueOf(n); + } + else if ("+".indexOf(str[i])>=0) { + n=y+x; + Top++; + result[Top]=String.valueOf(n); + } + else if ("×".indexOf(str[i])>=0) { + n=y*x; + Top++; + result[Top]=String.valueOf(n); + } + else if ("÷".indexOf(str[i])>=0) { + if (x==0) { + String s="error"; + return s; + } + else { + n=y/x; + Top++; + result[Top]=String.valueOf(n); + } + } + if ("%".indexOf(str[i])>=0) { + if (x==0) { + String s="error"; + return s; + } + else { + n=y%x; + Top++; + result[Top]=String.valueOf(n); + } + } + } + } + return result[Top]; + } + public String operationTwo(String str) + { + String result; + double a=Double.parseDouble(str),b=0; + b=Math.sqrt(a); + result=String.valueOf(b); + return result; + } + public String operationThree(String str) + { + String result; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a, 2); + result=String.valueOf(b); + return result; + } + public String operationFour(String str) + { + String result; + double a=Double.parseDouble(str),b=0; + String s; + if(a==0.0) { + s="error"; + return s; + } + else { + b=1/a; + result=String.valueOf(b); + return result; + } + } + public int posOfLastOperator(String s) + { + for(int i=s.length()-1;i>=0;i--) + { + if(isOperator(s.charAt(i))) + return i; + } + return -1; + } + public boolean isOperator(char c) + { + return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%')||(c=='(')||(c==')'); + } + +} -- Gitee From 0b65e65d6737f521f1b7ec93e8afc684ed67cfef Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 22 May 2022 15:52:06 +0800 Subject: [PATCH 05/20] =?UTF-8?q?=E8=83=BD=E5=A4=9F=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=E5=86=85=E7=9A=84=E6=A0=B9=E5=8F=B7=E3=80=81?= =?UTF-8?q?=E5=80=92=E6=95=B0=E3=80=81=E5=B9=B3=E6=96=B9=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E8=BF=90=E7=AE=97=EF=BC=8C=E4=BE=8B=E5=A6=821+=EF=BC=882+3?= =?UTF-8?q?=EF=BC=89^2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 66 ++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 0258b98..5816afc 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -7,7 +7,6 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","BS","1/x","sqr","^2","e","π","(",")","÷","7","8","9","×","4","5","6","-","1","2","3","+","+/-","0",".","="}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); @@ -70,8 +69,21 @@ public class Calculator extends JFrame implements ActionListener { c=""; else{ int pos=posOfLastOperator(c); - if(pos>=0) { - a=c.substring(pos+1); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationTwo(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); b=operationTwo(a); StringBuilder builder=new StringBuilder(c); builder.replace(pos+1,c.length(),b); @@ -91,8 +103,21 @@ public class Calculator extends JFrame implements ActionListener { c=""; else { int pos=posOfLastOperator(c); - if(pos>=0) { - a=c.substring(pos+1); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationThree(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); b=operationThree(a); StringBuilder builder=new StringBuilder(c); builder.replace(pos+1,c.length(),b); @@ -112,7 +137,20 @@ public class Calculator extends JFrame implements ActionListener { c=""; else { int pos=posOfLastOperator(c); - if(pos>=0) { + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationFour(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { a=c.substring(pos+1); b=operationFour(a); StringBuilder builder=new StringBuilder(c); @@ -323,7 +361,19 @@ public class Calculator extends JFrame implements ActionListener { } public boolean isOperator(char c) { - return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%')||(c=='(')||(c==')'); + return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%'); + } + public int posOfLastOperatorOne(String s) + { + for(int i=s.length()-1;i>=0;i--) + { + if(isOperatorOne(s.charAt(i))) + return i; + } + return -1; + } + public boolean isOperatorOne(char c) + { + return c=='('; } - } -- Gitee From 7029640111b8542ba59e3172a76a8c88f090ef67 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 22 May 2022 17:45:05 +0800 Subject: [PATCH 06/20] =?UTF-8?q?=E6=94=B9=E5=8A=A8CE=E9=94=AE=EF=BC=8C?= =?UTF-8?q?=E6=8C=89=E4=B8=8B=E8=AF=A5=E9=94=AE=E8=8B=A5=E6=97=A0=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=AC=A6=EF=BC=8C=E6=B8=85=E7=A9=BA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=A1=86=E5=86=85=E5=AE=B9=E5=B9=B6=E7=BD=AE"0.0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 5816afc..b9a1f71 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -61,6 +61,7 @@ public class Calculator extends JFrame implements ActionListener { text.setText(c); } else + c=""; text.setText("0.0"); } else if (label=="sqr") { -- Gitee From a74c4db508f31228b6eb2afd3705f1962745c1a8 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Mon, 23 May 2022 21:29:14 +0800 Subject: [PATCH 07/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86sin=E3=80=81co?= =?UTF-8?q?s=E3=80=81tan=E3=80=81lg=E3=80=81ln=E3=80=81x!=E7=9A=84?= =?UTF-8?q?=E7=A7=91=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 315 +++++++++++++++++++++++++++-- src/java2022spring/MainClass.java | 2 +- 2 files changed, 302 insertions(+), 15 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index b9a1f71..7cf8645 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -7,13 +7,14 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","BS","1/x","sqr","^2","e","π","(",")","÷","7","8","9","×","4","5","6","-","1","2","3","+","+/-","0",".","="}; + String[] keysName={"%","C","CE","BS","1/x","sqr","x^2","e","π","(",")","÷","7","8","9","×","4","5","6","-","1","2","3","+","x^3","0",".","=","sin","cos","tan","lg","ln","x!"}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; + String l; Pattern pattern=Pattern.compile("[+-×%]"); public Calculator() { - text.setBounds(18, 5, 258, 45); + text.setBounds(18, 5, 258, 48); text.setHorizontalAlignment(JTextField.RIGHT); text.setEditable(false); add(text); @@ -40,6 +41,7 @@ public class Calculator extends JFrame implements ActionListener { } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); + boolean flag=false; if(label=="=") { String s[]=operationOne(c); String result=outCome(s); @@ -98,7 +100,7 @@ public class Calculator extends JFrame implements ActionListener { } } } - else if(label=="^2") { + else if(label=="x^2") { String s,a,b; if(c=="") c=""; @@ -132,6 +134,40 @@ public class Calculator extends JFrame implements ActionListener { } } } + else if(label=="x^3") { + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationFive(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + b=operationFive(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationFive(c); + text.setText(s); + c=s; + } + } + } else if(label=="1/x"){ String s,a,b; if(c=="") @@ -166,6 +202,240 @@ public class Calculator extends JFrame implements ActionListener { } } } + else if(label=="sin"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.sin(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="cos"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.cos(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="tan"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.tan(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.tan(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.tan(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="lg"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.log10(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.log10(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.log10(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="ln"){ + String s,a; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.log(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.log(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.log(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="x!"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationSix(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + b=operationSix(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationSix(c); + text.setText(s); + c=s; + } + } + } else if (label=="e") { String s=String.valueOf(2.71828182846); c=c+s; @@ -176,11 +446,11 @@ public class Calculator extends JFrame implements ActionListener { c=c+s; text.setText(c); } - else { + else { c=c+label; text.setText(c); } -} + } String []operationOne(String str) { String s=""; char a[]=new char[200]; @@ -239,7 +509,7 @@ public class Calculator extends JFrame implements ActionListener { top++; a[top]=str.charAt(i); } - else { + else { if ("%×÷".indexOf(a[top])>=0) { outcome[j]=a[top]+""; j++; @@ -255,6 +525,7 @@ public class Calculator extends JFrame implements ActionListener { a[top]=str.charAt(i); } } + } } for (;top!=-1;) { @@ -351,8 +622,27 @@ public class Calculator extends JFrame implements ActionListener { return result; } } - public int posOfLastOperator(String s) - { + public String operationFive(String str) { + String result; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a, 3); + result=String.valueOf(b); + return result; + } + public String operationSix(String str) { + int ee; + double p=Double.parseDouble(str); + double result=p; + int c=1; + for(ee=1;ee<=result;ee++) + { + c=c*ee; + p=c; + } + String s=String.valueOf(p); + return s; + } + public int posOfLastOperator(String s) { for(int i=s.length()-1;i>=0;i--) { if(isOperator(s.charAt(i))) @@ -360,12 +650,10 @@ public class Calculator extends JFrame implements ActionListener { } return -1; } - public boolean isOperator(char c) - { + public boolean isOperator(char c) { return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%'); } - public int posOfLastOperatorOne(String s) - { + public int posOfLastOperatorOne(String s) { for(int i=s.length()-1;i>=0;i--) { if(isOperatorOne(s.charAt(i))) @@ -373,8 +661,7 @@ public class Calculator extends JFrame implements ActionListener { } return -1; } - public boolean isOperatorOne(char c) - { + public boolean isOperatorOne(char c) { return c=='('; } } diff --git a/src/java2022spring/MainClass.java b/src/java2022spring/MainClass.java index d1ed6a8..2bcce35 100644 --- a/src/java2022spring/MainClass.java +++ b/src/java2022spring/MainClass.java @@ -8,7 +8,7 @@ public static void main(String arg[]) a.setTitle("标准计算器"); a.setLayout(null);//清空布局 a.setResizable(true); - a.setBounds(500, 150, 300, 450); + a.setBounds(500, 150, 300, 530); a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); a.setVisible(true); a.validate(); -- Gitee From 00366e96e61c298dc798a90053f39d0189dec11f Mon Sep 17 00:00:00 2001 From: pandora-box Date: Fri, 27 May 2022 20:38:51 +0800 Subject: [PATCH 08/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B8=83=E5=B1=80?= =?UTF-8?q?=EF=BC=8C=E6=95=B4=E4=B8=AA=E7=AA=97=E5=8F=A3=E6=94=B9=E4=B8=BA?= =?UTF-8?q?BorderLayout=E5=B8=83=E5=B1=80=EF=BC=8C=E5=9C=A8=E8=AF=A5?= =?UTF-8?q?=E5=B8=83=E5=B1=80=E4=B8=8A=E6=96=B9=E5=86=8D=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=B8=80=E4=B8=AABorderLayout=E6=94=BE=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=A1=86=EF=BC=8C=E5=9C=A8=E8=AF=A5=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E4=B8=8B=E6=96=B9=E8=AE=BE=E7=BD=AEGridLayout=E6=94=BE?= =?UTF-8?q?=E7=BD=AE=E6=8C=89=E9=92=AE=E3=80=82=E5=8F=A6=E5=A4=96=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=AD=A3=E8=B4=9F=E5=8F=B7=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=92=8Ce^x=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 150 ++++++++++++++++++++++++----- src/java2022spring/MainClass.java | 9 +- 2 files changed, 126 insertions(+), 33 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 7cf8645..a875e04 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,39 +1,50 @@ package java2022spring; import javax.swing.*; + +import java.awt.BorderLayout; +import java.awt.Dimension; import java.awt.Font; +import java.awt.GridLayout; +import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","BS","1/x","sqr","x^2","e","π","(",")","÷","7","8","9","×","4","5","6","-","1","2","3","+","x^3","0",".","=","sin","cos","tan","lg","ln","x!"}; + String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x"}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; String l; Pattern pattern=Pattern.compile("[+-×%]"); public Calculator() { - text.setBounds(18, 5, 258, 48); - text.setHorizontalAlignment(JTextField.RIGHT); - text.setEditable(false); - add(text); - Font font=new Font("Times New Roman",Font.PLAIN,18); - text.setFont(font); - int x=20,y=65; + JFrame jframe=new JFrame(); + text.setPreferredSize(new Dimension (150,50)); + text.setHorizontalAlignment(JTextField.RIGHT); + JPanel jp1,jp2,jp3; + jframe.setTitle("科学计算器"); + jp1 = new JPanel(); + jp2 = new JPanel(); + jp3 = new JPanel(); + jp1.setLayout(new BorderLayout()); + jp1.add(jp2,"North"); + jp1.add(jp3,"Center"); + jframe.add(jp1); + jp2.setLayout(new BorderLayout()); + jp2.add(text,"South"); + text.setEditable(false); + jp3.setLayout(new GridLayout(8,5,4,5)); + jframe.setBounds(500, 100, 450, 430); + jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + jframe.setVisible(true); + text.setFont(new Font("Times New Roman",Font.PLAIN,22)); for (int i=0;i=0) { - c=c.substring(0, pos+1); + c=c.substring(0, pos+1); text.setText(c); } - else + else { c=""; text.setText("0.0"); } + } else if (label=="sqr") { String s,a,b; if(c=="") @@ -168,6 +180,74 @@ public class Calculator extends JFrame implements ActionListener { } } } + else if(label=="10^x") { + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationSeven(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + b=operationSeven(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationSeven(c); + text.setText(s); + c=s; + } + } + } + else if(label=="e^x") { + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + b=operationEight(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + b=operationEight(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + } + else { + s=operationEight(c); + text.setText(s); + c=s; + } + } + } else if(label=="1/x"){ String s,a,b; if(c=="") @@ -437,7 +517,7 @@ public class Calculator extends JFrame implements ActionListener { } } else if (label=="e") { - String s=String.valueOf(2.71828182846); + String s=String.valueOf(2.718281828459); c=c+s; text.setText(c); } @@ -445,6 +525,10 @@ public class Calculator extends JFrame implements ActionListener { String s=String.valueOf(3.141592653589); c=c+s; text.setText(c); + } + else if(label=="+/-") { + c=c+"-"; + text.setText(c); } else { c=c+label; @@ -457,9 +541,9 @@ public class Calculator extends JFrame implements ActionListener { String outcome[]=new String[200]; int top=-1,j=0; for (int i=0;i=0) { + if ("-0123456789.".indexOf(str.charAt(i))>=0) { s=""; - for (;i=0;i++) { + for (;i=0;i++) { s=s+str.charAt(i); } i--; @@ -642,6 +726,22 @@ public class Calculator extends JFrame implements ActionListener { String s=String.valueOf(p); return s; } + public String operationSeven(String str) + { + String result; + double a=Double.parseDouble(str),b=0; + b=Math.pow(10,a); + result=String.valueOf(b); + return result; + } + public String operationEight(String str) + { + String result; + double a=Double.parseDouble(str),b=0; + b=Math.pow(2.718281828459,a); + result=String.valueOf(b); + return result; + } public int posOfLastOperator(String s) { for(int i=s.length()-1;i>=0;i--) { diff --git a/src/java2022spring/MainClass.java b/src/java2022spring/MainClass.java index 2bcce35..435ffa0 100644 --- a/src/java2022spring/MainClass.java +++ b/src/java2022spring/MainClass.java @@ -4,13 +4,6 @@ import javax.swing.JFrame; public class MainClass{ public static void main(String arg[]) { - Calculator a=new Calculator(); - a.setTitle("标准计算器"); - a.setLayout(null);//清空布局 - a.setResizable(true); - a.setBounds(500, 150, 300, 530); - a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - a.setVisible(true); - a.validate(); + new Calculator(); } } \ No newline at end of file -- Gitee From e4444e02a7669fcccd1cdeed2604d114f5337304 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 29 May 2022 10:05:54 +0800 Subject: [PATCH 09/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86cot=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/Calculator.java | 43 +++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index a875e04..deecfe3 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -12,7 +12,7 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x"}; + String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x","cot"}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; @@ -146,6 +146,46 @@ public class Calculator extends JFrame implements ActionListener { } } } + else if(label=="cot"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.cos(l)/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.cos(l)/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=Math.cos(p)/Math.sin(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } else if(label=="x^3") { String s,a,b; if(c=="") @@ -530,6 +570,7 @@ public class Calculator extends JFrame implements ActionListener { c=c+"-"; text.setText(c); } + else { c=c+label; text.setText(c); -- Gitee From d261b22597aa4adbb07799a63bf74987997b121c Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 5 Jun 2022 23:35:30 +0800 Subject: [PATCH 10/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sec=E3=80=81csc?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 84 +++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index deecfe3..4c1ac17 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -12,7 +12,7 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x","cot"}; + String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x","cot","sec","csc"}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; @@ -147,7 +147,7 @@ public class Calculator extends JFrame implements ActionListener { } } else if(label=="cot"){ - String s,a,b; + String s,a; if(c=="") c=""; else { @@ -186,6 +186,86 @@ public class Calculator extends JFrame implements ActionListener { } } } + else if(label=="sec"){ + String s,a; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=1/Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=1/Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=1/Math.cos(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } + else if(label=="csc"){ + String s,a; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=1/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=1/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + } + else { + double p=Double.parseDouble(c); + double l=1/Math.sin(p); + s=String.valueOf(l); + text.setText(s); + c=s; + } + } + } else if(label=="x^3") { String s,a,b; if(c=="") -- Gitee From 1b470c790f870a094143a52b6b1d1594d3d5679b Mon Sep 17 00:00:00 2001 From: pandora-box Date: Sun, 5 Jun 2022 23:39:12 +0800 Subject: [PATCH 11/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sec=E3=80=81csc?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 4c1ac17..08c5c38 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -12,7 +12,14 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","Back","sin","1/x","sqr","x^2","÷","cos","π","e","x^3","×","tan","7","8","9","-","lg","4","5","6","+","ln","1","2","3","=","x!","(",")","0",".","10^x","+/-","e^x","cot","sec","csc"}; + String[] keysName={"%","C","CE","Back","sin", + "1/x","sqr","x^2","÷","cos", + "π","e","x^3","×","tan", + "7","8","9","-","lg", + "4","5","6","+","ln", + "1","2","3","=","x!", + "(",")","0",".","10^x", + "+/-","e^x","cot","sec","csc"}; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; -- Gitee From 9b69e5e290b7b03004b1ef075c644ac5a8ade1f4 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Mon, 6 Jun 2022 17:55:56 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mr,ms,mc,m+,m-=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/Calculator.java | 42 ++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 08c5c38..cf85951 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -12,18 +12,22 @@ import java.awt.event.ActionListener; import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { - String[] keysName={"%","C","CE","Back","sin", + String[] keysName={"MC","MS","MR","M+","M-", + "%","C","CE","Back","sin", "1/x","sqr","x^2","÷","cos", "π","e","x^3","×","tan", "7","8","9","-","lg", "4","5","6","+","ln", "1","2","3","=","x!", "(",")","0",".","10^x", - "+/-","e^x","cot","sec","csc"}; + "+/-","e^x","cot","sec","csc", + }; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); String c=""; String l; + boolean action; + String space; Pattern pattern=Pattern.compile("[+-×%]"); public Calculator() { JFrame jframe=new JFrame(); @@ -41,8 +45,8 @@ public class Calculator extends JFrame implements ActionListener { jp2.setLayout(new BorderLayout()); jp2.add(text,"South"); text.setEditable(false); - jp3.setLayout(new GridLayout(8,5,4,5)); - jframe.setBounds(500, 100, 450, 430); + jp3.setLayout(new GridLayout(9,5,4,5)); + jframe.setBounds(500, 100, 450, 450); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); text.setFont(new Font("Times New Roman",Font.PLAIN,22)); @@ -60,7 +64,35 @@ public class Calculator extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); boolean flag=false; - if(label=="=") { + if(label=="MS") { + space=text.getText(); + action=true; + + } + else if(label=="MC"&&action==true) { + space=""; + } + else if(label=="MR"&&action==true) { + c=space; + text.setText(c); + } + else if(label=="M+"&&action==true) { + double n1=Double.valueOf(text.getText()); + double n2=Double.valueOf(space); + n2=n1+n2; + space=String.valueOf(n2); + c=space; + text.setText(c); + } + else if(label=="M-"&&action==true) { + double n1=Double.valueOf(text.getText()); + double n2=Double.valueOf(space); + n2=n2-n1; + space=String.valueOf(n2); + c=space; + text.setText(c); + } + else if(label=="=") { String s[]=operationOne(c); String result=outCome(s); c=result; -- Gitee From 2a805142fdda26552c7e4d0f7594ec0e61cd6e08 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Mon, 6 Jun 2022 19:09:34 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B8=83=E5=B1=80?= =?UTF-8?q?=EF=BC=8C=E6=A8=A1=E4=BB=BFwin10=E9=A1=B5=E9=9D=A2=EF=BC=8C?= =?UTF-8?q?=E5=8F=82=E7=85=A7CSDN=E5=8E=9F=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 73 ++++++++++++++++++------------ 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index cf85951..2079cca 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -3,6 +3,7 @@ package java2022spring; import javax.swing.*; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; @@ -14,13 +15,13 @@ import java.util.regex.Pattern; public class Calculator extends JFrame implements ActionListener { String[] keysName={"MC","MS","MR","M+","M-", "%","C","CE","Back","sin", - "1/x","sqr","x^2","÷","cos", - "π","e","x^3","×","tan", - "7","8","9","-","lg", - "4","5","6","+","ln", - "1","2","3","=","x!", - "(",")","0",".","10^x", - "+/-","e^x","cot","sec","csc", + "1/x","sqr","x^2","x^3","cos", + "x!","e","e^x","10^x","tan", + "(",")","π","÷","cot", + "7","8","9","×","sec", + "4","5","6","-","csc", + "1","2","3","+","lg", + "+/-","0",".","=","ln", }; JButton keys[]=new JButton[keysName.length]; JTextField text = new JTextField("0.0"); @@ -31,32 +32,47 @@ public class Calculator extends JFrame implements ActionListener { Pattern pattern=Pattern.compile("[+-×%]"); public Calculator() { JFrame jframe=new JFrame(); - text.setPreferredSize(new Dimension (150,50)); - text.setHorizontalAlignment(JTextField.RIGHT); - JPanel jp1,jp2,jp3; - jframe.setTitle("科学计算器"); - jp1 = new JPanel(); - jp2 = new JPanel(); - jp3 = new JPanel(); - jp1.setLayout(new BorderLayout()); - jp1.add(jp2,"North"); - jp1.add(jp3,"Center"); - jframe.add(jp1); - jp2.setLayout(new BorderLayout()); - jp2.add(text,"South"); - text.setEditable(false); - jp3.setLayout(new GridLayout(9,5,4,5)); + Color color1=new Color(181,181,181); + Color color2=new Color(126,192,238);//等号颜色 + Color color3=new Color(232,232,232);//背景颜色、功能键和运算符的颜色 + JPanel textPanel=new JPanel();//建立一个画板放文本框 + textPanel.setLayout(new BorderLayout()); + textPanel.add(text); + // text.setPreferredSize(new Dimension (150,50)); + text.setHorizontalAlignment(JTextField.RIGHT); + text.setFont(new Font("楷体",Font.BOLD,35)); + text.setEditable(false); + text.setBorder(null);//删除文本框的边框 + text.setBackground(color1);//设置文本框的颜色 + JPanel keysPanel=new JPanel(); + keysPanel.setLayout(new GridLayout(9,5,4,5)); + jframe.setTitle("常用计算器"); jframe.setBounds(500, 100, 450, 450); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); - text.setFont(new Font("Times New Roman",Font.PLAIN,22)); for (int i=0;i Date: Mon, 6 Jun 2022 21:39:06 +0800 Subject: [PATCH 14/20] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 599 ++++++++++++++--------------- 1 file changed, 296 insertions(+), 303 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 2079cca..5f469f3 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,13 +1,10 @@ package java2022spring; import javax.swing.*; - import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; -import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Pattern; @@ -22,43 +19,42 @@ public class Calculator extends JFrame implements ActionListener { "4","5","6","-","csc", "1","2","3","+","lg", "+/-","0",".","=","ln", - }; - JButton keys[]=new JButton[keysName.length]; + };//设置按钮名称 + JButton keys[]=new JButton[keysName.length];//设置按钮 JTextField text = new JTextField("0.0"); String c=""; String l; boolean action; String space; - Pattern pattern=Pattern.compile("[+-×%]"); - public Calculator() { + Pattern pattern=Pattern.compile("[+-×%]");//正则表达式 + public Calculator() { JFrame jframe=new JFrame(); Color color1=new Color(181,181,181); - Color color2=new Color(126,192,238);//等号颜色 + Color color2=new Color(126,192,238);//"="号颜色 Color color3=new Color(232,232,232);//背景颜色、功能键和运算符的颜色 JPanel textPanel=new JPanel();//建立一个画板放文本框 textPanel.setLayout(new BorderLayout()); - textPanel.add(text); - // text.setPreferredSize(new Dimension (150,50)); - text.setHorizontalAlignment(JTextField.RIGHT); - text.setFont(new Font("楷体",Font.BOLD,35)); - text.setEditable(false); + textPanel.add(text);//将文本框放置面板中 + text.setHorizontalAlignment(JTextField.RIGHT);//设置文本框内容靠右 + text.setFont(new Font("楷体",Font.BOLD,35));//设置文本框字体 + text.setEditable(false);//设置文本框不可修改 text.setBorder(null);//删除文本框的边框 text.setBackground(color1);//设置文本框的颜色 JPanel keysPanel=new JPanel(); - keysPanel.setLayout(new GridLayout(9,5,4,5)); - jframe.setTitle("常用计算器"); - jframe.setBounds(500, 100, 450, 450); - jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - jframe.setVisible(true); + keysPanel.setLayout(new GridLayout(9,5,4,5));//设置按钮面板 + jframe.setTitle("常用计算器");//窗口名称 + jframe.setBounds(500, 100, 450, 450);//窗口位置大小 + jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭方式 + jframe.setVisible(true);//窗口可视 for (int i=0;i=0) { + int pos=posOfLastOperator(c); + if(pos>=0) { c=c.substring(0, pos+1); text.setText(c); - } - else { - c=""; - text.setText("0.0"); + } + else { + c=""; + text.setText("0.0"); } - } + }//将最新输入的运算符号后面的数字清空 else if (label=="sqr") { String s,a,b; if(c=="") - c=""; + c="";//若为空串,置为空串 else{ - int pos=posOfLastOperator(c); - int position=posOfLastOperatorOne(c); + int pos=posOfLastOperator(c);//获取最后一个运算符的位置(+-x÷%) + int position=posOfLastOperatorOne(c);//获取"("的位置,实现根号(2+3)即根号5的运算 if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - b=operationTwo(result); + p=operationOne(a);//将"("后面的运算式分开,数字和运算符分开,按算术法则带入outCome方法运算 + String result=outCome(p);//等号运算得出结果 + b=operationTwo(result);//将结果进行根号运算 StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),b); + builder.replace(position,c.length(),b);//将运算结果替换运算式 c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); b=operationTwo(a); @@ -158,14 +155,14 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } - else { + }//没括号,只有(+-x÷%) + else { s=operationTwo(c); text.setText(s); c=s; - } - } - } + }//只有数字 + } + }//数字根号运算 else if(label=="x^2") { String s,a,b; if(c=="") @@ -176,15 +173,14 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); - String result=outCome(p); - b=operationThree(result); + String result=outCome(p);//等号运算得出结果 + b=operationThree(result);//将得出结果进行平方运算 StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),b); + builder.replace(position,c.length(),b);//将运算结果替换运算式 c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); b=operationThree(a); @@ -192,14 +188,14 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { s=operationThree(c); text.setText(s); c=s; - } - } - } + }//只有数字 + } + }//平方运算 else if(label=="cot"){ String s,a; if(c=="") @@ -210,7 +206,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); double l=Double.parseDouble(result); @@ -220,26 +215,26 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),s); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.cos(l)/Math.sin(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + double l=Double.parseDouble(a); + double x=Math.cos(l)/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); - double l=Math.cos(p)/Math.sin(p); - s=String.valueOf(l); - text.setText(s); - c=s; - } + double p=Double.parseDouble(c); + double l=Math.cos(p)/Math.sin(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 } - } + }//三角函数tan运算 else if(label=="sec"){ String s,a; if(c=="") @@ -250,7 +245,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); double l=Double.parseDouble(result); @@ -260,26 +254,26 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),s); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=1/Math.cos(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + double l=Double.parseDouble(a); + double x=1/Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { double p=Double.parseDouble(c); double l=1/Math.cos(p); s=String.valueOf(l); - text.setText(s); - c=s; - } - } - } + text.setText(s); + c=s; + }//只有数字 + } + }//三角函数sec运算 else if(label=="csc"){ String s,a; if(c=="") @@ -290,7 +284,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); double l=Double.parseDouble(result); @@ -300,26 +293,26 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),s); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=1/Math.sin(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=1/Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); + double p=Double.parseDouble(c); double l=1/Math.sin(p); s=String.valueOf(l); - text.setText(s); - c=s; - } - } - } + text.setText(s); + c=s; + }//只有数字 + } + }//三角函数csc运算 else if(label=="x^3") { String s,a,b; if(c=="") @@ -330,7 +323,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); b=operationFive(result); @@ -338,7 +330,7 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),b); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); b=operationFive(a); @@ -346,14 +338,14 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { - s=operationFive(c); - text.setText(s); - c=s; - } + s=operationFive(c); + text.setText(s); + c=s; + }//只有数字 } - } + }//立方运算 else if(label=="10^x") { String s,a,b; if(c=="") @@ -364,7 +356,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); b=operationSeven(result); @@ -372,7 +363,7 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),b); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); b=operationSeven(a); @@ -380,14 +371,14 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { - s=operationSeven(c); - text.setText(s); - c=s; - } + s=operationSeven(c); + text.setText(s); + c=s; + }//只有数字 } - } + }//10^x运算 else if(label=="e^x") { String s,a,b; if(c=="") @@ -398,7 +389,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); b=operationEight(result); @@ -406,22 +396,22 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),b); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { - a=c.substring(pos+1); + a=c.substring(pos+1); b=operationEight(a); StringBuilder builder=new StringBuilder(c); builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { s=operationEight(c); text.setText(s); c=s; - } + }//只有数字 } - } + }//e^x运算 else if(label=="1/x"){ String s,a,b; if(c=="") @@ -432,30 +422,29 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); - b=operationFour(result); + b=operationFour(result);//0的倒数置为错误error StringBuilder builder=new StringBuilder(c); builder.replace(position,c.length(),b); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { - a=c.substring(pos+1); - b=operationFour(a); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),b); - c=builder.toString(); - text.setText(c); - } + a=c.substring(pos+1); + b=operationFour(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { s=operationFour(c); - text.setText(s); - c=s; - } + text.setText(s); + c=s; + }//只有数字 } - } + }//倒数运算 else if(label=="sin"){ String s,a,b; if(c=="") @@ -466,7 +455,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); double l=Double.parseDouble(result); @@ -476,7 +464,7 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),s); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { a=c.substring(pos+1); double l=Double.parseDouble(a); @@ -486,16 +474,16 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(pos+1,c.length(),s); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { double p=Double.parseDouble(c); double l=Math.sin(p); s=String.valueOf(l); text.setText(s); c=s; - } + }//只有数字 } - } + }//三角函数sin运算 else if(label=="cos"){ String s,a,b; if(c=="") @@ -506,7 +494,6 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=a.split(","); p=operationOne(a); String result=outCome(p); double l=Double.parseDouble(result); @@ -516,200 +503,198 @@ public class Calculator extends JFrame implements ActionListener { builder.replace(position,c.length(),s); c=builder.toString(); text.setText(c); - } + }//有括号 else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.cos(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); + double p=Double.parseDouble(c); double l=Math.cos(p); s=String.valueOf(l); - text.setText(s); - c=s; - } + text.setText(s); + c=s; + }//只有数字 } - } + }//三角函数cos运算 else if(label=="tan"){ String s,a,b; if(c=="") c=""; - else { + else { int pos=posOfLastOperator(c); int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.tan(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - } - else if(pos>=0) { + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.tan(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.tan(l); - s=String.valueOf(x); + double l=Double.parseDouble(a); + double x=Math.tan(l); + s=String.valueOf(x); StringBuilder builder=new StringBuilder(c); builder.replace(pos+1,c.length(),s); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); - double l=Math.tan(p); - s=String.valueOf(l); - text.setText(s); - c=s; - } + double p=Double.parseDouble(c); + double l=Math.tan(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 } - } + }//三角函数tan运算 else if(label=="lg"){ String s,a,b; if(c=="") c=""; - else { + else { int pos=posOfLastOperator(c); int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.log10(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - } - else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.log10(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.log10(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.log10(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); + double p=Double.parseDouble(c); double l=Math.log10(p); s=String.valueOf(l); text.setText(s); c=s; - } + }//只有数字 } - } + }//lg运算 else if(label=="ln"){ String s,a; if(c=="") c=""; - else { + else { int pos=posOfLastOperator(c); int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.log(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - } - else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.log(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - } + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.log(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.log(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) else { - double p=Double.parseDouble(c); - double l=Math.log(p); - s=String.valueOf(l); - text.setText(s); - c=s; - } + double p=Double.parseDouble(c); + double l=Math.log(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 } - } + }//ln运算 else if(label=="x!"){ String s,a,b; if(c=="") c=""; - else { + else { int pos=posOfLastOperator(c); int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - b=operationSix(result); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),b); - c=builder.toString(); - text.setText(c); - } - else if(pos>=0) { - a=c.substring(pos+1); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=operationOne(a); + String result=outCome(p); + b=operationSix(result); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); b=operationSix(a); StringBuilder builder=new StringBuilder(c); builder.replace(pos+1,c.length(),b); c=builder.toString(); text.setText(c); - } + }//没括号,只有(+-x÷%) else { s=operationSix(c); text.setText(s); c=s; - } + }//只有数字 } - } + }//x!运算 else if (label=="e") { String s=String.valueOf(2.718281828459); c=c+s; text.setText(c); - } + }//得出e的结果 else if (label=="π") { String s=String.valueOf(3.141592653589); c=c+s; text.setText(c); - } + }//得出π的结果 else if(label=="+/-") { c=c+"-"; text.setText(c); - } - - else { + }//正负号运算 + else { c=c+label; text.setText(c); + } } - } + //将算术式拆分成数组,运用栈的相关知识,得出运算优先级()、%、×、÷、+、-,以下计算原理借鉴CSDN博客qq_41398808 String []operationOne(String str) { String s=""; char a[]=new char[200]; @@ -799,7 +784,7 @@ public class Calculator extends JFrame implements ActionListener { int Top=-1; Pattern reg = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); for (int i=0;str[i]!=null;i++) { - if (reg.matcher(str[i]).matches()) { //数字字符进栈 + if (reg.matcher(str[i]).matches()) { //数字字符进栈 Top++; result[Top]=str[i]; } @@ -850,24 +835,24 @@ public class Calculator extends JFrame implements ActionListener { } return result[Top]; } - public String operationTwo(String str) - { + + public String operationTwo(String str) { String result; double a=Double.parseDouble(str),b=0; b=Math.sqrt(a); result=String.valueOf(b); return result; - } - public String operationThree(String str) - { + }//根号运算 + + public String operationThree(String str) { String result; double a=Double.parseDouble(str),b=0; b=Math.pow(a, 2); result=String.valueOf(b); return result; - } - public String operationFour(String str) - { + }//平方运算 + + public String operationFour(String str) { String result; double a=Double.parseDouble(str),b=0; String s; @@ -880,14 +865,16 @@ public class Calculator extends JFrame implements ActionListener { result=String.valueOf(b); return result; } - } + }//倒数运算 + public String operationFive(String str) { String result; double a=Double.parseDouble(str),b=0; b=Math.pow(a, 3); result=String.valueOf(b); return result; - } + }//立方运算 + public String operationSix(String str) { int ee; double p=Double.parseDouble(str); @@ -900,7 +887,8 @@ public class Calculator extends JFrame implements ActionListener { } String s=String.valueOf(p); return s; - } + }//n!运算 + public String operationSeven(String str) { String result; @@ -908,7 +896,8 @@ public class Calculator extends JFrame implements ActionListener { b=Math.pow(10,a); result=String.valueOf(b); return result; - } + }//10^x运算 + public String operationEight(String str) { String result; @@ -916,7 +905,8 @@ public class Calculator extends JFrame implements ActionListener { b=Math.pow(2.718281828459,a); result=String.valueOf(b); return result; - } + }//e^x运算 + public int posOfLastOperator(String s) { for(int i=s.length()-1;i>=0;i--) { @@ -924,10 +914,12 @@ public class Calculator extends JFrame implements ActionListener { return i; } return -1; - } + }//获取传入字符串最后一个运算符的位置 + public boolean isOperator(char c) { return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%'); } + public int posOfLastOperatorOne(String s) { for(int i=s.length()-1;i>=0;i--) { @@ -935,7 +927,8 @@ public class Calculator extends JFrame implements ActionListener { return i; } return -1; - } + }//获取传入字符串中括号的位置 + public boolean isOperatorOne(char c) { return c=='('; } -- Gitee From aa4de6d16f30f7dd2f26852f45e435ceb6e6b72d Mon Sep 17 00:00:00 2001 From: pandora-box Date: Mon, 6 Jun 2022 21:44:01 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 5f469f3..1448b85 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,10 +1,10 @@ package java2022spring; import javax.swing.*; -import java.awt.BorderLayout; -import java.awt.Color; +import java.awt.*; +/*import java.awt.Color; import java.awt.Font; -import java.awt.GridLayout; +import java.awt.GridLayout;*/ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Pattern; -- Gitee From b8d87520c5730898427101263863faa76e619edf Mon Sep 17 00:00:00 2001 From: pandora-box Date: Thu, 9 Jun 2022 19:52:39 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 39 +++++++++++++++++++++--------- src/java2022spring/MainClass.java | 2 +- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 1448b85..01e8a95 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -2,12 +2,10 @@ package java2022spring; import javax.swing.*; import java.awt.*; -/*import java.awt.Color; -import java.awt.Font; -import java.awt.GridLayout;*/ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Pattern; +import java.math.BigDecimal; public class Calculator extends JFrame implements ActionListener { String[] keysName={"MC","MS","MR","M+","M-", @@ -25,8 +23,10 @@ public class Calculator extends JFrame implements ActionListener { String c=""; String l; boolean action; + boolean flag; String space; Pattern pattern=Pattern.compile("[+-×%]");//正则表达式 + Pattern reg = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); public Calculator() { JFrame jframe=new JFrame(); Color color1=new Color(181,181,181); @@ -42,6 +42,7 @@ public class Calculator extends JFrame implements ActionListener { text.setBackground(color1);//设置文本框的颜色 JPanel keysPanel=new JPanel(); keysPanel.setLayout(new GridLayout(9,5,4,5));//设置按钮面板 + keysPanel.setBackground(color1);//设置按钮面板颜色 jframe.setTitle("常用计算器");//窗口名称 jframe.setBounds(500, 100, 450, 450);//窗口位置大小 jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭方式 @@ -64,7 +65,6 @@ public class Calculator extends JFrame implements ActionListener { for(int i=40;i<43;i++) keys[i].setBackground(Color.white);//设置数字按钮特殊颜色 keys[43].setBackground(color2);//设置"="号特殊颜色 - keysPanel.setBackground(color1);//设置按钮面板颜色 jframe.getContentPane().add("North",textPanel);//将画板放在窗口北面 jframe.getContentPane().add("Center",keysPanel);//将按钮面板放在窗口中心 textPanel.setBorder(BorderFactory.createMatteBorder(15,3,1,3,color1));//设置边框 @@ -77,7 +77,6 @@ public class Calculator extends JFrame implements ActionListener { //actionPerformed方法设置各个功能键的用途 public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); - boolean flag=false; if(label=="MS") { space=text.getText(); action=true; @@ -106,9 +105,11 @@ public class Calculator extends JFrame implements ActionListener { text.setText(c); }//获取文本框的数字实现,实现和记忆存储区的数字相减(存储区-文本框) else if(label=="=") { + c="0"+c; String s[]=operationOne(c); String result=outCome(s); c=result; + text.setText(c); }//等号运算 else if(label=="C") { @@ -132,9 +133,14 @@ public class Calculator extends JFrame implements ActionListener { }//将最新输入的运算符号后面的数字清空 else if (label=="sqr") { String s,a,b; - if(c=="") + Boolean d = c.matches("-[0-9]+.*[0-9]*"); + if(c=="") c="";//若为空串,置为空串 - else{ + else if(d==true) { + c="error"; + text.setText(c); + } + else { int pos=posOfLastOperator(c);//获取最后一个运算符的位置(+-x÷%) int position=posOfLastOperatorOne(c);//获取"("的位置,实现根号(2+3)即根号5的运算 if(position>=0) { @@ -644,8 +650,13 @@ public class Calculator extends JFrame implements ActionListener { }//ln运算 else if(label=="x!"){ String s,a,b; + Boolean d = c.matches("-[0-9]+.*[0-9]*"); if(c=="") c=""; + else if(d==true) { + c="error"; + text.setText(c); + } else { int pos=posOfLastOperator(c); int position=posOfLastOperatorOne(c); @@ -686,7 +697,11 @@ public class Calculator extends JFrame implements ActionListener { text.setText(c); }//得出π的结果 else if(label=="+/-") { - c=c+"-"; + if(reg.matcher(c).matches()) { + StringBuffer c1=new StringBuffer(c); + c1.insert(0, "-"); + c=c1.toString(); + } text.setText(c); }//正负号运算 else { @@ -701,9 +716,9 @@ public class Calculator extends JFrame implements ActionListener { String outcome[]=new String[200]; int top=-1,j=0; for (int i=0;i=0) { + if ("0123456789.".indexOf(str.charAt(i))>=0) { s=""; - for (;i=0;i++) { + for (;i=0;i++) { s=s+str.charAt(i); } i--; @@ -782,7 +797,6 @@ public class Calculator extends JFrame implements ActionListener { public String outCome(String str[]) { String result[]=new String[200]; int Top=-1; - Pattern reg = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); for (int i=0;str[i]!=null;i++) { if (reg.matcher(str[i]).matches()) { //数字字符进栈 Top++; @@ -833,6 +847,9 @@ public class Calculator extends JFrame implements ActionListener { } } } + double z=Double.parseDouble(result[Top]); + String zz=String.format("%.1f", z); + result[Top]=zz; return result[Top]; } diff --git a/src/java2022spring/MainClass.java b/src/java2022spring/MainClass.java index 435ffa0..904a468 100644 --- a/src/java2022spring/MainClass.java +++ b/src/java2022spring/MainClass.java @@ -1,6 +1,6 @@ package java2022spring; -import javax.swing.JFrame; + public class MainClass{ public static void main(String arg[]) { -- Gitee From 656a3d068e5777db383dd358f813268551f9c4f0 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Thu, 9 Jun 2022 22:25:46 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E6=9B=B4=E6=96=B02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 310 ++++++++++++++--------------- 1 file changed, 155 insertions(+), 155 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 01e8a95..7e552a7 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -76,7 +76,7 @@ public class Calculator extends JFrame implements ActionListener { //actionPerformed方法设置各个功能键的用途 public void actionPerformed(ActionEvent e) { - String label = e.getActionCommand(); + String label = e.getActionCommand(); //获取按钮相应字符串 if(label=="MS") { space=text.getText(); action=true; @@ -179,7 +179,7 @@ public class Calculator extends JFrame implements ActionListener { if(position>=0) { String[] p; a=c.substring(position+1,c.length()-1); - p=operationOne(a); + p=operationOne(a);//将"("后面的运算式分开,数字和运算符分开,按算术法则带入outCome方法运算 String result=outCome(p);//等号运算得出结果 b=operationThree(result);//将得出结果进行平方运算 StringBuilder builder=new StringBuilder(c); @@ -202,6 +202,157 @@ public class Calculator extends JFrame implements ActionListener { }//只有数字 } }//平方运算 + else if(label=="x^3") { + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=operationOne(a);//将"("后面的运算式分开,数字和运算符分开,按算术法则带入outCome方法运算 + String result=outCome(p);//等号运算得出结果 + b=operationFive(result);//将得出结果进行平立方运算 + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),b);//将运算结果替换运算式 + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + b=operationFive(a); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),b); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) + else { + s=operationFive(c); + text.setText(s); + c=s; + }//只有数字 + } + }//立方运算 + else if(label=="sin"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=operationOne(a);//将"("后面的运算式分开,数字和运算符分开,按算术法则带入outCome方法运算 + String result=outCome(p);//等号运算得出结果 + double l=Double.parseDouble(result); + double x=Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) + else { + double p=Double.parseDouble(c); + double l=Math.sin(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 + } + }//三角函数sin运算 + else if(label=="cos"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.cos(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) + else { + double p=Double.parseDouble(c); + double l=Math.cos(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 + } + }//三角函数cos运算 + else if(label=="tan"){ + String s,a,b; + if(c=="") + c=""; + else { + int pos=posOfLastOperator(c); + int position=posOfLastOperatorOne(c); + if(position>=0) { + String[] p; + a=c.substring(position+1,c.length()-1); + p=a.split(","); + p=operationOne(a); + String result=outCome(p); + double l=Double.parseDouble(result); + double x=Math.tan(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(position,c.length(),s); + c=builder.toString(); + text.setText(c); + }//有括号 + else if(pos>=0) { + a=c.substring(pos+1); + double l=Double.parseDouble(a); + double x=Math.tan(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); + }//没括号,只有(+-x÷%) + else { + double p=Double.parseDouble(c); + double l=Math.tan(p); + s=String.valueOf(l); + text.setText(s); + c=s; + }//只有数字 + } + }//三角函数tan运算 else if(label=="cot"){ String s,a; if(c=="") @@ -240,7 +391,7 @@ public class Calculator extends JFrame implements ActionListener { c=s; }//只有数字 } - }//三角函数tan运算 + }//三角函数cot运算 else if(label=="sec"){ String s,a; if(c=="") @@ -319,40 +470,7 @@ public class Calculator extends JFrame implements ActionListener { }//只有数字 } }//三角函数csc运算 - else if(label=="x^3") { - String s,a,b; - if(c=="") - c=""; - else { - int pos=posOfLastOperator(c); - int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=operationOne(a); - String result=outCome(p); - b=operationFive(result); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),b); - c=builder.toString(); - text.setText(c); - }//有括号 - else if(pos>=0) { - a=c.substring(pos+1); - b=operationFive(a); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),b); - c=builder.toString(); - text.setText(c); - }//没括号,只有(+-x÷%) - else { - s=operationFive(c); - text.setText(s); - c=s; - }//只有数字 - } - }//立方运算 - else if(label=="10^x") { + else if(label=="10^x") { String s,a,b; if(c=="") c=""; @@ -451,124 +569,6 @@ public class Calculator extends JFrame implements ActionListener { }//只有数字 } }//倒数运算 - else if(label=="sin"){ - String s,a,b; - if(c=="") - c=""; - else { - int pos=posOfLastOperator(c); - int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.sin(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - }//有括号 - else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.sin(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - }//没括号,只有(+-x÷%) - else { - double p=Double.parseDouble(c); - double l=Math.sin(p); - s=String.valueOf(l); - text.setText(s); - c=s; - }//只有数字 - } - }//三角函数sin运算 - else if(label=="cos"){ - String s,a,b; - if(c=="") - c=""; - else { - int pos=posOfLastOperator(c); - int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.cos(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - }//有括号 - else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.cos(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - }//没括号,只有(+-x÷%) - else { - double p=Double.parseDouble(c); - double l=Math.cos(p); - s=String.valueOf(l); - text.setText(s); - c=s; - }//只有数字 - } - }//三角函数cos运算 - else if(label=="tan"){ - String s,a,b; - if(c=="") - c=""; - else { - int pos=posOfLastOperator(c); - int position=posOfLastOperatorOne(c); - if(position>=0) { - String[] p; - a=c.substring(position+1,c.length()-1); - p=a.split(","); - p=operationOne(a); - String result=outCome(p); - double l=Double.parseDouble(result); - double x=Math.tan(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); - c=builder.toString(); - text.setText(c); - }//有括号 - else if(pos>=0) { - a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.tan(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); - }//没括号,只有(+-x÷%) - else { - double p=Double.parseDouble(c); - double l=Math.tan(p); - s=String.valueOf(l); - text.setText(s); - c=s; - }//只有数字 - } - }//三角函数tan运算 else if(label=="lg"){ String s,a,b; if(c=="") -- Gitee From ca9f8ee24fc948fa9a17a09ffab25033107ad243 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Thu, 9 Jun 2022 23:23:29 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E6=9B=B4=E6=96=B03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 7e552a7..7fa2ae3 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -248,29 +248,29 @@ public class Calculator extends JFrame implements ActionListener { p=operationOne(a);//将"("后面的运算式分开,数字和运算符分开,按算术法则带入outCome方法运算 String result=outCome(p);//等号运算得出结果 double l=Double.parseDouble(result); - double x=Math.sin(l); - s=String.valueOf(x); + double x=Math.sin(l);//计算出sin结果 + s=String.valueOf(x);//将double型结果转换成字符串形式 StringBuilder builder=new StringBuilder(c); - builder.replace(position,c.length(),s); + builder.replace(position,c.length(),s);//将运算结果替换运算式 c=builder.toString(); text.setText(c); }//有括号 else if(pos>=0) { a=c.substring(pos+1); - double l=Double.parseDouble(a); - double x=Math.sin(l); - s=String.valueOf(x); - StringBuilder builder=new StringBuilder(c); - builder.replace(pos+1,c.length(),s); - c=builder.toString(); - text.setText(c); + double l=Double.parseDouble(a); + double x=Math.sin(l); + s=String.valueOf(x); + StringBuilder builder=new StringBuilder(c); + builder.replace(pos+1,c.length(),s); + c=builder.toString(); + text.setText(c); }//没括号,只有(+-x÷%) else { double p=Double.parseDouble(c); double l=Math.sin(p); s=String.valueOf(l); - text.setText(s); - c=s; + text.setText(s); + c=s; }//只有数字 } }//三角函数sin运算 @@ -935,7 +935,7 @@ public class Calculator extends JFrame implements ActionListener { public boolean isOperator(char c) { return (c=='+')||(c=='-')||(c=='×')||(c=='÷')||(c=='%'); - } + }//判断是否为+-x÷%符号 public int posOfLastOperatorOne(String s) { for(int i=s.length()-1;i>=0;i--) -- Gitee From 1168785f8f7b7c03bdbd0cd7246c6f3e93a346a8 Mon Sep 17 00:00:00 2001 From: pandora-box Date: Fri, 10 Jun 2022 00:29:41 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E6=9B=B4=E6=96=B05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 7fa2ae3..5caf667 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -709,7 +709,7 @@ public class Calculator extends JFrame implements ActionListener { text.setText(c); } } - //将算术式拆分成数组,运用栈的相关知识,得出运算优先级()、%、×、÷、+、-,以下计算原理借鉴CSDN博客qq_41398808 +//将算术式拆分成数组,运用栈的相关知识,得出运算优先级()、%、×、÷、+、-,方法operationOne和Outcome的计算原理(有关中缀表达式转后缀表达式,后缀表达式计算)借鉴CSDN博客qq_4139880 String []operationOne(String str) { String s=""; char a[]=new char[200]; -- Gitee From ebd14a05ecff7873381c875f067308914e50039b Mon Sep 17 00:00:00 2001 From: pandora-box Date: Fri, 10 Jun 2022 13:24:10 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E6=9C=80=E7=BB=88=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 5caf667..5c8ca3b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -712,44 +712,49 @@ public class Calculator extends JFrame implements ActionListener { //将算术式拆分成数组,运用栈的相关知识,得出运算优先级()、%、×、÷、+、-,方法operationOne和Outcome的计算原理(有关中缀表达式转后缀表达式,后缀表达式计算)借鉴CSDN博客qq_4139880 String []operationOne(String str) { String s=""; - char a[]=new char[200]; - String outcome[]=new String[200]; + char a[]=new char[200];//栈 + String outcome[]=new String[200];//后缀表达式字符串组 int top=-1,j=0; + //遍历中缀表达式 for (int i=0;i=0) { - s=""; + s="";//清空字符串 for (;i=0;i++) { s=s+str.charAt(i); } i--; - outcome[j]=s; + outcome[j]=s;//数字字符直接加入后缀表达式 j++; } - else if ("(".indexOf(str.charAt(i))>=0) { + else if ("(".indexOf(str.charAt(i))>=0)//当遇到左括号 + { top++; - a[top]=str.charAt(i); + a[top]=str.charAt(i);//左括号入栈 } - else if (")".indexOf(str.charAt(i))>=0) { + else if (")".indexOf(str.charAt(i))>=0) //当遇到右括号 + { for (;;) { if (a[top]!='(') { - outcome[j]=a[top]+""; + outcome[j]=a[top]+"";//栈顶元素出栈 j++; top--; } else { - top--; + top--;//删除栈顶的左括号 break; } } } - else if ("×%÷".indexOf(str.charAt(i))>=0) { - if (top==-1) { + else if ("×%÷".indexOf(str.charAt(i))>=0) //遇到高优先级运算符 + { + if (top==-1) //若栈为空直接入栈 + { top++; a[top]=str.charAt(i); } else { if ("×%÷".indexOf(a[top])>=0) { - outcome[j]=a[top]+""; + outcome[j]=a[top]+"";//栈顶元素出栈进入后缀表达式 j++; a[top]=str.charAt(i); } @@ -763,8 +768,10 @@ public class Calculator extends JFrame implements ActionListener { } } } - else if ("+-".indexOf(str.charAt(i))>=0) { - if (top==-1) { + else if ("+-".indexOf(str.charAt(i))>=0) //遇到低优先级运算符 + { + if (top==-1)//栈为空直接入栈 + { top++; a[top]=str.charAt(i); } @@ -787,7 +794,8 @@ public class Calculator extends JFrame implements ActionListener { } } - for (;top!=-1;) { + for (;top!=-1;) //遍历结束后将栈中剩余元素依次出栈进入后缀表达式 + { outcome[j]=a[top]+""; j++; top--; @@ -797,7 +805,8 @@ public class Calculator extends JFrame implements ActionListener { public String outCome(String str[]) { String result[]=new String[200]; int Top=-1; - for (int i=0;str[i]!=null;i++) { + for (int i=0;str[i]!=null;i++) //遍历后缀表达式 + { if (reg.matcher(str[i]).matches()) { //数字字符进栈 Top++; result[Top]=str[i]; @@ -811,7 +820,7 @@ public class Calculator extends JFrame implements ActionListener { if ("-".indexOf(str[i])>=0) { n=y-x; Top++; - result[Top]=String.valueOf(n); + result[Top]=String.valueOf(n);//将运算结果重新入栈 } else if ("+".indexOf(str[i])>=0) { n=y+x; -- Gitee