Because it is a novice (very pure kind), so a bit messy, the code is very redundant (will be condensed later), naming rules please ignore. Of course, this calculator is only able to run up to the extent that there will be some inexplicable error, such as my index operation index only support integers and so on. After all, it is the first time to write a small thing alone, still very happy.
1. Main section (Layout and click event ActionEvent monitoring)
Package calculator; import Java.awt.*;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;public class Jisuanqi {//Three basic layout components Frame F = new Frame (" Calculator "); TextField t = new TextField ("0", 40); panel p = new Panel (new GridLayout (5,4,10,10));//panel is grid layout//Load business logic Yewuluoji YWLJ = new Yewuluoji ();//button Stringstring strnum = "0123456789"; String strtoken = "+-*/"; String strequal = "="; String strpoint = "."; String strclear = "C"; String[] Buttonstr = {"C", "+/-", "x^2", "X^y", "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "=", "/"};// Listener Event ActionListener Al = null;//creates and adds a button and joins the Listener event public void Init () {for (String i:buttonstr) {button b = new button (i); B.addactionlistener (Getal ());p. Add (b); F.addwindowlistener (New Windowl ()); F.add (T,borderlayout.north); F.add (P); The default is Borderlayout.center layout f.pack ();//Adaptive appropriate size f.setvisible (true);//f set to visible}//get listener events, note that anonymous inner classes do not use local variables because one is in the memory heap, One in the stack, the life cycle is different public ACtionlistener Getal () {if (al==null) {al = new ActionListener () {public void actionperformed (ActionEvent e) {String ButtonS TR1 = E.getactioncommand (); Returns the name of the component associated with the event string textstr = T.gettext (); result = Ywlj.yewumethod (BUTTONSTR1,TEXTSTR);//Run business logic t.settext (TEXTSTR );}};/ /This is just a statement, the semicolon ends}return al;} Window Close event class Windowl extends windowadapter{@Overridepublic void windowclosing (WindowEvent e) {system.exit (0);}} public static void Main (string[] args) {//TODO auto-generated method stub new Jisuanqi (). init ();}}
2. Operational rules section (using BigDecimal, not very familiar)
Package calculator; Import Java.math.bigdecimal;public class Yunsuan {//--------------------------addition public static String plus ( String a,string b) {BigDecimal Biga = new BigDecimal (a); It is best to use string to construct bigdecimalbigdecimal BIGB = new BigDecimal (b); return Biga.add (BIGB). toString ();} ---------------------------subtraction public static string minus (string a,string b) {BigDecimal Biga = new BigDecimal (A + ""); It is best to use string to construct bigdecimalbigdecimal BIGB = new BigDecimal (b + ""); return Biga.subtract (BIGB). toString ();} --------------------------Multiply public static string multi (string a,string b) {BigDecimal Biga = new BigDecimal (A + ""); It is best to use string to construct bigdecimalbigdecimal BIGB = new BigDecimal (b + ""); return biga.multiply (BIGB). toString ();} ------------------------division public static string divide (string a,string b) {BigDecimal Biga = new BigDecimal (A + ""); It is best to use string to construct bigdecimalbigdecimal BIGB = new BigDecimal (b + ""); return Biga.divide (BIGB). toString ();} -------------------------square operation public static String SquaRe (String a) {BigDecimal Biga = new BigDecimal (a); return biga.multiply (Biga). toString ();} The power exponent operation of the------------------------x^y public static string Zhishu (string a, int b) {BigDecimal Biga = new BigDecimal (a); BigDecimal Bigz = biga;for (int i=1;i<b;i++) {Bigz = biga.multiply (Bigz);} return bigz.tostring ();}}
3. Main business logic (mainly the toggle click Logic for various keystrokes)
Package Calculator, public class Yewuluoji {//button tag private string strnum = "0123456789";p rivate string strzf = "+/-";p rivate St Ring[] Strtoken = {"+", "-", "*", "/", "X^y"};p rivate string strsquare = "x^2";p rivate string strequal = "=";p rivate string St Rpoint = "."; Private String strclear = "C"; Clear key, reset//Status code string fint = Null;boolean F = false;//First number, if the first number is also 0 (the number of the first click of the joint characterization record), if you get a double type, How to copy string sInt = Null;boolean s = false;//second number, s depicts the second click of the number string token = ""; Boolean tokenclickif = false;//whether to click the operation symbol Boolean EQUALCLICKIF = false;//whether to click the equals sign Boolean zfclickif = false;//whether to click on the sign +/-//return Should be set TextField string-"Textstrpublic string Yewumethod (String buttonstr,string textstr) {//Get the button character, If the number button is not clicked, the event listener hears (also divide int and double)//If the first number is not initialized, the first number is initialized (int and double) if (F==false && isnum ( BUTTONSTR)) {//Initialize first digit, and f->truefint = Buttonstr;f=true; Set TEXTSTR to the corresponding STRINGTEXTSTR = string.valueof (fint) of the number;} Already clicked on the first digit, but did not click on the operation symbol but continued to click on the second number, such as click 12 such a number else if (F==true && isnum (BUTTONSTR) && tokenclickif==false) {textstr = textstr + Buttonstr;fint = textstr;} After setting the first number, click the operation symbol else if (f==true && istoken (buttonstr) && Tokenclickif = = False) {Tokenclickif = true; token = BUTTONSTR; Log the operation symbol of the Click Zfclickif = false; Reset Sign Status}//Click on the second number of else if (f==true && s==false && tokenclickif==true && isnum (buttonstr)) { Textstr = Buttonstr;sint = textstr;s= true;} Second digit Continuous Click input else if (f==true && s==true && tokenclickif==true && isnum (buttonstr)) {textstr = Textstr + buttonstr;sint = textstr;} Already clicked the operation symbol, and set the first and second numbers, continue to click the operation symbol, the key is if the result is a double how to give the first value else if (f==true && s==true && tokenclickif ==true && Istoken (buttonstr)) {//update the calculation result in the text box TEXTSTR = Jisuan (BUTTONSTR);//assigns the calculated result to the first number fint = Textstr;} If the click Is Point pointelse if (Ispoint (BUTTONSTR)) {if (Haspoint (TEXTSTR) ==false) {textstr = Textstr+buttonstr;if (f==true) {if (s==false) Fint = Textstr;elsesint = Textstr;}} If you click on the sign ElSe if (f==true && iszf (buttonstr)) {if (S==false && zfclickif==false) {//The second number is not assigned, or the end of the operation has been initialized TEXTSTR = "-" +textstr;fint = textstr;zfclickif=true;} else if (s==true && zfclickif==false) {textstr = "-" +textstr;sint = textstr;zfclickif=true;} Consecutive click-to-sign or else if (s==false && zfclickif = = True) {Textstr = Textstr.split ("-") [1];fint = Textstr;zfclickif = False ;} else if (s==true && zfclickif==true) {textstr = Textstr.split ("-") [1];sint = Textstr;zfclickif = false;}} If you clicked the equals sign "=" Else if (f==true && s==true && tokenclickif==true && isequal (buttonstr)) {Textstr = Jisuan (token); fint = textstr;//Update Status S=false;zfclickif = False;//pointclickif = false; Floating-point operation, or do not clear the state of the position, otherwise the result can continue to add pointtokenclickif=false;token= ""; sInt = null;} Click on the square symbol else if (f==true && issquare (buttonstr)) {textstr = Yunsuan.square (fint); fint = textstr;// Reset sign Zfclickif=false;} Click the Reset key, Clearelse if (Isclear (BUTTONSTR)) {//Reset all status Fint = null; f = false;//first number, ifThe first number is also 0 (combined to describe the number of the first click of the record), if you get a double type, how to copy sint = null; s = false;//The second number, s depicts the second click of the Digital token = ""; Tokenclickif = false;//Whether to click the operation symbol EQUALCLICKIF = false;//whether to click the equals sign zfclickif = false; Textstr = "0";} return TEXTSTR;} Determines whether the clicked is a number, Strnum.split ("") returns a string array that is cut by the delimiter, public boolean isnum (string buttonstr) {for (String I: Strnum.split ("")) {if (Buttonstr.equals (i)) return true; return false;} Determines whether the key is an operation symbol Tokenpublic Boolean Istoken (String buttonstr) {for (string i:strtoken) {if (Buttonstr.equals (i)) return true;} return false;} Whether the plus sign public boolean iszf (String buttonstr) {if (Buttonstr.equals (STRZF)) return True;return false;} is the square public boolean issquare (String buttonstr) {if (Buttonstr.equals (Strsquare)) return True;return false;} Determines whether the key is equal to the public boolean isequal (String buttonstr) {if (Buttonstr.equals (strequal)) return True;return false;} Determines whether the point Pointpublic boolean ispoint (String buttonstr) {if (Buttonstr.equals (strpoint)) return True;return false;} Determines whether the reset key is public boolean isclear (String ButtoNSTR) {if (Buttonstr.equals (strclear)) return True;return false;} Used to determine if the text has pointpublic boolean haspoint (String textstr) {for (String I:textstr.split ("")) {if (I.equals (".")) return true;} return false;} Calculates the public string Jisuan (string buttonstr) {if (Buttonstr.equals ("+")) return Yunsuan.plus (fint,sint); else if ( Buttonstr.equals ("-")) return Yunsuan.minus (Fint,sint), Else if (buttonstr.equals ("*")) return Yunsuan.multi (Fint, SINT), Else if (buttonstr.equals ("/")) return Yunsuan.divide (fint,sint); else if (Buttonstr.equals ("X^y")) return Yunsuan.zhishu (Fint,integer.parseint (sInt)); return "";}}
Java Novice's own implementation of the calculator, a bit messy