import java.awt.BorderLayout;
?\[{c8 /V& import java.awt.Color;
F["08+4x=e import java.awt.FlowLayout;
CMUB=tvet import java.awt.GridLayout;
Mfq/e\U import java.awt.event.ActionEvent;
\g+n( import java.awt.event.ActionListener;
\@$l9nNuDe import java.text.DecimalFormat;
ls'E" _ &['\0yO8=2 import javax.swing.JButton;
pda%2-H#u import javax.swing.JFrame;
MS k?$ import javax.swing.JLabel;
u^~DR^yU import javax.swing.JMenu;
G4qH\IF&2M import javax.swing.JMenuBar;
. [ai,A import javax.swing.JMenuItem;
O:eE-F( $ import javax.swing.JPanel;
ok+C(( d import javax.swing.JScrollPane;
\^GrpqS[g import javax.swing.JTextArea;
=&ea import javax.swing.JTextField;
EgZdE>{ V iF>`J public class Calculator implements ActionListener {
5q#NA#5"> //导入动作监听接口设计面板中的单位
+5=Wv2LB JFrame frame;
Mdn%2w+b ?a! f*S JTextField textAnswer;
l;F<b* y#B*_B JPanel panel, panel1, panel2, panel3;
g()G/<5 gx3eD#xlrn JMenuBar mainMenu;
@f1wEtS RQcTydWsF JTextField textMemory;
}(y34 j~OK'b;6 JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
"PZ~#Q+S r3/teuCY JButton buttonBk, buttonCe, buttonC;
,o_~wFP 0j. Iok JButton button[];
|,NfwQq J j,t!aX5k JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
j7/~ %X0 2MxoyETK JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
}c@9!o9X buttonDiv, buttonMod;
!p% Imq ML=<;3#9\ JButton buttonSqrt, buttonDao, buttonEqual;
{P{'R&T*& X2849D JMenu editMenu, viewMenu, helpMenu;
i!07 e p]Z93_}t4 JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp,
>iE/wN aboutCal;
r.Lf5&3, *y.tLw DecimalFormat df; //设置数据输出精度
Y %;Z}G=# !qzLn boolean clickable; //控制当前能否按键
0W$Yn9$=2f iS. m%'? double memoryd; //使用内存中存储的数字
l)t~Ws (-h (~V)0Zz$X int memoryi;
X~)lixX~D 4~T!ASyDJ( double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
t ' Mi 6Y)iU${ short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
xtBT's} 5%kH!,uZw String copy; //做复制用
f^h]A@ lPL{ 9!/ JTextArea help; //帮助
- KS-EE]; !mode JScrollPane scrollHelp;
5@q1`,v^ 1+3 xM#HR //构造函数
-XZ)=LlM public Calculator() {
jMRXE} clickable = true;
OS6?t answerd = 0;
jWLg+[n`F frame = new JFrame("计算器");
!k0[ df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
n)R?c@A"u textAnswer = new JTextField(15);
~Hw'S6-8gg textAnswer.setText("");
XG^j textAnswer.setEditable(false);
5T JIK O', textAnswer.setBackground(new Color(255, 255, 255));
4dg@7"(h. panel = new JPanel();
b@.pYdW| frame.getContentPane().add(panel);
$9tD/*q{K panel1 = new JPanel();
T&jl'g?Vt panel2 = new JPanel();
z XA$1V] panel.setLayout(new BorderLayout());
xgwNbAS //设计整个面板
+(le2 0_ mainMenu = new JMenuBar();
'L(`D>|UJb panel.add(mainMenu, BorderLayout.NORTH);
3Dg m panel.add(textAnswer, BorderLayout.CENTER);
D`OsM3 ' panel.add(panel1, BorderLayout.SOUTH);
+<&t=p. panel1.setLayout(new BorderLayout());
|iS&HXOU textMemory = new JTextField(3);
MS )(qZg? textMemory.setEditable(false);
Y8 .5O\| textMemory.setBackground(new Color(217, 217, 217));
,nyo7,3m labelMemSpace = new JLabel(" ");
Jl+OWs~ R buttonBk = new JButton("Backspace");
+Ffb&<( buttonBk.setForeground(new Color(255, 0, 0));
J+Cxd o{~ buttonCe = new JButton("CE");
sUt[!e buttonCe.setForeground(new Color(255, 0, 0));
WROu^i)a buttonC = new JButton("C");
%BX{>h buttonC.setForeground(new Color(255, 0, 0));
[{d8K buttonBk.addActionListener(this);
/jPzTj{bM buttonCe.addActionListener(this);
7[!9KE buttonC.addActionListener(this);
f7nZsK# panel1.add(panel2, BorderLayout.NORTH);
@Zd?p4z panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
atqi6Y5h panel2.add(textMemory);
Xv?H1U panel2.add(labelMemSpace);
Vqn54xI&eh panel2.add(buttonBk);
dP(4r+Ty panel2.add(buttonCe);
|.Mbu6 panel2.add(buttonC);
Q\_IgjQ panel3 = new JPanel();
P10mR zg; panel1.add(panel3, BorderLayout.CENTER);
J"*nrf4+6 button = new JButton[10];
oBuK!sv for (int i = 0; i < button.length; i++) {
2E C"y44 button[i] = new JButton(Integer.toString(i));
|/-``O button[i].setForeground(new Color(0, 0, 255));
~v7+}-L }
-v9[ >6$ buttonMC = new JButton("MC");
'(%IL'o buttonMC.setForeground(new Color(255, 0, 0));
G eZ,81( buttonMR = new JButton("MR");
\17NuB^Dk buttonMR.setForeground(new Color(255, 0, 0));
B a r[F m buttonMS = new JButton("MS");
a<\O]YM>^ buttonMS.setForeground(new Color(255, 0, 0));
lMq*I < buttonMAdd = new JButton("M+");
^2C(KN!e buttonMAdd.setForeground(new Color(255, 0, 0));
fql"?XYy( buttonDot = new JButton(".");
0M)io/ buttonDot.setForeground(new Color(0, 0, 255));
FAqWb#8.7 buttonAddAndSub = new JButton("+/-");
\X,l5+-BKs buttonAddAndSub.setForeground(new Color(0, 0, 255));
W_z8]%A buttonAdd = new JButton("+");
S#xte1Bid buttonAdd.setForeground(new Color(255, 0, 0));
\$ RYj w buttonSub = new JButton("-");
MmWB Eb; buttonSub.setForeground(new Color(255, 0, 0));
RbjH"Z_? buttonMul = new JButton("*");
UMTJo buttonMul.setForeground(new Color(255, 0, 0));
mX|'VFv ^ buttonDiv = new JButton("/");
&ohXkB#= buttonDiv.setForeground(new Color(255, 0, 0));
jV0 pEGzE# buttonMod = new JButton("%");
{ Tvvv5jn buttonMod.setForeground(new Color(0, 0, 255));
ho>Y=as buttonSqrt = new JButton("sqrt");
gVbd7S buttonSqrt.setForeground(new Color(0, 0, 255));
yzmEWF~= buttonDao = new JButton("1/x");
SfB20{8Un buttonDao.setForeground(new Color(0, 0, 255));
OA@nz]b~ buttonEqual = new JButton("=");
f%>g`s;f7 buttonEqual.setForeground(new Color(255, 0, 0));
@JLKfh%JGR //将所有行为与监听绑定
/y8qI panel3.setLayout(new GridLayout(4, 6));
gQaH3FK panel3.add(buttonMC);
M|+R<9_ buttonMC.addActionListener(this);
Gn&! Kr% panel3.add(button[7]);
@~2_'*A&. button[7].addActionListener(this);
MFPbm[`(^ panel3.add(button[8]);
N:T)#hX button[8].addActionListener(this);
Wy[7c]8 panel3.add(button[9]);
+KY') button[9].addActionListener(this);
VKB{RbqQ panel3.add(buttonDiv);
-PHglwT= buttonDiv.addActionListener(this);
V'|[2U6 panel3.add(buttonSqrt);
8!?%k& buttonSqrt.addActionListener(this);
@(U {2 panel3.add(buttonMR);
R,vi#~5\ buttonMR.addActionListener(this);
T:zGq.W$Q_ panel3.add(button[4]);
!r?sEBt button[4].addActionListener(this);
IK`W#Gs panel3.add(button[5]);
5DNseC button[5].addActionListener(this);
|hl&!K{ panel3.add(button[6]);
$jt3Y9)}+ button[6].addActionListener(this);
#Oi}>3)2m panel3.add(buttonMul);
y\o @w1 buttonMul.addActionListener(this);
2c#q1!3Q4 panel3.add(buttonMod);
WX?mKo]- buttonMod.addActionListener(this);
Bu y HF1yN panel3.add(buttonMS);
V-N6iz buttonMS.addActionListener(this);
B+oCUCtoP panel3.add(button[1]);
d!wt?g6vG button[1].addActionListener(this);
v4E%<P/ panel3.add(button[2]);
w3%-@j0( button[2].addActionListener(this);
7Bg$`:$ panel3.add(button[3]);
1]}Pw"0 button[3].addActionListener(this);
>BBJr7Y& panel3.add(buttonSub);
2xo PS'DNW buttonSub.addActionListener(this);
FpyrD|.J5 panel3.add(buttonDao);
P#KR:W buttonDao.addActionListener(this);
a[ :3`y8; panel3.add(buttonMAdd);
iLn!]=e buttonMAdd.addActionListener(this);
;OFD{DpF! panel3.add(button[0]);
f9wx-ugk button[0].addActionListener(this);
~_O^hKa" panel3.add(buttonAddAndSub);
F/3v$Md)5_ buttonAddAndSub.addActionListener(this);
}r57 A 5\ panel3.add(buttonDot);
ye |91~E2 buttonDot.addActionListener(this);
R{lD?KJ/I panel3.add(buttonAdd);
!hfl'nUO buttonAdd.addActionListener(this);
(&.J9] panel3.add(buttonEqual);
e(3_>4fRp buttonEqual.addActionListener(this);
zdU zl> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Ec|^U frame.pack();
XlVL] frame.show();
LF}(q`g. }
d%ACB.n\h \F{Rb/]p //设置各个按钮行为
wyUl.=Wv| public void actionPerformed(ActionEvent event) {
gEljQ boolean sign = false; //判断是否是double型数参与运算,是为true,不是为false
^r5| E Object temp = event.getSource();
r(YA T2s try {
{$7q7@. //如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
:$RR\2{;ud for (int i = 0; i <= 9; i++)
8UE1H3O ( if (temp == button[i] && clickable == true)
`"P6Y24i` textAnswer.setText(textAnswer.getText()
7HTx*t<` + Integer.toString(i));
"'\EKxDL[h //按下"."按钮时,判断当前文本框内字符串中含不含".",如果已含,则不允许再插入"."
baQ[n)zs if (temp == buttonDot && clickable == true) {
jd} 7VnJ boolean isDot = false;
$) QNP` if (textAnswer.getText().length() == 0)
ZEatD,) isDot = true;
gd7e<i;W for (int i = 0; i < textAnswer.getText().length(); i++)
1XRo^WeZ if ('.' == textAnswer.getText().charAt(i)) {
D FlW$~u isDot = true;
"8@* bv break;
VDk'"O }
Hz[ )1!0V if (isDot == false)
"M0|qf textAnswer.setText(textAnswer.getText() + ".");
"jI B }
goHaTEj if ((temp == buttonAdd || temp == buttonSub || temp == buttonMul || temp == buttonDiv)
|!I*"AuCH && clickable == true) {
!pM#%& //"+"操作
{L1,_h" if (temp == buttonAdd) {
oBTDQvY\ switch (prekey) {
RhNTj;W*6M case 0:
e 5s\.+ answerd += Double.parseDouble(textAnswer.getText());
SImS`&W=8 break;
\:):dwq. case 1:
GmA.-C answerd -= Double.parseDouble(textAnswer.getText());
UOy y4 break;
1l!!hc7 case 2:
>YhwP/alu answerd *= Double.parseDouble(textAnswer.getText());
KFmqrb" break;
J<VXAc2~ case 3:
[<=zi if (Double.parseDouble(textAnswer.getText()) == 0) {
> !s.V.>r ]8=R 5"_c //textAnswer="aa";
.)^$=:Xd textAnswer.setText("除数不能为零");
MGMMq< clickable = false;
E_r<w>V/M } else
r'txzC answerd /= Double.parseDouble(textAnswer.getText());
IAj\NI][ break;
(t|9xq[iM default:
N$D)oy o. answerd = Double.parseDouble(textAnswer.getText());
O5>I }
Cjc;P48n@ textAnswer.setText("");
eP5iI%} prekey = key = 0;
BvK^~ }
D-,l(e'~ //"-"操作
&9\y]pLu if (temp == buttonSub) {
XhSR ;u"( switch (prekey) {
FYWiI}^* case 0:
p>2Q $ answerd += Double.parseDouble(textAnswer.getText());
!3$lAi[c0 break;
aB&R6[T8 case 1:
gG@t.9c>P answerd -= Double.parseDouble(textAnswer.getText());
L? M<C I break;
fDv].Q.*K case 2:
BM0pv@f_7 answerd *= Double.parseDouble(textAnswer.getText());
Gq=>;5RI break;
HWul.{8 case 3:
I>YR\F if (Double.parseDouble(textAnswer.getText()) == 0) {
f>MD)[Ew" textAnswer.setText("除数不能为零");
nKtK}tsEJ clickable = false;
T8"Vm#T } else
D$PL'QK1 answerd /= Double.parseDouble(textAnswer.getText());
wE^2$y break;
BhhkdoF=\$ default:
ONARX@zv answerd = Double.parseDouble(textAnswer.getText());
ns 43R }
t;i PTPJF textAnswer.setText("");
yB_.;/#D prekey = key = 1;
;+q0PCw }
dYP*Zw` //"*"操作
*+9) C @ if (temp == buttonMul) {
a]#P5'yy* switch (prekey) {
^0nS case 0:
>x0 wV y answerd += Double.parseDouble(textAnswer.getText());
M v*/LtHQ break;
?5 !:l case 1:
c3ahd$ answerd -= Double.parseDouble(textAnswer.getText());
SD0/$/rD break;
1P#.7 7- case 2:
G/RZ~.*?` answerd *= Double.parseDouble(textAnswer.getText());
W08@!'^? break;
+3? ` case 3:
><IdTig if (Double.parseDouble(textAnswer.getText()) == 0) {
?nQ"qGh7 textAnswer.setText("除数不能为零");
9 c7-e clickable = false;
x*n<y%9- } else
N _qG9^ answerd /= Double.parseDouble(textAnswer.getText());
+j bW s|B break;
E(+]p" default:
qyk| '$J answerd = Double.parseDouble(textAnswer.getText());
Q|Bb+<w }
~j4c6VD //textAnser = "";
pBcn@/xNT textAnswer.setText("");
w#^:pn`g prekey = key = 2;
0hCHTP }
eBBjtSuU? //"/"操作
*wkbr5IF8 if (temp == buttonDiv) {
X&Cn$.^n switch (prekey) {
b4R^8)d"I case 0:
yPcg(y answerd += Double.parseDouble(textAnswer.getText());
6).RVBsnT break;
tA zQ&vq&t case 1:
7OHV!iSdZ answerd -= Double.parseDouble(textAnswer.getText());
kJo#B27Hf break;
K[i<@xk case 2:
b5L)Xq}# answerd *= Double.parseDouble(textAnswer.getText());
p?4|3U:[2k break;
h nIf7vJ case 3:
|9XrPMynS if (Double.parseDouble(textAnswer.getText()) == 0) {
[ePQ)5 q textAnswer.setText("除数不能为零");
nq2} clickable = false;
!/}&|Rc" } else
LgNAPQ7 answerd /= Double.parseDouble(textAnswer.getText());
hx!X|V break;
Xs]s) qHM default:
] y*^ gj& answerd = Double.parseDouble(textAnswer.getText());
TmUx&Flj| }
fJAUS_Y?C textAnswer.setText("");
m.9+w)] prekey = key = 3;
gFE/[GQ }
J<LP2K,5 }
~R-.R!| //"="操作
|-(Npd< if (temp == buttonEqual && clickable == true) {
i%hl^rP //如果连续按"=",则进行连续运算
kaBC CoOJ if (prekey == 5) {
t'N~] if (key == 0) {
zA K'x answerd += vard;
)ue6Up{ textAnswer.setText(df.format(answerd));
6vI 6X=?$= }
o"l T8c= if (key == 1) {
p)Y`K1g answerd -= vard;
"9(F textAnswer.setText(df.format(answerd));
L[- *Z[MGb }
O{_mA #8 if (key == 2) {
o~K.s<!AG answerd *= vard;
2<bL. . textAnswer.setText(df.format(answerd));
vQC !:@ }
e#NMIC+)T if (key == 3) {
bzF7n8oo if (Double.parseDouble(textAnswer.getText()) == 0) {
* mPdy\OX textAnswer.setText("除数不能为零");
l8{|j?U<9 clickable = false;
:`R+- } else {
Nmln,jcq answerd /= vard;
Z]hUe]Tr textAnswer.setText(df.format(answerd));
7Y=eXy]U }
Q b7"$Z }
ZgEO&R- } else {
S"<%9L vard = Double.parseDouble(textAnswer.getText());
X>r]b\L if (key == 0) {
X 1Pey H prekey = -1;
{[ A?6Yf answerd += Double.parseDouble(textAnswer.getText());
SktK"&K textAnswer.setText(df.format(answerd));
NVocVbgDp }
`%Y1#8F' if (key == 1) {
uY\f/WPkd prekey = -1;
h"% dc6 answerd -= Double.parseDouble(textAnswer.getText());
O`1j Wj textAnswer.setText(df.format(answerd));
k I a'4n }
t0p5,3KJ 8 if (key == 2) {
-l= _S8 prekey = -1;
.oaDOFJ answerd *= Double.parseDouble(textAnswer.getText());
mnIr]. textAnswer.setText(df.format(answerd));
,ZY0sW7znJ }
ItPjAsMaD if (key == 3) {
r(<@/K|T- prekey = -1;
$XY8;m 2b if (Double.parseDouble(textAnswer.getText()) == 0) {
lfSk,`0Lm textAnswer.setText("除数不能为零");
Z>N >9@@ clickable = false;
9_;Yfw } else {
>d673=JP answerd /= Double.parseDouble(textAnswer.getText());
X_kEXJD textAnswer.setText(df.format(answerd));
F{2 v }
Nf,>-hi }
z[^WKTh> }
a]v b6:{ ` prekey = 5;
37XU]WP_ }
;i3<h%%x //"%"操作,对第二个操作数除以100
o~y72Y-@ if (temp == buttonMod && clickable == true) {
U+J0:"|R~ if (answerd == 0) {
pYbc^-p String s = textAnswer.getText();
[i-5]0Bc7 textAnswer.setText(s);
i0Nh& _[ } else {
/ZM/fx9 boolean isDot = false;
/,5 c%"c- for (int i = 0; i < textAnswer.getText().length(); i++)
II9vL8l if ('.' == textAnswer.getText().charAt(i)) {
h[*%5uo<, isDot = true;
@4@.4k jU break;
Y`/5otrB }
3f*L*u; # //如果是double数,除100
M+RE*9- ^ if (isDot == true) {
(,Vd=Fpzi double dtemp = Double.parseDouble(textAnswer.getText());
a p"Yd dtemp = dtemp / 100.0;
))h n=!5 textAnswer.setText(Double.toString(dtemp));
@nq(x/I a* } else {
DVWl "tS7 //如果是int数但能被100整除,则去掉末尾两个零
J0Rz/!< if (Integer.parseInt(textAnswer.getText()) % 100 == 0) {
b1HS) <; int itemp = Integer.parseInt(textAnswer.getText());
C>Ii?S|q itemp /= 100;
g{Qg`6j textAnswer.setText(Integer.toString(itemp));
)9iH"S| }
5iG| m2Q; //如果是int数,但不能被100整除,则按double数处理
6W+7ll^Z else {
%wdNK double dtemp = Double.parseDouble(textAnswer
P.V4H*B\ .getText());
n1w7(]z dtemp = dtemp / 100.0;
I&C QAN textAnswer.setText(Double.toString(dtemp));
pqdIRHdm<d }
YA(6p + }
EMUw~jW$V }
CdT2<Fh IR }
%^v`QE1 //开根号运算
>D>D}IS| if (temp == buttonSqrt && clickable == true) {
-6>8:I String s = textAnswer.getText();
}K|2DG2A if (s.charAt(0) == '-') {
]T?C.$oL] textAnswer.setText("负数不能开根号");
cM HltI^49 clickable = false;
\mw r"y } else
_!KCud textAnswer.setText(Double.toString(java.lang.Math
K8b~DaZG? .sqrt(Double.parseDouble(textAnswer.getText()))));
g@nPm (S }
QzX_z `p //倒数运算
"0dA}0MP if (temp == buttonDao && clickable == true) {
ToN;0603 if (textAnswer.getText().charAt(0) == '0'
BbcDp"A4} && textAnswer.getText().length() == 1) {
ZR:E}A textAnswer.setText("零不能求倒数");
l{Bf ~zvaX clickable = false;
~L|p>^YU } else {
$8v rT;cO boolean isDec = true;
%8[@gb&G'0 int i, j, k;
zK`6%kK String s = Double.toString(1 / Double
T)Y@ c .parseDouble(textAnswer.getText()));
$~S^Z9N for (i = 0; i < s.length(); i++)
9/M\/4 @ if (s.charAt(i) == '.')
5emi:|z break;
ugS,1e.S for (j = i + 1; j < s.length(); j++)
mi*D>M-3 if (s.charAt(j) != '0') {
L0jgt$Y3 isDec = false;
~lenJ;C7 break;
w c1K? }
[kAy#=Bz# if (isDec == true) {
F w(G String stemp = "";
I~mj#?g#rG for (k = 0; k < i; k++)
&N*D2Fh stemp += s.charAt(k);
& $^Rv5H3R textAnswer.setText(stemp);
}za&\$J"0 } else
D|E{UwAkN textAnswer.setText(s);
Y%LAf<R,'% }
e:?N<L%l: }
8pZ*[4b //按下"+/-"按钮时处理
'YW J!(B if (temp == buttonAddAndSub && clickable == true) {
}v Wd[` boolean isNumber = true;
YO|C `d String s = textAnswer.getText();
)'W9g0Iy for (int i = 0; i < s.length(); i++)
=t) 5G\ if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'
J(iV!zn5 || s.charAt(i) == '.' || s.charAt(i) == '-')) {
dYOw;)N isNumber = false;
5\U}4%d1+ break;
Scsm,=By }
&|y7#C0 if (isNumber == true) {
h65QR //如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
x'KQZ}*Mo if (s.charAt(0) == '-') {
^f,Ls textAnswer.setText("");
4E*7Q1S|K for (int i = 1; i < s.length(); i++) {
Yo;&k/ char a = s.charAt(i);
OQFSS&# textAnswer.setText(textAnswer.getText() + a);
r[sI.)d#* }
9<vMnEs7 }
x 2\"6 //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
7BOA]W& else
DCDh-;$LQG textAnswer.setText('-' + s);
u S'tc }
.H]H uR }
j0-wA h09 //计算器有关内存操作
DVs moby_ //'MC'的操作,将内存清0
kD|8*M2F@ if (temp == buttonMC && clickable == true) {
#IX`'/4wC memoryd = memoryi = 0;
(KOS\A ( textMemory.setText("");
e.DzH'$p }
/G)^(Ph0 //'MS'的操作,将当前文本框内容保存入内存,显示'M'
TsM:Rfi if (temp == buttonMS && clickable == true) {
'x5"QtIj boolean isDot = false;
(SMP"M) textMemory.setText(" M");
mg}kq +C for (int i = 0; i < textAnswer.getText().length(); i++)
R{-iqe if ('.' == textAnswer.getText().charAt(i)) {
8Rcp DE isDot = true;
!DyHAWZ] break;
^QVJ7kjp }
Jl7yjx5 //如果是double,则存入memoryd(double存储器)
A#(9]k)& if (isDot == true) {
Q4y|'sNh ~ memoryd = Double.parseDouble(textAnswer.getText());
6@[~cn<E memoryi = 0; //保证存储器中存放最新的值
N];3WI }
21>w*%M //如果是int,则存入memoryi(int存储器)
EiYMjW else {
tJFx9m)D>K memoryi = Integer.parseInt(textAnswer.getText());
G%zz<&( memoryd = 0; //保证存储器中存放最新的值
QF&*`}8 }
+uAk:/~ }
012T#F96, //'MR'的操作,将存储器中的信息输出
%g,d_Qq37 if (temp == buttonMR && clickable == true) {
mH?R+C# if (memoryd != 0)
SY Kl/+c^\ textAnswer.setText(Double.toString(memoryd));
+]{_V<= if (memoryi != 0)
{x]1Bq v textAnswer.setText(Integer.toString(memoryi));
>o+ g$ }
U*Q:T8 bs //'M+'的功能,将当前文本框里的数据和存储器中数据相加后,再存入存储器
4I<4)zj if (temp == buttonMAdd && clickable == true) {
x(^D+ boolean isDot = false;
S7&o]u for (int i = 0; i < textAnswer.getText().length(); i++)
2g$zk`jiY if ('.' == textAnswer.getText().charAt(i)) {
v*^FeD$h isDot = true;
* i;G1O& break;
3d`Qd*HI3 }
#5*{p0=% if (memoryi != 0) { //存储中是一个int型数
56V&6NV^4 if (isDot == false) //被加数是一个int型数
d-=n$o memoryi += Integer.parseInt(textAnswer.getText());
?E~i else { //被加数是一个double型数,则将int存储器中数传入double存储器与当前数相加,int存储器清零
q^U%n{[T memoryd = memoryi
yK"sn$ + Double.parseDouble(textAnswer.getText());
?o]?RHa memoryi = 0;
%8VFU }
(#RdIH]v7t } else
i31R Jf51u memoryd += Double.parseDouble(textAnswer.getText());
57,j$E }
U|eGYSny //按下'Backspace'键,利用循环将当前字符串中的最后一个字母删除
$RN,Zbv if (temp == buttonBk && clickable == true) {
. ^Iiwm=y String s = textAnswer.getText();
/8&*ik^n textAnswer.setText("");
M8%*5!X7 for (int i = 0; i < s.length() - 1; i++) {
"jlbYja9nQ char a = s.charAt(i);
P bt#> o textAnswer.setText(textAnswer.getText() + a);
y/ \83 }
|5rKAzz }
NGse1 |M //按下'CE'按钮,将当前文本框内数据清除
{K]y kFi if (temp == buttonCe) {
`\_w#S[ textAnswer.setText("");
gWoyMK#e clickable = true;
#PO!dOeH }
%~ %n<?I //按下'C'按钮,文本框内数据清除,同时var,answer清0
va-808w= if (temp == buttonC) {
}=ogj:/drR vard = answerd = 0;
<?m 1@1@ textAnswer.setText("");
s[ Gx clickable = true;
EQv-,?A; }
"} Ef]n{p\ }
_uX-M{ W7 //输入中如果有操作非法,比如按下两次'+',捕获异常
4}{4/t? catch (Exception e) {
xZ:eYb textAnswer.setText("操作非法");
t,+.. clickable = false;
4v V@8t6E }
~),ajU, }
}xYR{S h< \5|?@ //主函数
wDzgFVWo public static void main(String args[]) {
t^; M4R&P) new Calculator();
34rI &+R }
iZWXj?,>r< }
:c)( )CyK -/{
查看本文来源