-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNum.java
70 lines (58 loc) · 1.24 KB
/
ComplexNum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class ComplexNum {
// Z = a + bi
private int Rez; // 实部
private int Imz; // 虚部
public int getRez() {
return Rez;
}
public void setRez(int rez) {
Rez = rez;
}
public int getImz() {
return Imz;
}
public void setImz(int imz) {
Imz = imz;
}
public ComplexNum(){}
// 构造函数
public ComplexNum(int rez, int imz) {
super();
Rez = rez;
Imz = imz;
}
// 加
public static void plus(ComplexNum a,ComplexNum b){
ComplexNum temp = new ComplexNum();
temp.setRez(a.getRez()+b.getRez());
temp.setImz(a.getImz()+b.getImz());
display(temp);
}
// 减
public static void minus(ComplexNum a,ComplexNum b){
ComplexNum temp = new ComplexNum();
temp.setRez(a.getRez()-b.getRez());
temp.setImz(a.getImz()-b.getImz());
display(temp);
}
// 显示
public static void display(ComplexNum a){
StringBuffer sb = new StringBuffer();
sb.append(a.getRez());
if(a.getImz()>0){
sb.append("+"+a.getImz()+"i");
}else if(a.getImz()<0){
sb.append(a.getImz()+"i");
}
System.out.println(sb.toString());
}
public static void main(String[] args) {
ComplexNum a = new ComplexNum(4, 3); //构造方法1
ComplexNum b = new ComplexNum(); // 构造方法2
b.setRez(5);
b.setImz(3);
plus(a, b); //加
minus(a, b); //减
display(a);//显示
}
}