Java简单计算器实验报告.docx
- 文档编号:27307503
- 上传时间:2023-06-29
- 格式:DOCX
- 页数:17
- 大小:26.72KB
Java简单计算器实验报告.docx
《Java简单计算器实验报告.docx》由会员分享,可在线阅读,更多相关《Java简单计算器实验报告.docx(17页珍藏版)》请在冰豆网上搜索。
Java简单计算器实验报告
简单计算器(Java)
运行界面
实验代码
packageComputer;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassMyComputerextendsJFrameimplementsActionListener{
JTextAreamemoryArea=newJTextArea("",1,3);
JTextAreadispresult=newJTextArea("0.",1,20);
JButtonclear=newJButton("C");
JButtonjbuttons[]=newJButton[24];//***
doubleresult=0,first=0,second=0;
doublememery=0;
charfirstsymbol='\0',secondsymbol='\0';
booleanprev=true,repeat=true,dot=true;
MyComputer(){//定义显示样式
super("Calculator");
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exceptione){
System.out.print(e);
}
JPanelresultField=newJPanel();//放置结果、清除键、保存标识的区域,上半部分
JPanelbuttonField=newJPanel();//放置按键,下半部分
Containerall=getContentPane();//组合上述两部分
GridLayoutgrid1=newGridLayout(4,6,3,3);//按键
memoryArea.setEditable(false);
clear.addActionListener(this);
dispresult.setEditable(false);
resultField.add(memoryArea);
resultField.add(clear);
resultField.add(dispresult);
all.setLayout(newFlowLayout());
all.add(resultField);
Stringbuttonname[]={"sin","MC","0","1","2","+","cos","MR","3","4","5","-","tan","MS","6","7","8","x","+/-","M+"
"9",".","=","/"};
buttonField.setLayout(grid1);
for(inti=0;i<6;i++){
for(intj=0;j<4;j++){
jbuttons[i*4+j]=newJButton(buttonname[i*4+j]);
jbuttons[i*4+j].addActionListener(this);//为按键注册监听器
buttonField.add(jbuttons[i*4+j]);
}
}
all.add(buttonField);
setSize(400,210);
setResizable(true);
setVisible(true);
}
publicvoidpressNumber(Stringn){
if(prev){
dispresult.setText(n);
prev=false;
}
elsedispresult.append(n);
}
publicbooleandivide(doubled){
if(d==0){
dispresult.setText("除数不能为0!
");
prev=true;
repeat=true;
firstsymbol='\0';
secondsymbol='\0';
returntrue;
}
returnfalse;
}
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
//判断事件源,进行相应处理
if(source==clear){
dispresult.setText("0.");
firstsymbol='\0';
secondsymbol='\0';
prev=true;
repeat=true;
dot=true;
return;
}
if(source==jbuttons[0]){
doubletemp=Math.sin(Double.parseDouble(dispresult.getText()));
dispresult.setText(String.valueOf(temp));
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[1]){
memery=0;
memoryArea.setText("");
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[2]){
pressNumber("0");
repeat=false;
return;
}
if(source==jbuttons[3]){
pressNumber("1");
repeat=false;
return;
}
if(source==jbuttons[4]){
pressNumber("2");
repeat=false;
return;
}
if(source==jbuttons[6]){
doubletemp=Math.cos(Double.parseDouble(dispresult.getText()));
dispresult.setText(String.valueOf(temp));
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[7]){
if(memoryArea.getText().equals("M"))
dispresult.setText(String.valueOf(memery));
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[8]){
pressNumber("3");
repeat=false;
return;
}
if(source==jbuttons[9]){
pressNumber("4");
repeat=false;
return;
}
if(source==jbuttons[10]){
pressNumber("5");
repeat=false;
return;
}
if(source==jbuttons[12]){
doubletemp=Math.tan(Double.parseDouble(dispresult.getText()));
dispresult.setText(String.valueOf(temp));
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[13]){
memery=Double.parseDouble(dispresult.getText());
if(memery!
=0)memoryArea.setText("M");
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[14]){
pressNumber("6");
repeat=false;
return;
}
if(source==jbuttons[15]){
pressNumber("7");
repeat=false;
return;
}
if(source==jbuttons[16]){
pressNumber("8");
repeat=false;
return;
}
if(source==jbuttons[18]){
doubletemp=-Double.parseDouble(dispresult.getText());
dispresult.setText(String.valueOf(temp));
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[19]){
memery+=Double.parseDouble(dispresult.getText());
if(memery!
=0)memoryArea.setText("M");
prev=true;
repeat=false;
dot=true;
return;
}
if(source==jbuttons[20]){
pressNumber("9");
repeat=false;
return;
}
if(source==jbuttons[21]){
if(dot){
pressNumber(".");
dot=false;
repeat=false;
}
return;
}
if(source==jbuttons[22]){
second=Double.parseDouble(dispresult.getText());
dot=true;
switch(secondsymbol){
case'*':
second*=first;
break;
case'/':
if(divide(second))return;
second=first/second;
}//endofswitch(secondsymbol)
secondsymbol='\0';
switch(firstsymbol){
case'+':
result+=second;
break;
case'-':
result-=second;
break;
case'*':
result*=second;
dispresult.setText(String.valueOf(result));
break;
case'/':
if(divide(second))return;
result/=second;
}//endofswitch(firstsymbol)
if(firstsymbol!
='\0')dispresult.setText(String.valueOf(result));
firstsymbol='\0';
prev=true;
repeat=false;
return;
}
if(source==jbuttons[5]){
dot=true;
if(repeat){
firstsymbol='+';
return;
}
second=Double.parseDouble(dispresult.getText());
switch(secondsymbol){
case'*':
second*=first;
break;
case'/':
if(divide(second))return;
second=first/second;
}//endofswitch(secondsymbol)
secondsymbol='\0';
switch(firstsymbol){
case'\0':
result=second;
firstsymbol='+';
break;
case'+':
result+=second;
dispresult.setText(String.valueOf(result));
break;
case'-':
result-=second;
firstsymbol='+';
dispresult.setText(String.valueOf(result));
break;
case'*':
result*=second;
firstsymbol='+';
dispresult.setText(String.valueOf(result));
break;
case'/':
if(divide(second))return;
result/=second;
firstsymbol='+';
dispresult.setText(String.valueOf(result));
}//endofswitch(firstsymbol)
prev=true;
repeat=true;
return;
}
if(source==jbuttons[11]){
dot=true;
if(repeat){
firstsymbol='-';
return;
}
second=Double.parseDouble(dispresult.getText());
switch(secondsymbol){
case'*':
second*=first;
break;
case'/':
if(divide(second))return;
second=first/second;
}
secondsymbol='\0';
switch(firstsymbol){
case'\0':
result=second;
firstsymbol='-';
break;
case'+':
result+=second;
firstsymbol='-';
dispresult.setText(String.valueOf(result));
break;
case'-':
result-=second;
dispresult.setText(String.valueOf(result));
break;
case'*':
result*=second;
firstsymbol='-';
dispresult.setText(String.valueOf(result));
break;
case'/':
if(divide(second))return;
result/=second;
firstsymbol='-';
dispresult.setText(String.valueOf(result));
}
prev=true;
repeat=true;
return;
}
if(source==jbuttons[17]){
dot=true;
if(repeat){
if(secondsymbol=='\0')firstsymbol='*';
elsesecondsymbol='*';
return;
}
second=Double.parseDouble(dispresult.getText());
switch(secondsymbol){
case'\0':
switch(firstsymbol){
case'\0':
firstsymbol='*';
result=second;
break;
case'+':
case'-':
first=second;
secondsymbol='*';
break;
case'*':
result*=second;
dispresult.setText(String.valueOf(result));
break;
case'/':
if(divide(second))return;
result/=second;
dispresult.setText(String.valueOf(result));
firstsymbol='*';
}
break;
case'*':
first*=second;
dispresult.setText(String.valueOf(first));
break;
case'/':
if(divide(second))return;
first/=second;
secondsymbol='*';
dispresult.setText(String.valueOf(first));
}
prev=true;
repeat=true;
return;
}
if(source==jbuttons[23]){
dot=true;
if(repeat){
if(secondsymbol=='\0')firstsymbol='/';
elsesecondsymbol='/';
return;
}
second=Double.parseDouble(dispresult.getText());
switch(secondsymbol){
case'\0':
switch(firstsymbol){
case'\0':
firstsymbol='/';
result=second;
break;
case'+':
case'-':
first=second;
secondsymbol='/';
break;
case'*':
result*=second;
firstsymbol='/';
dispresult.setText(String.valueOf(result));
break;
case'/':
if(divide(second))return;
result/=second;
dispresult.setText(String.valueOf(result));
}//end
break;
case'*':
first*=second;
secondsymbol='/';
dispresult.setText(String.valueOf(first));
break;
case'/':
if(divide(second))return;
result/=second;
dispresult.setText(String.valueOf(first));
}//end
prev=true;
repeat=true;
return;
}
}
publicstaticvoidmain(String[]args){
My
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 简单 计算器 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)