diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..b9d2fc2dfcbcf526cfa7d5cac41e8d1f70a0c53e --- /dev/null +++ b/Calculator.java @@ -0,0 +1,136 @@ +package java2020spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.lang.reflect.Array; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class Calculator extends JFrame implements ActionListener{ + private static final long serialVersionUID = 1L; + private static final String COMMANDS = null; + private static final Icon[] COMMAND = null; + private static final String M = null; + + public static void main(String[] args) { + Calculator calculator=new Calculator(); + calculator.setVisible(true); + } + + public Calculator() throws HeadlessException { + super("Caculator"); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + this.setTitle(Const.TITLE); + this.setSize(Const.FRAME_W,Const.FRAME_H); + this.setLayout(new BorderLayout()); + this.setLocation(Const.FRAME_X,Const.FRAME_Y); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + resultText.setBounds(20,18,255,115); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + resultText.setEditable(false); + History.setBounds(290,40,250,370); + History.setAlignmentX(LEFT_ALIGNMENT); + History.setEditable(false); + label.setBounds(400,15,100,20); + j1.setBounds(20,18,320,115); + j1.setLayout(new GridLayout()); + j2.setBounds(380,40,250,380); + j2.setLayout(new GridLayout()); + resultText.setLineWrap(true); + resultText.setWrapStyleWord(true); + History.setLineWrap(true); + History.setWrapStyleWord(true); + g1.setViewportView(resultText); + g1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + j1.add(g1); + j2.add(g2); + this.add(j1); + this.add(j2); + this.setLayout(null); + this.add(label); + int x=20,y=150; + for (int i=0;i=0) + { + c =""; + for(;i=0;i++) + { + c=c+str.charAt(i); + } + i--; + postQueue[f]=c; + f++; + } else if("(".indexOf(str.charAt(i))>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if(")".indexOf(str.charAt(i))>=0) + { + for(;;) + { + if(opStack[d]!='(') + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + else + { + d--; + break; + } + } + } + if("*%/".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } else { + if ("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) { + postQueue[f]=opStack[d] + ""; + f++; + opStack[d]=str.charAt(i); + } + } + } + else if("+-".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } + else { + if("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]= str.charAt(i); + } + } + } + } + for(;d!=-1;) + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + return postQueue; + } + + public String Result(String str[]) { + String Result[]=new String[100]; + int d=-1; + for (int i=0;str[i]!=null;i++) + { + if ("+-*%/".indexOf(str[i])<0) + { + d++; + Result[d]=str[i]; + } + if ("+-*%/".indexOf(str[i])>=0) + { + double x,y,n; + x=Double.parseDouble(Result[d]); + d--; + y=Double.parseDouble(Result[d]); + d--; + if ("-".indexOf(str[i])>=0) { + n=y-x; + d++; + Result[d]=String.valueOf(n); + } + if ("+".indexOf(str[i])>=0) { + n=y+x; + d++; + Result[d]=String.valueOf(n); + } + if ("*".indexOf(str[i])>=0) { + n=y*x; + d++; + Result[d]=String.valueOf(n); + + } + if ("/".indexOf(str[i])>=0) + { + if (x==0) + { + String s="error!"; + return s; + } + else { + n=y/x; + d++; + Result[d]=String.valueOf(n); + } + } + if("%".indexOf(str[i])>=0) + { + if (x==0) + { + String s="error!"; + return s; + } + else { + n=y%x; + d++; + Result[d]=String.valueOf(n); + } + } + + } + } + return Result[d]; + } + + public String kfys(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.sqrt(a); + result=String.valueOf(b); + return result; + } + + public String pfys(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,2); + result=String.valueOf(b); + return result; + } + + public String op(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=-a; + result=String.valueOf(b); + return result; + } + + public String ba(String str) + { + String l=resultText.getText(); + l=l.substring(0,l.length()-1); + return l; + } + + public String pyf(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,3); + result=String.valueOf(b); + return result; + } + + public String j(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,5); + result=String.valueOf(b); + return result; + } + + private String p(String a2) { + // TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍 + return null; + } + + private String k(String a2) { + // TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍 + return null; + } + + public static String getCommands() { + return COMMANDS; + } + + public static Icon[] getCommand() { + return COMMAND; + } + + public static String getM() { + return M; + } + + public JButton[] getCommands2() { + return commands2; + } + + public void setCommands2(JButton[] commands2) { + this.commands2 = commands2; + } + + public Array[] getMs() { + return ms; + } + + public void setMs(Array[] ms) { + this.ms = ms; + } + + public JButton[] getMs2() { + return ms2; + } + + public void setMs2(JButton[] ms2) { + this.ms2 = ms2; + } +} + diff --git "a/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/.keep" "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/calculator/Calculator.java" "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/calculator/Calculator.java" new file mode 100644 index 0000000000000000000000000000000000000000..e1a68f18ce1dc843087c17f54d882cd0c18add21 --- /dev/null +++ "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/calculator/Calculator.java" @@ -0,0 +1,469 @@ +package calculator; + +import javax.swing.*; + +import java2020spring.Const; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.lang.reflect.Array; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridLayout; + +public class Calculator extends JFrame implements ActionListener{ + + private static final long serialVersionUID = 1L; + private static final String COMMANDS = null; + private static final Icon[] COMMAND = null; + private static final String M = null; + public static void main(String[] args) { + Calculator calculator=new Calculator(); + calculator.setVisible(true); + } + + public Calculator() throws HeadlessException { + super("Caculator"); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + this.setTitle(Const.TITLE); + this.setSize(Const.FRAME_W,Const.FRAME_H); + this.setLayout(new BorderLayout()); + this.setLocation(Const.FRAME_X,Const.FRAME_Y); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + resultText.setBounds(20,18,255,115); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + resultText.setEditable(false); + History.setBounds(290,40,250,370); + History.setAlignmentX(LEFT_ALIGNMENT); + History.setEditable(false); + label.setBounds(400,15,100,20); + j1.setBounds(20,18,320,115); + j1.setLayout(new GridLayout()); + j2.setBounds(380,40,250,380); + j2.setLayout(new GridLayout()); + resultText.setLineWrap(true); + resultText.setWrapStyleWord(true); + History.setLineWrap(true); + History.setWrapStyleWord(true); + g1.setViewportView(resultText); + g1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + j1.add(g1); + j2.add(g2); + this.add(j1); + this.add(j2); + this.setLayout(null); + this.add(label); + int x=20,y=150; + for (int i=0;i=0) + { + c =""; + for(;i=0;i++) + { + c=c+str.charAt(i); + } + i--; + postQueue[f]=c; + f++; + } else if("(".indexOf(str.charAt(i))>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if(")".indexOf(str.charAt(i))>=0) + { + for(;;) + { + if(opStack[d]!='(') + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + else + { + d--; + break; + } + } + } + if("*%/".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } else { + if ("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) { + postQueue[f]=opStack[d] + ""; + f++; + opStack[d]=str.charAt(i); + } + } + } + else if("+-".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } + else { + if("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]= str.charAt(i); + } + } + } + } + for(;d!=-1;) + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + return postQueue; + } + + public String Result(String str[]) { + String Result[]=new String[100]; + int d=-1; + for (int i=0;str[i]!=null;i++) + { + if ("+-*%/".indexOf(str[i])<0) + { + d++; + Result[d]=str[i]; + } + if ("+-*%/".indexOf(str[i])>=0) + { + double x,y,n; + x=Double.parseDouble(Result[d]); + d--; + y=Double.parseDouble(Result[d]); + d--; + if ("-".indexOf(str[i])>=0) { + n=y-x; + d++; + Result[d]=String.valueOf(n); + } + if ("+".indexOf(str[i])>=0) { + n=y+x; + d++; + Result[d]=String.valueOf(n); + } + if ("*".indexOf(str[i])>=0) { + n=y*x; + d++; + Result[d]=String.valueOf(n); + + } + if ("/".indexOf(str[i])>=0) + { + if (x==0) + { + String s="error!"; + return s; + } + else { + n=y/x; + d++; + Result[d]=String.valueOf(n); + } + } + if("%".indexOf(str[i])>=0) + { + if (x==0) + { + String s="error!"; + return s; + } + else { + n=y%x; + d++; + Result[d]=String.valueOf(n); + } + } + + } + } + return Result[d]; + } + + public String kfys(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.sqrt(a); + result=String.valueOf(b); + return result; + } + + public String pfys(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,2); + result=String.valueOf(b); + return result; + } + + public String op(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=-a; + result=String.valueOf(b); + return result; + } + + public String ba(String str) + { + String l=resultText.getText(); + l=l.substring(0,l.length()-1); + return l; + } + + public String pyf(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,3); + result=String.valueOf(b); + return result; + } + + public String j(String str) { + String result=""; + double a=Double.parseDouble(str),b=0; + b=Math.pow(a,5); + result=String.valueOf(b); + return result; + } + + public static String getCommands() { + return COMMANDS; + } + + public static Icon[] getCommand() { + return COMMAND; + } + + public static String getM() { + return M; + } + + public JButton[] getCommands2() { + return commands2; + } + + public void setCommands2(JButton[] commands2) { + this.commands2 = commands2; + } + + public Array[] getMs() { + return ms; + } + + public void setMs(Array[] ms) { + this.ms = ms; + } + + public JButton[] getMs2() { + return ms2; + } + + public void setMs2(JButton[] ms2) { + this.ms2 = ms2; + } +} + + + + diff --git "a/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/Const.java" "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/Const.java" new file mode 100644 index 0000000000000000000000000000000000000000..32cb2830d6ffee4d39fad8d14fd7a6fdbfdc0cf4 --- /dev/null +++ "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/Const.java" @@ -0,0 +1,14 @@ +package java2020spring; + +import java.awt.*; + +public class Const { + public static final int FRAME_W=300; + public static final int FRAME_H=300; + public static final int SCREEN_W=Toolkit.getDefaultToolkit().getScreenSize().width; + public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().height; + public static final int FRAME_X=(SCREEN_W-FRAME_W)/2; + public static final int FRAME_Y=(SCREEN_H-FRAME_H)/2; + public static final String TITLE="计算器"; + +} diff --git "a/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/resultText.java" "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/resultText.java" new file mode 100644 index 0000000000000000000000000000000000000000..9a0cb4fa92f95953971677602c6761c238598de4 --- /dev/null +++ "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\205\255\346\254\241\346\217\220\344\272\244/java2020spring/resultText.java" @@ -0,0 +1,17 @@ +package java2020spring; + +import java.awt.Color; + +public class resultText { + + public static void setHorizontalAlignment(int right) { + // TODO 自动生成的方法存根 + + } + + public static void setBackground(Color white) { + // TODO 自动生成的方法存根 + + } + +} diff --git "a/\350\256\241\347\256\227\345\231\250\347\254\254\345\233\233\346\254\241\346\217\220\344\272\244" "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\233\233\346\254\241\346\217\220\344\272\244" new file mode 100644 index 0000000000000000000000000000000000000000..f41a4acd3e2f9e77c6379874aa32a70582a5ce26 --- /dev/null +++ "b/\350\256\241\347\256\227\345\231\250\347\254\254\345\233\233\346\254\241\346\217\220\344\272\244" @@ -0,0 +1,400 @@ +package calculator; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.lang.reflect.Array; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java2020spring.Const; + +public class Calculator extends JFrame implements ActionListener{ + private static final long serialVersionUID = 1L; + private static final String COMMANDS = null; + private static final Icon[] COMMAND = null; + private static final String M = null; + + public static void main(String[] args) { + Calculator calculator=new Calculator(); + calculator.setVisible(true); + } + + public Calculator() throws HeadlessException { + super("Caculator"); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + this.setTitle(Const.TITLE); + this.setSize(Const.FRAME_W,Const.FRAME_H); + this.setLayout(new BorderLayout()); + this.setLocation(Const.FRAME_X,Const.FRAME_Y); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + resultText.setBounds(20,18,255,115); + resultText.setAlignmentX(RIGHT_ALIGNMENT); + resultText.setEditable(false); + History.setBounds(290,40,250,370); + History.setAlignmentX(LEFT_ALIGNMENT); + History.setEditable(false); + label.setBounds(400,15,100,20); + j1.setBounds(20,18,320,115); + j1.setLayout(new GridLayout()); + j2.setBounds(380,40,250,380); + j2.setLayout(new GridLayout()); + resultText.setLineWrap(true); + resultText.setWrapStyleWord(true); + History.setLineWrap(true); + History.setWrapStyleWord(true); + g1.setViewportView(resultText); + g1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + g2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + j1.add(g1); + j2.add(g2); + this.add(j1); + this.add(j2); + this.setLayout(null); + this.add(label); + int x=20,y=150; + for (int i=0;i=0) + { + c =""; + for(;i=0;i++) + { + c=c+str.charAt(i); + } + i--; + postQueue[f]=c; + f++; + } else if("(".indexOf(str.charAt(i))>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if(")".indexOf(str.charAt(i))>=0) + { + for(;;) + { + if(opStack[d]!='(') + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + else + { + d--; + break; + } + } + } + if("*%/".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } else { + if ("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) { + postQueue[f]=opStack[d] + ""; + f++; + opStack[d]=str.charAt(i); + } + } + } + else if("+-".indexOf(str.charAt(i))>=0) + { + if(d==-1) + { + d++; + opStack[d]=str.charAt(i); + } + else { + if("*%/".indexOf(opStack[d])>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]=str.charAt(i); + } + else if("(".indexOf(opStack[d])>=0) + { + d++; + opStack[d]=str.charAt(i); + } + else if("+-".indexOf(str.charAt(i))>=0) + { + postQueue[f]=opStack[d]+""; + f++; + opStack[d]= str.charAt(i); + } + } + } + } + for(;d!=-1;) + { + postQueue[f]=opStack[d]+""; + f++; + d--; + } + return postQueue; + } + + public String Result(String str[]) { + String Result[]=new String[100]; + int Top=-1; + for (int i=0;str[i]!=null;i++) + { + if ("+-*%/".indexOf(str[i])<0) + { + Top++; + Result[Top]=str[i]; + } + 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); + } + if ("+".indexOf(str[i])>=0) { + n=y+x; + Top++; + Result[Top]=String.valueOf(n); + } + if ("*".indexOf(str[i])>=0) { + 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); + } + } + 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]; + } + + + + private String p(String a2) { + // TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍 + return null; + } + + private String k(String a2) { + // TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍 + return null; + } + + public static String getCommands() { + return COMMANDS; + } + + public static Icon[] getCommand() { + return COMMAND; + } + + public static String getM() { + return M; + } + + public JButton[] getCommands2() { + return commands2; + } + + public void setCommands2(JButton[] commands2) { + this.commands2 = commands2; + } + + public Array[] getMs() { + return ms; + } + + public void setMs(Array[] ms) { + this.ms = ms; + } + + public JButton[] getMs2() { + return ms2; + } + + public void setMs2(JButton[] ms2) { + this.ms2 = ms2; + } +} + + + +