计算器Word格式.docx
《计算器Word格式.docx》由会员分享,可在线阅读,更多相关《计算器Word格式.docx(17页珍藏版)》请在冰点文库上搜索。
//key为0标示+,为1表示-,为2表示*,为3表示/
shortkey=-1;
//pkey的取值为-1~5
//pkey为-1表示第一次按下=按钮,为5说明不是第一次按下=按钮
//pkey为其他值0,1,2,3时分别代表+,-,*,/
shortpkey=-1;
Frameframe=newFrame("
Calculator"
);
//定义标题为Calculator的窗体
TextFieldtextfield=newTextField(30);
//定义计算机
//定义backspace,ce,c0按钮
Buttonbackspace=newButton("
Backspace"
Buttonce=newButton("
CE"
Buttonc0=newButton("
C"
//定义面板,其中backspace,ce,c0按钮和计算机兰textfeild放在面板p1中
//其余按钮放在面板p2中
Panelp1=newPanel();
Panelp2=newPanel();
//定义界面上的按钮数组,即除去backspace,ce,c0按钮的所有按钮
Stringnames[]={"
7"
"
8"
9"
/"
sqrt"
4"
5"
6"
*"
%"
1"
2"
3"
-"
1/x"
0"
+/-"
."
+"
="
};
Buttonbb[]=newButton[names.length];
publicstaticvoidmain(String[]args){
Calculatorcal=newCalculator();
cal.go();
}
publicvoidgo(){
frame.setSize(300,200);
frame.setLayout(newBorderLayout());
Fontfonts=newFont("
楷体"
Font.PLAIN,12);
//第一页
//设置面板p1的字体和布局管理类型,将textfeild对象添加到面板中
p1.setFont(fonts);
p1.setLayout(newGridLayout(2,1,5,10));
textfield.setFont(fonts);
p1.add(textfield,null);
//backspace,ce,c0按钮注册addActionListener方法
backspace.addActionListener(this);
ce.addActionListener(this);
c0.addActionListener(this);
//将backspace,ce,c0按钮添加到p1面板中
p1.add(backspace,null);
p1.add(ce,null);
p1.add(c0,null);
//设置面板p2的布局管理器类型为GridLayout
p2.setLayout(newGridLayout(4,5,5,5));
//为按钮组中的按钮注册addActionListener方法
for(inti=0;
i<
names.length;
i++){
bb[i]=newButton(names[i]);
p2.add(bb[i]);
bb[i].addActionListener(this);
//设置面板p1和面板p2在窗体中的位置
//面板p1在窗体的North方向,面板p2在窗体的South方向
frame.add(p1,BorderLayout.NORTH);
frame.add(p2,BorderLayout.SOUTH);
frame.setVisible(true);
//当单击关闭按钮时退出系统
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
publicvoidactionPerformed(ActionEventae){
Strings;
//通过ActionEvent类的getActionCommand()方法,得出动作是由哪个按钮触发的。
//第二页
s=ae.getActionCommand();
//用户单击一次,计算栏里便去掉一个字符
//先将计算栏里的字符清空,再添加除最好一个字符的字符串
if(s.equals("
)&
&
number){
t_content=textfield.getText();
textfield.setText("
"
for(inti=0;
t_content.length()-1;
charc=t_content.charAt(i);
textfield.setText(textfield.getText()+c);
//用户单击的是CE按钮,计算栏清空
if(s.equals("
)){
number=false;
//用户单击的是C按钮
data1=data2=0.0;
//用户单击的是sqrt按钮,其中负数不能被开方
if(t_content.charAt(0)=='
-'
){
负数不能被开方"
number=false;
}else{
textfield.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(t_content))));
//用户单击的是1/x求倒数按钮,求倒数时分母不能为0
)&
number){
if(t_content.equals("
)){//第三页
textfield.setText("
零不能求倒数"
}else{
textfield.setText(Double.toString(1/Double.parseDouble(t_content)));
//用户单击的是%按钮
if(data2==0){
textfield.setText(t_content);
booleang=false;
textfield.getText().length();
i++)
if('
.'
==textfield.getText().charAt(i)){
g=true;
break;
//如果是Double数,除100
if(g=true){
doubledtemp=Double.parseDouble(textfield.getText());
dtemp=dtemp/100.0;
textfield.setText(Double.toString(dtemp));
//如果是int数但能被100整除,则去掉末尾两个0
else{
if(Integer.parseInt(textfield.getText())%100==0){
intitemp=Integer.parseInt(textfield.getText());
itemp/=100;
textfield.setText(Integer.toString(itemp));
//如果是int数,但不能被100整除,则按Double数处理
}//第四页
//对正负数字的处理
Stringcontent;
for(inti=1;
t_content.length();
textfield.setText(textfield.getText()+t_content.charAt(i));
+t_content);
//所按下的按钮为+按钮
switch(pkey){
case0:
data2+=Double.parseDouble(textfield.getText());
case1:
case2:
case3:
if(textfield.getText().equals("
除数不能为零"
key=-1;
data2/=data1;
textfield.setText(Double.toString(data2));
default:
data2=Double.parseDouble(textfield.getText());
pkey=key=0;
}
//第五页
//所按下的按钮为-按钮
pkey=key=1;
}
//所按下的按钮为*按钮
)){//第六页
pkey=key=2;
//所按下的按钮为/按钮
)){
pkey=key=3;
}//第七页
//所按下的按钮为=,求结果
//如果不是第一次按下=按钮
if(pkey==5){
data2+=data1;
data2-=data1;
data2*=data1;
//如果是第一次按下=按钮
else{
data1=Double.parseDouble(textfield.getText());
switch(key){
data2-=Double.parseDouble(textfield.getText());
//第八页
data2*=Double.parseDouble(textfield.getText());
data2/=Double.parseDouble(textfield.getText());
pkey=5;
//按下的按钮时数字按钮
switch(s.charAt(0)){
case'
0'
:
1'
2'
3'
4'
5'
6'
7'
8'
9'
number=true;
textfield.setText(textfield.getText()+s);
//对.的处理
//第九页
//对计算机栏中的数据进行检验
//如果其中有小数点,则用户单击该按钮时不在添加小数点
if(t_content.charAt(i)=='
//数据中已经有小数点
if(g==true)
//数据中没有小数点,当用户单击小数点时添加小数点
if(g==false)
textfield.setText(t_content+"
privateStringInteger(Stringstring){
//TODOAuto-generatedmethodstub
returnnull;