diff --git a/bin/java2022spring/Main.class b/bin/java2022spring/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..26b85ed03693c5ec364f83ccfe1a968985290b37 Binary files /dev/null and b/bin/java2022spring/Main.class differ diff --git a/bin/java2022spring/StandardCalculator.class b/bin/java2022spring/StandardCalculator.class new file mode 100644 index 0000000000000000000000000000000000000000..b0274c40b9e8112a0b3ecb18010d36fefa3bb2e5 Binary files /dev/null and b/bin/java2022spring/StandardCalculator.class differ diff --git a/readme.md b/readme.md deleted file mode 100644 index 4af16add33b576d095308a1888db35774618715d..0000000000000000000000000000000000000000 --- a/readme.md +++ /dev/null @@ -1,2 +0,0 @@ -# 科学计算器 -0103 张三 \ No newline at end of file diff --git a/src/java2022spring/1.txt b/src/java2022spring/1.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/java2022spring/Main.java b/src/java2022spring/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..6323eb41dfa4aba0d3264892f5160ed497e478bd --- /dev/null +++ b/src/java2022spring/Main.java @@ -0,0 +1,8 @@ +package java2022spring; +public class Main { + public static void main(String args[]) { + StandardCalculator win1=new StandardCalculator("小皓标准计算器",500,300,500,600); + } + +} + diff --git a/src/java2022spring/ScientificCalculator.java b/src/java2022spring/ScientificCalculator.java new file mode 100644 index 0000000000000000000000000000000000000000..ab736bda59ed7b8461224d70b0ae863f6ed74653 --- /dev/null +++ b/src/java2022spring/ScientificCalculator.java @@ -0,0 +1,255 @@ +package java2022spring; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.math.*; +import java.text.*; +public class ScientificCalculator extends JFrame implements ActionListener{ + JMenuBar menubar=new JMenuBar(); + JMenu moshi=new JMenu("妯″紡"); + JMenuItem menuitem1=new JMenuItem("鏍囧噯璁$畻鍣"); + JMenuItem menuitem2=new JMenuItem("绉戝璁$畻鍣"); + JTextField text=new JTextField(); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + int i=0; + boolean first; + double result; + String operator="="; + boolean operate; + public ScientificCalculator(String s,int x,int y,int z,int n) { + init(s); + this.setLocation(x,y); + setSize(z,n); + setVisible(true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + } + void init(String s) { + Color color0=new Color(125,200,240);// + Color color1=new Color(255,255,255); + Color color2=new Color(192,192,192); + setTitle(s); + this.setJMenuBar(menubar); + menubar.add(moshi); + menubar.add(moshi); + moshi.setFont(new Font("妤蜂綋", Font.BOLD, 20)); + moshi.add(menuitem1); + moshi.add(menuitem2); + moshi.setFont(new Font("妤蜂綋", Font.BOLD, 20)); + moshi.add(menuitem1); + moshi.add(menuitem2); + this.setLayout(new BorderLayout()); + panel1.add(text); + text.setBorder(null); + this.add(panel1,BorderLayout.NORTH); + text.setHorizontalAlignment(JTextField.RIGHT); + text.setPreferredSize(new Dimension (450,100)); + text.setFont(new Font("榛戜綋", Font.BOLD, 40)); + panel1.add(text); + panel2.setLayout(new FlowLayout()); + panel2.setLayout(new GridLayout(9, 4, 2, 2)); + JButton button1[]=new JButton[36]; + String str0[]= {"sin","cos","tan","x^2","x^3","x^y","蟺","e","e^x","鈭歺","rt","x!","ln","lg","%","10^x","|x|","CE","C","鈫","7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"}; + for(i=0;i<36;i++) { + button1[i]=new JButton(str0[i]); + button1[i].setFont(new Font("妤蜂綋", Font.BOLD, 20)); + button1[i].setBorderPainted(false); //瀛椾綋 + button1[i].addActionListener(this);//鍙栨秷杈圭晫 + panel2.add(button1[i]); + } + for(i=0;i<20;i++) { + button1[i].setBackground(color2); + } + button1[23].setBackground(color2); + button1[27].setBackground(color2); + button1[31].setBackground(color2); + button1[35].setBackground(color2); + button1[20].setBackground(color1); + button1[21].setBackground(color1); + button1[22].setBackground(color1); + button1[24].setBackground(color1); + button1[25].setBackground(color1); + button1[26].setBackground(color1); + button1[28].setBackground(color1); + button1[29].setBackground(color1); + button1[30].setBackground(color1); + button1[32].setBackground(color1); + button1[33].setBackground(color1); + add(panel2,BorderLayout.CENTER); + button1[34].setBackground(color0); + menuitem1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + StandardCalculator win1= new StandardCalculator("灏忕殦鏍囧噯璁$畻鍣",500,300,500,600); + win1.setVisible(true); + } + }); + } + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + if(str.equals("鈫")) { + String str0=text.getText(); + int i=str0.length(); + if(i>0) { + if(i>1) { + str0= str0.substring(0, i-1); + text.setText(str0); + } + else + text.setText("0"); + first = true; + + } + } + else if(str.equals("C")){ + text.setText("0"); + first = true; + operator = "="; + } + else if("0123456789.".indexOf(str) >= 0){ + if (first) { + text.setText(str); + } else if((str.equals(".")) && (text.getText().indexOf(".")<0)){ + text.setText(text.getText() + "."); + } + else if (!str.equals(".")){ + text.setText(text.getText()+str); + } + first=false; + } + else if(str.equals("蟺")) + text.setText(String.valueOf(3.14159265)); + else if(str.equals("e")) + text.setText(String.valueOf(2.17828)); + else if(str.equals("CE")) { + text.setText(null); + } + else + Operator(str); + } + public double getNumber() { + double result0=0; + result0= Double.valueOf(text.getText()).doubleValue(); + return result0; + } + private void Operator(String str) { + if (operator.equals("/")) { + if (getNumber()==0) { + operate =false; + text.setText("閿欒:闄ゆ暟涓嶈兘涓洪浂"); + } + else { + result/= getNumber(); + } + } + else if (operator.equals("+")) { + result+= getNumber(); + } + else if (operator.equals("-")) { + result -= getNumber(); + } + else if (operator.equals("*")) { + result *=getNumber(); + } + else if (operator.equals("sin")) { + result=Math.sin(result); + } + else if (operator.equals("cos")) { + result=Math.cos(result); + } + else if (operator.equals("tan")) { + result=Math.tan(result); + } + else if (operator.equals("|x|")) { + result=Math.abs(result); + } + else if (operator.equals("=")) { + result=getNumber(); + } + else if (operator.equals("x^2")) { + double o=result; + result=o*o; + } + else if (operator.equals("x^3")) { + double o=result; + result=o*o*o; + } + else if (operator.equals("x^y")) { + result=Math.pow(result,getNumber()); + } + else if (operator.equals("鈭歺")) { + if(result<0) { + operate=false; + }else { + result= Math.sqrt(result); + } + } + else if (operator.equals("rt")) { + result=result*57.3; + } + else if (operator.equals("x!")) { + double b=result; + int c=1; + for(i=1;i<=b;i++) + { + c=c*i; + result=c;} + } + else if(operator.equals("10^x")) { + int a=10; + double b=result; + double c=Math.pow(a, b); + result=c; + } + else if (operator.equals("ln")) { + result=Math.log(result); + } + else if (operator.equals("lg")) { + result=Math.log10(result); + } + else if (operator.equals("%")) { + result%= getNumber(); + } + else if (operator.equals("e^x")) { + result= Math.exp(result); + } + else if (operator.equals("=")) { + result=getNumber(); + } + isOperate(operate); + operator =str; + first= true; + operate= true; + } + private void isOperate(boolean operate) { + if(operate=true) { + long a; + double b; + a=(long)result; + b=a-result; + BigDecimal num=new BigDecimal(String.valueOf(result)); + if (b==0) { + text.setText(String.valueOf(a)); + } else if (num.scale()==1) { + text.setText(String.valueOf(new DecimalFormat("0.0").format(result))); + } else if (num.scale() == 2) { + text.setText(String.valueOf(new DecimalFormat("0.00").format(result))); + } else if (num.scale() == 3) { + text.setText(String.valueOf(new DecimalFormat("0.000").format(result))); + } else if (num.scale() == 4) { + text.setText(String.valueOf(new DecimalFormat("0.0000").format(result))); + } else if (num.scale() == 5) { + text.setText(String.valueOf(new DecimalFormat("0.00000").format(result))); + } else if (num.scale() == 6) { + text.setText(String.valueOf(new DecimalFormat("0.000000").format(result))); + } + else + text.setText(String.valueOf(new DecimalFormat("0.0000000").format(result))); + } + else if(operate=false) { + text.setText("鍑虹幇閿欒"); + } + } + public static void main(String args[]) { + ScientificCalculator win2=new ScientificCalculator("灏忕殦绉戝璁$畻鍣",500,300,500,600); +} +} \ No newline at end of file diff --git a/src/java2022spring/StandardCalculator.java b/src/java2022spring/StandardCalculator.java new file mode 100644 index 0000000000000000000000000000000000000000..90b28c7955baf169835c3080792ad3ead9746e87 --- /dev/null +++ b/src/java2022spring/StandardCalculator.java @@ -0,0 +1,170 @@ +package java2022spring; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class StandardCalculator extends JFrame implements ActionListener{ + JMenuBar menubar=new JMenuBar(); + JMenu moshi=new JMenu("妯″紡"); + JMenuItem menuitem1=new JMenuItem("鏍囧噯璁$畻鍣"); + JMenuItem menuitem2=new JMenuItem("绉戝璁$畻鍣"); + JTextField text=new JTextField(); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + Color color0= new Color(125, 200, 240); + int i=0; + boolean first; + double result; + String operator="="; + boolean operate; + public StandardCalculator(){} + public StandardCalculator(String s,int x,int y,int z,int n) { + init(s); + setLocation(x,y); + setSize(z,n); + setVisible(true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + } + void init( String s) { + Color color0=new Color(125,200,240);// + Color color1=new Color(255,255,255); + Color color2=new Color(192,192,192); + setTitle(s); + this.setJMenuBar(menubar); + menubar.add(moshi); + moshi.setFont(new Font("妤蜂綋", Font.BOLD, 20)); + moshi.add(menuitem1); + moshi.add(menuitem2); + this.setLayout(new BorderLayout()); + text.setHorizontalAlignment(JTextField.RIGHT); + text.setPreferredSize(new Dimension (470,100)); + text.setFont(new Font("榛戜綋", Font.BOLD, 40)); + panel1.add(text); + text.setBorder(null); + text.setBackground(color1);//鍒犻櫎鏂囨湰妗嗙殑杈规 + this.add(panel1,BorderLayout.NORTH); + panel2.setLayout(new FlowLayout()); + panel2.setLayout(new GridLayout(5,4,2,2));// 鐢ㄧ綉鏍煎竷灞鍣紝4琛岋紝4鍒楃殑缃戞牸锛岀綉鏍间箣闂寸殑姘村钩鏂瑰悜鍨傜洿鏂瑰悜闂撮殧鍧囦负2涓儚绱 + String str[]= {"|x|","CE","C","鈫","7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"}; + JButton button[]=new JButton[20]; + for(i=0;i<=19;i++) { + button[i]=new JButton(str[i]); + button[i].setFont(new Font("妤蜂綋", Font.BOLD, 20)); + button[i].setBorderPainted(false); //瀛椾綋 + button[i].addActionListener(this);//鍙栨秷杈圭晫 + panel2.add(button[i]); + } + add(panel2,BorderLayout.CENTER); + button[4].setBackground(color1); + button[5].setBackground(color1); + button[6].setBackground(color1); + button[8].setBackground(color1); + button[9].setBackground(color1); + button[10].setBackground(color1); + button[12].setBackground(color1); + button[13].setBackground(color1); + button[16].setBackground(color1); + button[17].setBackground(color1); + button[14].setBackground(color1); + button[0].setBackground(color2); + button[1].setBackground(color2); + button[2].setBackground(color2); + button[3].setBackground(color2); + button[7].setBackground(color2); + button[11].setBackground(color2); + button[15].setBackground(color2); + button[18].setBackground(color0); + button[19].setBackground(color2); + menuitem2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + ScientificCalculator science=new ScientificCalculator("灏忕殦绉戝璁$畻鍣",500,300,500,600); + science.setVisible(true); + } + }); + } + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + if(str.equals("鈫")) { + String str0=text.getText(); + int i=str0.length(); + if(i>0) { + if(i>1) { + str0= str0.substring(0, i-1); + text.setText(str0); + } + else + text.setText("0"); + first = true; + + } + } + else if(str.equals("C")){ + text.setText("0"); + first = true; + operator = "="; + result=0; + } + else if(str.equals("CE")){ + text.setText(null); + } + else if("0123456789.".indexOf(str) >= 0){ + if (first) { + text.setText(str); + } else if((str.equals(".")) && (text.getText().indexOf(".")<0)){ + text.setText(text.getText() + "."); + } + else if (!str.equals(".")){ + text.setText(text.getText()+str); + } + first=false; + } + else if(str.equals("x^2")||str.equals("|x|")){ + operate1(str); + } + else + operate2(str); + } + private void operate2(String str) { + if (operator.equals("/")) { + if (getNumber()==0.0) { + operate=false; + text.setText("閿欒:闄ゆ暟涓嶈兘涓洪浂"); + } + else + result/=getNumber(); + } + else if (operator.equals("+")) { + result+=getNumber(); + } + else if (operator.equals("-")) { + result -=getNumber(); + } + else if (operator.equals("*")) { + result *=getNumber(); + } + + else if (operator.equals("=")) { + result=getNumber(); + }if (operate) { + text.setText(String.valueOf(result)); + } + operator=str; + first=true; + operate=true; + } + private void operate1(String str) { + if (operator.equals("|x|")) { + result=Math.abs(result); + text.setText(String.valueOf(result)); + } + operator=str; + first=true; + operate=true; + } + public double getNumber() { + double result0=0; + result0= Double.valueOf(text.getText()).doubleValue(); + return result0; +} +} + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..0000000000000000000000000000000000000000 --- a/src/java2022spring/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -}