Nonsense less say directly on the code, hope to see friends and I can communicate more,, thank you
Package Myjisuanqi;
Import Java.awt.BorderLayout;
Import Java.awt.Button;
Import Java.awt.Frame;
Import Java.awt.GridLayout;
Import Java.awt.Panel;
Import Java.awt.TextField;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
public class MainFrame03 extends Frame implements mouselistener{
Private String first;
Private String second;
Private String str = "";
Private String operator;
Private TextField input = new TextField ();
Private button Kuohao1 = New button ("(");
Private button Kuohao2 = New button (")");
Private button one = New button ("1");
Private button, both = New Button ("2");
Private button three = New button ("3");
Private button Add = New button ("+");
Private button four = New button ("4");
Private button five = New button ("5");
Private button six = New button ("6");
Private button Jian = New button ("-");
Private button seven = New button ("7");
Private button eight = New button ("8");
Private button nine = New button ("9");
Private button Cheng = New button ("*");
Private button AC = New button ("Clear 0");
Private button Zero = New button ("0");
Private button equal = New button ("=");
Private button Chu = New button ("/");
Private button Dian = New button (".");
public static void Main (String args[]) {
Init ();
}
private void init () {
Frame f = new Frame ("Beam Yangyang made calculator 3.0--Basic can achieve all functions of simple calculator, including recognition of two-level operation and brackets");
F.add ("North", input);
panel keys = new Panel ();
F.add ("Center", keys);
Keys.setlayout (New GridLayout (bis));
Keys.add (one);
Keys.add (both);
Keys.add (three);
Keys.add (add);
Keys.add (four);
Keys.add (five);
Keys.add (six);
Keys.add (Jian);
Keys.add (seven);
Keys.add (eight);
Keys.add (nine);
Keys.add (Cheng);
Keys.add (DIAN);
Keys.add (zero);
Keys.add (equal);
Keys.add (CHU);
Panel Kuohao = new Panel ();
Kuohao.add (KUOHAO1);
Kuohao.add (AC);
Kuohao.add (KUOHAO2);
F.add ("South", Kuohao);
Zero.addmouselistener (this);
One.addmouselistener (this);
Two.addmouselistener (this);
Three.addmouselistener (this);
Four.addmouselistener (this);
Five.addmouselistener (this);
Six.addmouselistener (this);
Seven.addmouselistener (this);
Eight.addmouselistener (this);
Nine.addmouselistener (this);
Add.addmouselistener (this);
Jian.addmouselistener (this);
Cheng.addmouselistener (this);
Chu.addmouselistener (this);
Equal.addmouselistener (this);
Dian.addmouselistener (this);
Ac.addmouselistener (this);
Kuohao1.addmouselistener (this);
Kuohao2.addmouselistener (this);
F.setlocation (600,0);
F.setsize (300, 250);
F.setvisible (TRUE);
}
@Override
public void mouseclicked (MouseEvent e) {
Button btn = (button) E.getsource ();
if (btn! = equal) {
str = Str+btn.getlabel ();
Input.settext (str);
}
if (btn = = equal) {
try {
Input.settext (opt (str) + "");
} catch (Exception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
}
}
public static float opt (String s) throws exception{
if (s = = NULL | | "". Equals (S.trim ())) {
return 0f;
}
int A1=s.indexof ("+");
int A2=s.indexof ("-");
int A3=s.indexof ("*");
int A4=s.indexof ("/");
int A5=s.indexof ("(");
if (a1==-1&&a2==-1&&a3==-1&&a4==-1) {
if (S.trim () ==null| | "". Equals (S.trim ())) {
throw new Exception ("Operate error");
}
Return Float.parsefloat (S.trim ());
}
if (a5!=-1) {
int A6=s.indexof (")");
if (a6==-1) {
throw new Exception ("Parentheses do not match");
}else{
Float f=opt (s.substring (A5+1,A6). Trim ());
S=s.replace (S.substring (a5,a6+1), string.valueof (f));
return opt (s);
}
}
if (a1!=-1) {
return opt (s.substring (0,A1)) +opt (S.substring (A1+1,s.length ()));
}
if (a2!=-1) {
return opt (s.substring (0,A2))-opt (S.substring (A2+1,s.length ()));
}
if (a3!=-1) {
return opt (s.substring (0,A3)) *opt (S.substring (A3+1,s.length ()));
}
if (a4!=-1) {
return opt (s.substring (0,A4))/opt (S.substring (A4+1,s.length ()));
}
Return Integer.parseint (S.trim ());
}
@Override
public void mousepressed (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mousereleased (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mouseentered (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mouseexited (MouseEvent e) {
TODO auto-generated Method Stub
}
}
2017-05-18
Java Novice Practice: Use frame to write a calculator that recognizes two-level operations and bracket precedence operations