The program can realize the basic arithmetic operation, and can control the precision of the operation result effectively, and realize the memory related function.
Features are as follows:
MC: Clear Storage data
MR: Read the stored data on the screen
MS: Store the displayed data in memory
m+: Use the stored data plus the number displayed on the screen and update the value in the memory
M-: Subtract the number displayed on the screen and update the values in the storage with the stored data
←: Delete The mantissa of the number of inputs
C: Clear Calculator data, next input data start new Operation
CE: Clears the number of the current input
±: Positive negative Conversion
√: open square
+,-, *,/subtraction, the result
%: modulo
1/x: Take the countdown
Program Flowchart:
Code implementation:
Class Operatenum {//operand class public
double value;//operand's actual value public
int dotwei;//operand If decimal, the number of digits after the decimal point is public
String invertedstring;//1/x strings public
, String sqrtedstring;//sqrt (x) string public
Boolean issqrted,isinverted;/ /Does the tag √,1/x over, used for subsequent judgment public
Boolean clear;//clear for true indicates whether the reset operand
is public boolean isfloat;//isfloat true, indicating that the operand is a decimal Public
operatenum ()
{
value=0;
dotwei=0;
Sqrtedstring=null;
Invertedstring=null;
Issqrted=false;
Isinverted=false;
Clear=false;
Isfloat=false;
}
public void Reset ()//reset operand
{
value=0;
dotwei=0;
Sqrtedstring=null;
Invertedstring=null;
Issqrted=false;
Isinverted=false;
Clear=false;
Isfloat=false
}
}
Class Storagefield {//Store public
double storagenum;//store stored data public
Boolean storagemode;//whether to start the store
Public Storagefield ()
{
storagenum=0;
Storagemode=false
}
}
Import java.awt.*;
Import Java.awt.event.KeyAdapter;
Import java.awt.event.KeyEvent;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.math.BigDecimal;
Import javax.swing.*;
Import Javax.swing.border.Border;
Import Javax.swing.border.LineBorder;
public class Calc extends jframe{private JTextField display1,display2,display3;
Private JButton num[],operate[]; Private String name[]={"MC", "MR", "MS", "m+", "M", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "
1 "," 2 "," 3 ","-"," = "," 0 ",". "," + "}; Private Operatenum op1=new Operatenum (), Op2=new operatenum ();//operand 1, operands 2 private Storagefield storage=new Storagefield ()//store private string action= "OP1";//Represents the first operand of the object "OP1" action to be manipulated, "OP2" operates the second operand, private string sign= "";//operator, default is null private
String Screen1,screen2;
Private Boolean disable=false;
Public Calc () {Super ("calculator");
This.setlayout (NULL);
This.setsize (228, 324);
This.setresizable (FALSE);
JPanel jp=new JPanel (null); Jp.setbounds (10,15,getwidth () -24,getheight ());
Jp.setbackground (New Color (217,228,241));
This.getcontentpane (). SetBackground (New Color (217,228,241));
/** Display **/display1=new JTextField ("");
Display2=new JTextField ("0");
Display3=new JTextField ("");
Display1.setenabled (FALSE);
Display2.setenabled (FALSE);
Display3.setenabled (FALSE);
Display1.setbounds (0, 0, 204, 26);
Display2.setbounds (20, 20, 184, 34);
Display3.setbounds (0, 20, 20, 34);
Display1.sethorizontalalignment (Jlabel.right);
Display2.sethorizontalalignment (Jlabel.right);
Display3.sethorizontalalignment (Jlabel.center);
Display1.setfont (New Font ("Song Body", font.plain,12));
Display2.setfont (New Font ("Song Body", font.bold,20));
Display3.setfont (New Font ("Song Body", font.plain,20));
Display1.setdisabledtextcolor (Color.Black);
Display2.setdisabledtextcolor (Color.Black);
Display3.setdisabledtextcolor (Color.Black);
Display1.setborder (New Lineborder (New Color (242,247,252)); Display2.setborder (New Lineborder) (New Color (242,247,252)));
Display3.setborder (New Lineborder (New Color (242,247,252));
Display1.setbackground (New Color (242,247,252));
Display2.setbackground (New Color (242,247,252));
Display3.setbackground (New Color (242,247,252));
/** button 35px*29px 28 ***/int i;
Operate=new jbutton[28];
/** Top 24 **/for (i=0;i<24;i++) {operate[i]=new JButton (name[i));
Operate[i].setmargin (New Java.awt.Insets (0,0,0,0));
Operate[i].setbounds (i%5* (35+7), 60+i/5* (29+5) +5, 35, 29);
Jp.add (Operate[i]);
/***= number ***/operate[i]=new JButton (name[i));
Operate[i].setmargin (New Java.awt.Insets (0,0,0,0));
Operate[i].setbounds (i%5* (35+7), 60+i/5* (29+5) +5, 35, 29*2+5);
Jp.add (Operate[i]);
i++;
/***0 number ***/operate[i]=new JButton (name[i));
Operate[i].setmargin (New Java.awt.Insets (0,0,0,0));
Operate[i].setbounds (i%5* (35+7), 60+i/5* (29+5) +5, 35*2+7, 29);
Jp.add (Operate[i]);
/** Top 24 **/for (i=i+1;i<name.length;i++) {operate[i]=new JButton (name[i)); Operate[i].setmargin (NEW java.awt.Insets (0,0,0,0));
Operate[i].setbounds ((i+1)%5* (35+7), 60+i/5* (29+5) +5, 35, 29);
Jp.add (Operate[i]);
/** * To button Register mouse listener, keyboard listener and background * * Mouseadapter ml=new mouseadapter ();
Keyadapter kl=new Keyadapter ();
for (i=0;i<name.length;i++) {operate[i].addmouselistener (ml);
Operate[i].addkeylistener (KL);
Operate[i].setbackground (New Color (233,240,247));
Operate[i].setforeground (New Color (30,57,91));
} jp.add (Display1);
Jp.add (Display2);
Jp.add (DISPLAY3);
Jp.addkeylistener (KL);
This.add (JP);
This.setvisible (TRUE); Class Mouseadapter extends mouseadapter{public void mouseclicked (MouseEvent e) {JButton operate= (JButton) E
. GetSource ();
if (Disable)/disable button, click the C recovery Calculator {if (operate.gettext () = = "C") {clear ();
Disable=false;
else return;
Switch (Operate.gettext ()) {case "MC": MC ();
Case "Mr": Mr ();
Case "MS": MS ();
Case "m+": Madd (); Case "M": MCUT ();
Case "←": cutend ();
Case "CE": Cutnum ();
Case "C": Clear ();
Case "±": Revolt ();
Case "√": sqrt (); Case ' 7 ': Case ' 8 ': Case ' 9 ': Case ' 4 ': Case ' 5 ': Case ' 6 ': Case ' 3 ': Case ' 2 ': Case ' 1 ': case
"0": Read (Integer.parseint (Operate.gettext ()); break;//converts the text on the key to an int case "/":d ivide ();
Case "%": Mo ();
Case "*": Mul ();
Case "1/x": Inverted ();
Case "-": Cut ();
Case "+": Add ();
Case "=": sum ();
Case ".":d ot ();
} public void mouseentered (MouseEvent e) {(JButton) E.getsource ()). SetBackground (New Color (255,211,113));
public void mouseexited (MouseEvent e) {(JButton) E.getsource ()). SetBackground (New Color (233,240,247));
} * * MC cancellation store, empty storage data/public void MC () {storage.storagenum=0;
Storage.storagemode=false;
Display3.settext (NULL); }/* * Mr Read memory stored data */public void Mr () {Op1.value=storage. Storagenum;
Screen2= "" +op1.value;
if (Op1.value==math.floor (op1.value)) screen2= "" + (int) op1.value;
Display2.settext (SCREEN2);
Op1.clear=true;
/* * MS Save data to memory/public void MS () {storage.storagenum=op1.value; Display3.settext ("M");//The lower left corner of the screen shows the number of M sign}/* * m+ already stored plus the current calculation and storage/public void Madd () {storage.storagenum=s
Torage.storagenum+op1.value;
}/* * m already stored number minus current calculation and storing the difference in memory/public void Mcut () {storage.storagenum=storage.storagenum-op1.value; }/* *← input number to remove mantissa */public void Cutend () {if (action== "OP1" &&op1.value!=0)//represents operation on OP1 {if (op
1.isfloat==false)//If OP1 is an integer {op1.value= (int) OP1.VALUE/10;
Screen2= "" + (int) op1.value;
else{//if OP1 is a decimal BigDecimal bd=new BigDecimal (op1.value);
Op1.value=bd.setscale (--op1.dotwei,bigdecimal.round_down). Doublevalue ();
Screen2= "" +op1.value;
if (op1.dotwei==0)//decimal point after the digits are removed, the modified integer, update isfloat tag op1.isfloat=false; } else if (action== "OP2" &&op2.value!=0)//Represents the operation of the OP2 {if (op2.isfloat==false)//If OP2 bit integer {op2.value= (int) OP2.VALUE/10;
Screen2= "" + (int) op2.value;
else{//if OP2 is a decimal BigDecimal bd=new BigDecimal (op2.value);
Op2.value=bd.setscale (--op2.dotwei,bigdecimal.round_down). Doublevalue ();
Screen2= "" +op2.value;
if (op2.dotwei==0)//decimal point after the digits are removed, the modified integer, update isfloat tag op2.isfloat=false;
} display2.settext (SCREEN2)//output modified operand}/* CE empty current operand operand/public void cutnum () {if (action== "OP1")
Op1.reset ();
else if (action== "OP2") Op2.reset ();
Display2.settext ("0");//Initialize Display 2}/* C zero Reset calculator/public void Clear () {op1.reset ();
Op2.reset ();
Initialize data member action= "OP1";
Sign= "";
Initialize the display Display1.settext ("");
Display2.settext ("0");
}/* *± sign */public void Revolt () {if (action== "Op1") {op1.value=-op1.value;
Screen2= "" +op1.value;
else if (action== "Op2") {op2.value=-op2.value; Screen2= "" +op2.Value
} display2.settext (SCREEN2); }/* *√ root */public void sqrt () {Double x;//temporary variable if (action== "Op1") {op1.sqrtedstring= "sqrt" ("+op1.value
+")";
OP1.VALUE=MATH.SQRT (Op1.value);
Op1.issqrted=true;
X=op1.value;
else {op2.sqrtedstring= "sqrt (" +op2.value+ ")";
OP2.VALUE=MATH.SQRT (Op2.value);
Op2.issqrted=true;
X=op2.value;
} screen2=x+ "";
if (X==math.floor (x))//If x is an integer {screen2= (int) x+ "";//The floating-point number x is first converted to an int and then converted to a string} display2.settext (SCREEN2); /* * Press the number key/public void read (int value) {Display2.setfont (new font ("Arial", font.bold,20));/default font size DISPLAY2.S
Ettext (null);//Clear screen if (op1.clear==true) Op1.reset ();
if (op2.clear==true) Op2.reset ();
if (action== "OP1")//represents the transmission of data to the OP1 {if (op1.isfloat==true)//if OP1 as floating-point number {int i=1;
Double Num=value;
++op1.dotwei;
while (I<=op1.dotwei) {num*=0.1;
i++; op1.value=op1.value+num;//adds a new decimal point to the operand OP1//Because the Double-precision floating-point number has a higher precision, and we just takeIt's Op1.dotwei save Op1.value=double.parsedouble (String.Format ("%.")
+op1.dotwei+ "F", Op1.value)); Because a double-precision floating-point number has a higher precision, we simply take its op1.dotwei display on the screen Display2.settext (String.Format ("%.").
+op1.dotwei+ "F", Op1.value)); ELSE//OP1 is an integer {op1.value=op1.value*10+value;//adds a new integer bit to OP1 display2.settext ((int) op1.value+ "");//Screen output OP1
Value} else if (action== "OP2")//indicates that the data is sent to the OP2 {if (op2.isfloat==true)/If OP2 is the floating-point number {int i=1;
Double Num=value;
++op2.dotwei;
while (I<=op2.dotwei) {num*=0.1;
i++; op2.value=op2.value+num;//adds a new decimal point to the operand OP2//Because the Double-precision floating-point number has a higher precision, and we just take its Op2.dotwei save Op2.value=double.parsedou BLE (String.Format ("%.")
+op2.dotwei+ "F", Op2.value)); Because a double-precision floating-point number has a higher precision, we simply take its op2.dotwei display on the screen Display2.settext (String.Format ("%.").
+op2.dotwei+ "F", Op2.value));
} else//op2 is an integer {op2.value=op2.value*10+value;
Display2.settext ((int) op2.value+ "");
}} public void Divide () {run ("/"); } public VoiD Mo () {run ("%");
public void Mul () {Run ("*");
} * * 1/x/public void inverted () {double num;
String str;
if (action== "Op1") {op1.invertedstring= "1/" +op1.value;//1/x form string op1.value=1/op1.value;
Op1.isinverted=true;
Num=op1.value;
} else{op2.invertedstring= "1/" +op2.value;//1/x form string op2.value=1/op2.value;
Op1.isinverted=true;
Num=op2.value;
} str=num+ ""; if (Str.length () >=16)//Calculator screen can display the maximum length of the data {Display2.setfont (new font ("Arial", font.bold,14));/reduce the font output Display2.settex
T (str.substring (0, 16));
else Display2.settext (str);
public void Cut () {run ("-");
public void Add () {run ("+");
public void sum () {Display2.setfont (New Font ("Arial", font.bold,20));
int d1=op1.dotwei,d2=op2.dotwei,i;
Switch (sign) {//After the result is saved to the OP1 case "+": op1.value=op1.value+op2.value;break;
Case "-": op1.value=op1.value-op2.value;break;
Case "*": op1.value=op1.value*op2.value;break; Case "/": OP1.VALUE=OP1. Value/op2.value;break;
Case "%": op1.value=op1.value%op2.value;break;
} if (op2.value==0&&sign== "/")//divisor is 0 {disable=true;
Display2.settext (op1.value+ "");
Display1.settext (NULL);
Action= "OP1";
return;
} if (Op1.value==math.floor (Op1.value))//result is an integer {display2.settext ((int) op1.value+ "");
op1.dotwei=0;
Op1.isfloat=false;
else{//result is a decimal String str=op1.value+ ""; Accurately control the accuracy of arithmetic operations, add, subtract, modulo operations, the number of valid digits after the decimal max (D1,D2) bit if (sign.equals ("+") | | Sign.equals ("-") | |
Sign.equals ("%")) {i=d1>d2?d1:d2;
Str=op1.value+ ""; Str=str.substring (0, Str.indexof (".")
+I+1);//Fetch I-bit output}//accurately control the precision of arithmetic operation result, multiplication operation, the number of valid digits after the decimal point is d1+d2 bit else if (sign.equals ("*")) {i=d1+d2;
BigDecimal bd=new BigDecimal (op1.value);
Op1.value=bd.setscale (I,bigdecimal.round_down). Doublevalue (); Str=op1.value+ "";//update Modified STR}//result exceeds maximum length of display data if (Str.length () >=16) {Display2.setfont (New Font ("Arial", Fo Nt.
bold,14)); Str=str.substring (0, 16);
} display2.settext (str); Op1.dotwei=str.length ()-str.indexof (".")
-1;//update op1w value} display1.settext (null);
Action= "OP1";
When op1.clear=true;//begins a new expression operation, OP1 first resets the op2.clear=true;//to start a new expression operation, OP2 to reset the sign= "";
The public void dot () {if (action== "OP1") op1.isfloat=true;
else op2.isfloat=true;
Display2.settext (Display2.gettext () + ".");}
public void Run (String SIGN) {Display2.setfont (New Font ("Arial", font.bold,20));
Action= "OP2";
int d1=op1.dotwei,d2=op2.dotwei,i; if (!sign.equals ("))//detects whether the case" + "is saved to the OP1 after the first operation of the expression {switch (sign) {//operation): OP1.VALUE=OP1.VALUE+OP2.VALUE;BR
Eak
Case "-": op1.value=op1.value-op2.value;break;
Case "*": op1.value=op1.value*op2.value;break;
Case "/": Op1.value=op1.value/op2.value;break;
Case "%": op1.value=op1.value%op2.value;break;
} String Temp=isspecilehandle (); if (temp==null) Temp=display2.gettext ()//() First save the data in the Display2 text box if (op2.value==0&&sign== "/") the divisor is 0 {disable= TRue
Display2.settext (op1.value+ "");
Display1.settext (Display1.gettext () +op1.value);
} if (Op1.value==math.floor (Op1.value))//result is an integer {display2.settext ((int) op1.value+ "");
op1.dotwei=0;
Op1.isfloat=false;
} else{String str=op1.value+ ""; Accurately control the accuracy of arithmetic operations, add, subtract, modulo operations, the number of valid digits after the decimal max (D1,D2) bit if (sign.equals ("+") | | Sign.equals ("-") | |
Sign.equals ("%")) {i=d1>d2?d1:d2;
BigDecimal bd=new BigDecimal (op1.value);
Op1.value=bd.setscale (I,bigdecimal.round_down). Doublevalue (); Str=op1.value+ "";//update Modified STR}//accurately control the precision of the arithmetic result, multiplication, the number of valid digits after the decimal point is d1+d2 bit else if (sign.equals ("*")) {I=D1+D2
;
BigDecimal bd=new BigDecimal (op1.value);
Op1.value=bd.setscale (I,bigdecimal.round_down). Doublevalue (); Str=op1.value+ "";//update Modified STR}//result exceeds maximum length of display data if (Str.length () >=16) {Display2.setfont (New Font ("Arial", Fo Nt.
bold,14));
Str=str.substring (0, 16);
} display2.settext (str); Op1.dotwei=str.length ()-str.indexof (".")-1;//after each addition, if the result OP1 value is a decimal, update op1w value} sign=sign;
Display1.settext (Display1.gettext () +temp+sign);
After the op2.value=op1.value;//operation, the operand OP2 the default value of OP1 op2.clear=true;//the next time you type the data, OP2 to reset op1.clear=false;//the next time you type the data, OP1 do not reset}/* * Isspecilehandle () * The operand is sqrt () or 1/x, * If yes, return "sqrt (x)" or "1/x" string/public string Isspecilehandle () {Strin
G Temp=null;
if (op1.issqrted) {temp=op1.sqrtedstring;
Op1.issqrted=false;
else if (op2.issqrted) {temp=op2.sqrtedstring;
Op2.issqrted=false;
} if (op1.isinverted) {temp=op1.invertedstring;
Op1.isinverted=false;
else if (op2.isinverted) {temp=op2.invertedstring;
Op2.isinverted=false;
return to temp;
Class Keyadapter extends keyadapter{public void keypressed (KeyEvent e) {int keycode=e.getkeycode (); if ((keycode>=96&&keycode<=105) | | (keycode>=48&&keycode<=57))
Number key {if (keycode>=96) keycode-=48; Switch (keycode) {case 48:Read (0);
Case 49:read (1);
Case 50:read (2);
Case 51:read (3);
Case 52:read (4);
Case 53:read (5);
Case 54:read (6);
Case 55:read (7);
Case 56:read (8);
Case 57:read (9); }//Operator key +-*/= and. and backspace (* = number) Else if (keycode==110| | KEYCODE==46) | | (keycode==111| | keycode==47) | | (keycode==45| | keycode==109) | | keycode==107| | keycode==106| | keycode==61| | keycode==10| | keycode==8) {if (keycode==110| |
keycode==46)) dot (); else if (keycode==111| |
keycode==47) Run ("/"); else if (keycode==45| |
keycode==109) Run ("-");
else if (keycode==107) run ("+");
else if (keycode==106) Run ("*"); else if (keycode==61| |
keycode==10)//= number sum ();
else if (keycode==8) cutend ();
}} public static void Main (string[] args) {//TODO auto-generated Method Stub calc App=new calc ();
}
}
Program screenshot