📄 ch5_e5_13c.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ch5_e5_13c extends Applet implements ActionListener
{
final int NUMBER = 4;
ComplexInputPanel inputComplexPnl[][];
ComplexOutputPanel outputComplexPnl[];
Button caculateBtn[];
final String COMMAND_NAME[] = {"ADD","MINUS","MULTIPLE","DIVIDE"};
public ch5_e5_13c()
{
super();
//初始化
inputComplexPnl = new ComplexInputPanel[NUMBER][2];
outputComplexPnl = new ComplexOutputPanel[NUMBER];
caculateBtn = new Button[NUMBER];
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j] = new ComplexInputPanel();
outputComplexPnl[i] = new ComplexOutputPanel();
caculateBtn[i] = new Button("=");
caculateBtn[i].addActionListener(this);
caculateBtn[i].setActionCommand(COMMAND_NAME[i]);
}
//加入各图形界面成分
for(int i=0; i<NUMBER; i++)
{
//第一个操作数的输入界面成分
add(inputComplexPnl[i][0]);
//操作符
switch(i)
{
case 0: add(new Label("+"));
break;
case 1: add(new Label("-"));
break;
case 2: add(new Label("*"));
break;
case 3: add(new Label("/"));
break;
}
//第二个操作数的输入界面成分
add(inputComplexPnl[i][1]);
//启动运算的按钮
add(caculateBtn[i]);
//运算结果的输出界面成分
add(outputComplexPnl[i]);
}//for
}
public void init()
{
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j].resetFaces();
outputComplexPnl[i].resetFaces();
}
}//init
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getActionCommand().equals(COMMAND_NAME[0]))
{
inputComplexPnl[0][0].getComplexNumber();
outputComplexPnl[0].setComplexNumber(
inputComplexPnl[0][0].complexAdd(
inputComplexPnl[0][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[1]))
{
inputComplexPnl[1][0].getComplexNumber();
outputComplexPnl[1].setComplexNumber(
inputComplexPnl[1][0].complexMinus(
inputComplexPnl[1][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[2]))
{
inputComplexPnl[2][0].getComplexNumber();
outputComplexPnl[2].setComplexNumber(
inputComplexPnl[2][0].complexMulti(
inputComplexPnl[2][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[3]))
{
inputComplexPnl[3][0].getComplexNumber();
outputComplexPnl[3].setComplexNumber(
inputComplexPnl[3][0].complexDivide(
inputComplexPnl[3][1].getComplexNumber()));
}
else
{
showStatus("未定义这种事件的处理操作" + e.toString());
return;
}
}
catch(NumberFormatException nfe)
{
showStatus("数据格式错误,请重新输入。");
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j].resetFaces();
outputComplexPnl[i].resetFaces();
}
}
}
}
interface ComplexCaculationsInterface
{
public ComplexNumber complexAdd(ComplexNumber c);
public ComplexNumber complexAdd(double d);
public ComplexNumber complexMinus(ComplexNumber c);
public ComplexNumber complexMinus(double d);
public ComplexNumber complexMulti(ComplexNumber c);
public ComplexNumber complexMulti(double d);
public ComplexNumber complexDivide(double d);
public ComplexNumber complexDivide(ComplexNumber c);
}
abstract class ComplexPanel extends Panel implements ComplexCaculationsInterface
{
ComplexNumber innerComplex;
Label prefixLabel,midLabel,suffixLabel;
ComplexPanel()
{
super();
innerComplex = new ComplexNumber();
prefixLabel = new Label("(");
midLabel = new Label("+");
suffixLabel = new Label("i)");
createAddFaces();
}
abstract void createAddFaces();
abstract ComplexNumber getComplexNumber()
throws NumberFormatException;
abstract void setComplexNumber(ComplexNumber c);
abstract void resetFaces();
public ComplexNumber complexAdd(ComplexNumber c)
{
return innerComplex.complexAdd(c);
}
public ComplexNumber complexAdd(double d)
{
return innerComplex.complexAdd(d);
}
public ComplexNumber complexMinus(ComplexNumber c)
{
return innerComplex.complexMinus(c);
}
public ComplexNumber complexMinus(double d)
{
return innerComplex.complexMinus(d);
}
public ComplexNumber complexMulti(ComplexNumber c)
{
return innerComplex.complexMulti(c);
}
public ComplexNumber complexMulti(double d)
{
return innerComplex.complexMulti(d);
}
public ComplexNumber complexDivide(double d)
{
return innerComplex.complexDivide(d);
}
public ComplexNumber complexDivide(ComplexNumber c)
{
return innerComplex.complexDivide(c);
}
}
class ComplexInputPanel extends ComplexPanel
{
private TextField realTfd,imaginaryTfd;
void createAddFaces()
{
realTfd = new TextField(3);
imaginaryTfd = new TextField(3);
add(prefixLabel);
add(realTfd);
add(midLabel);
add(imaginaryTfd);
add(suffixLabel);
}
ComplexNumber getComplexNumber() throws NumberFormatException
{
innerComplex = new ComplexNumber(
Double.parseDouble(realTfd.getText()),
Double.parseDouble(
(imaginaryTfd.getText().equals("")) ? "0" :
imaginaryTfd.getText() )
);
return innerComplex;
}
void setComplexNumber(ComplexNumber c)
{
double r,i;
r = c.getRealPart();
i = c.getImaginaryPart();
innerComplex.setRealPart(r);
innerComplex.setImaginaryPart(i);
realTfd.setText(Double.toString(r));
imaginaryTfd.setText(Double.toString(i));
}
void resetFaces()
{
realTfd.setText("");
imaginaryTfd.setText("");
}
}
class ComplexOutputPanel extends ComplexPanel
{
private Label realLbl,imaginaryLbl;
void createAddFaces()
{
realLbl = new Label(" ");
imaginaryLbl = new Label(" ");
add(prefixLabel);
add(realLbl);
add(midLabel);
add(imaginaryLbl);
add(suffixLabel);
}
ComplexNumber getComplexNumber() throws NumberFormatException
{
innerComplex = new ComplexNumber(
Double.parseDouble(realLbl.getText()),
Double.parseDouble(
(imaginaryLbl.getText().equals("")) ? "0" :
imaginaryLbl.getText() )
);
return innerComplex;
}
void setComplexNumber(ComplexNumber c)
{
double r,i;
r = c.getRealPart();
i = c.getImaginaryPart();
innerComplex.setRealPart(r);
innerComplex.setImaginaryPart(i);
realLbl.setText(Double.toString(r));
imaginaryLbl.setText(Double.toString(i));
}
void resetFaces()
{
realLbl.setText(" ");
imaginaryLbl.setText(" ");
}
}
class ComplexNumber implements ComplexCaculationsInterface
{
//域
private double m_dRealPart;
private double m_dImaginPart;
//构造函数
ComplexNumber()
{
m_dRealPart = 0.0;
m_dImaginPart = 0.0;
}
ComplexNumber(double r,double i)
{
m_dRealPart = r;
m_dImaginPart = i;
}
ComplexNumber(ComplexNumber c)
{
m_dRealPart = c.getRealPart();
m_dImaginPart = c.getImaginaryPart();
}
//get,set方法
double getRealPart()
{
return m_dRealPart;
}
double getImaginaryPart()
{
return m_dImaginPart;
}
void setRealPart(double d)
{
m_dRealPart = d;
}
void setImaginaryPart(double d)
{
m_dImaginPart = d;
}
//复数的共轭
ComplexNumber complexConjugate()
{
return new ComplexNumber(m_dRealPart,
-1 * m_dImaginPart);
}
//复数运算方法
public ComplexNumber complexAdd(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart + c.getRealPart(),
this.m_dImaginPart + c.getImaginaryPart());
}
public ComplexNumber complexAdd(double c)
{
return new ComplexNumber(
this.m_dRealPart + c, this.m_dImaginPart);
}
public ComplexNumber complexMinus(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart - c.getRealPart(),
this.m_dImaginPart - c.getImaginaryPart());
}
public ComplexNumber complexMinus(double c)
{
return new ComplexNumber(
this.m_dRealPart - c, this.m_dImaginPart);
}
public ComplexNumber complexMulti(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart * c.getRealPart()
- this.m_dImaginPart * c.getImaginaryPart(),
this.m_dRealPart * c.getImaginaryPart()
+ this.m_dImaginPart * c.getRealPart());
}
public ComplexNumber complexMulti(double c)
{
return new ComplexNumber(
this.m_dRealPart * c, this.m_dImaginPart * c);
}
public ComplexNumber complexDivide(double d)
{
return new ComplexNumber(this.m_dRealPart / d,
this.m_dImaginPart /d);
}
public ComplexNumber complexDivide(ComplexNumber c)
{
double mod = c.getRealPart()*c.getRealPart()
+ c.getImaginaryPart()*c.getImaginaryPart();
if(mod == 0)
return new ComplexNumber(0,0);
else
return this.complexMulti(
c.complexConjugate()).complexDivide(mod);
}
//toString()
public String toString()
{
return "(" + m_dRealPart + " + "
+ m_dImaginPart + " i" + ")";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -