一:需求分析
(1)由用户输入一个简单的四则运算表达式,求出其计算结果后显示。
(2)允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等。
二:本组课题及本人任务
本组课题
计算数学表达式的程序
本人任务
设置布局与功能键
三:总体设计(概要设计)
四:本人负责的主要功能展示与代码分析
class CalFrame extends Frame {//窗口
double d1, d2;
int op = 0;
TextField tf;
CalPanelL p1;
CalPanelR p2;
CalFrame(){
super("计算器");
setLayout(new FlowLayout(FlowLayout.CENTER,8,10));
setBackground(new Color(140,120,110));
setForeground(Color.white);
setResizable(false);
setSize(300,200);
setLocation(400,260);
tf = new TextField(28);
tf.setEditable(false);
tf.setBackground(Color.white);
tf.setForeground(Color.black);
tf.setFont(new Font("Arial",Font.BOLD,16));
add(tf);
p1 = new CalPanelL();
add(p1);
p2 = new CalPanelR();
add(p2);
setVisible(true);
addWindowListener(new Wclose());
}
class PressBAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 0;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBSub implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 1;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBMul implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 2;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBDiv implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 3;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBYx implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 4;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBEqual implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double result = 0;
d2 = Double.parseDouble(tf.getText());
switch(op) {
case 0:result = d1 + d2;break;
case 1:result = d1 - d2;break;
case 2:result = d1 * d2;break;
case 3:result = d1 / d2;break;
case 4:result = Math.pow(d1,d2);break;
default:
}
tf.setText(String.valueOf(result));
} catch(Exception ee) {}
}
}
class PressBSqrt implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.sqrt(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
class PressBInt implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.floor(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
class PressBSin implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.sin(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
class PressBTan implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.tan(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
class PressBCos implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.cos(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
class PressBRec implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double c= Double.parseDouble(tf.getText());
tf.setText(""+1/c);
} catch(Exception ee) {}
}
}
class PressBBack implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String text = tf.getText();
tf.setText(text.substring(0, text.length() - 1));
} catch(Exception ee) {}
}
}
class PressBCE implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf.setText("");
}
}
class PressBPi implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"3.1415926535898");
}
}
class PressBAbs implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.abs(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}
五:测试与改进
测试
99*9
5^5
sin1
9的倒数
5.964取整
-55的绝对值
9开平方
改进
1.界面设计一开始不美观,排序不当,经过多次修改终于美观有序。
2.功能键有时不能正确输出答案,经过多次修改,使各功能键完善。
3.显示器不能显示表达式,还有待于改进。
六:代码提交记录截图
七:团队课程设计博客链接
团队博客链接
八:课程设计感想
通过此次的课程设计实践,我不但进一步理解掌握了课上老师教授的知识,而且通过查找资料学会了课上没有的东西,进一步理解了面向对象的含义,
发现了很多它比面向过程突出的地方,第一次接触了图形界面,初步了解了GUI方面的知识。在实践中我遇到了很多问题,但我都努力克服,并最终成功
的完成了这次课程设计。