实验报告 编写一个简易实用的计算器。
开课实验室:现代信息交流中心40*开课时间:2012年**月**日实验报告:年月日
一、实验目的:熟悉Java图形用户界面的开发原理与技术,包括各种组件和容器技术。
二、实验内容:
编写一个简易实用的计算器。
三、源代码清单:
package exp3;
import Result extends Panel {
TextField tf;
Result() {
tf = new TextField("0", 25);
add(tf);
}
}
class Key extends Panel {
Button backSpace, clearEnter, cancel,lBracket,rBracket;
Button one, two, three, four, five, six, seven, eight, nine, zero;
Button add, subtract, multiplay, divide;
Button sqrt, reciprocal, mod;
Button positiveAndNegative, point, equal;
Key() {
setLayout(new GridLayout(5, 5));
backSpace = new Button("←");
(new Color(255, 0, 0));
add(backSpace);
clearEnter = new Button("CE");
(new Color(255, 0, 0));
add(clearEnter);
cancel = new Button("C");
(new Color(255, 0, 0));
add(cancel);
lBracket =new Button("(");
add(lBracket);
rBracket =new Button(")");
add(rBracket);
seven = new Button("7");
add(seven);
eight = new Button("8");
add(eight);
nine = new Button("9");
add(nine);
divide = new Button("/");
add(divide);
sqrt = new Button("sqrt");
add(sqrt);
four = new Button("4");
add(four);
five = new Button("5");
add(five);
six = new Button("6");
add(six);
multiplay = new Button("*");
add(multiplay);
mod = new Button("%");
add(mod);
one = new Button("1");
add(one);
two = new Button("2");
add(two);
three = new Button("3");
add(three);
subtract = new Button("-");
add(subtract);
reciprocal = new Button("1/x");
add(reciprocal);
zero = new Button("0");
add(zero);
positiveAndNegative = new Button("+/-");
add(positiveAndNegative);
point = new Button(".");
add(point);
add = new Button("+");
add(add);
equal = new Button("=");
add(equal);
}
}
public class Calculator extends Applet implements ActionListener { Result result = new Result();
Key key = new Key();
boolean isInit = true;
String operation = "";
float theFirstNum = 0;
float theSecondNum = 0;
float f = 0; == -1) {
+ ".");
isInit = false;
}
}
if () == {
theFirstNum = operation = "add";
isInit = true;
}
if () == {
theFirstNum = operation = "subtract";
isInit = true;
}
if () == {
theFirstNum = operation = "multiplay";
isInit = true;
}
if () == {
theFirstNum = operation = "divide";
isInit = true;
}
if () == {
theFirstNum = operation = "mod";
isInit = true;
}
if () == {
theFirstNum = isInit = true;
}
if () == {
if "0"))
"Error");
else {
theFirstNum = / theFirstNum));
isInit = true;
}
}
if () == {
if "0")) {
}
else {
if "-"))
else
"-" + }
}
if () == {
theSecondNum = if ("")) {
"0");
}
if ("add"))
+ theSecondNum));
if ("subtract"))
- theSecondNum));
if ("multiplay"))
* theSecondNum));
if ("divide")) {
if "0"))
"Error");
else
.valueOf(theFirstNum / theSecondNum));
}
if ("mod")) {
if "0"))
"Error");
else
.valueOf(theFirstNum % theSecondNum));
}
isInit = true;
theFirstNum = 0;
theSecondNum = 0;
operation = "";
}
if () == {
if "0")){
}else {
if == 1) {
"0");
isInit = true;
} else
- 1));
}
}
if () == {
"0");
isInit = true;
}
if () == {
"0");
theFirstNum = 0;
theSecondNum = 0;
operation = "";
isInit = true;
}
if () == {
f = theFirstNum; alueOf(theFirstNum / theSecondNum));
}
if ("mod")) {
if "0"))
"Error");
else
.valueOf(theFirstNum % theSecondNum));
}
theFirstNum = f; //把f再重新赋给第一个数
operation = s; // 把s再重新赋给运算符号
}
}
}
四、运行结果:(给出运行结果贴图)