数据结构课第一次实验

实现复数运算

复习知识点: 运算符重载

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//complex number.hpp

#ifndef complex_number_hpp
#define complex_number_hpp

#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>


template <typename ElemType>
class ComplexNumber {
private:
ElemType x;
ElemType y;
public:
// ComplexNumber() = default;
ComplexNumber(ElemType _x = 0,ElemType _y = 0);
ComplexNumber(ComplexNumber&);


ComplexNumber<ElemType>& operator+(ComplexNumber&);
ComplexNumber<ElemType>& operator+(ElemType x);
ComplexNumber<ElemType>& operator-(ComplexNumber&);
ComplexNumber<ElemType>& operator-(ElemType x);
ComplexNumber<ElemType>& operator*(ComplexNumber&);
ComplexNumber<ElemType>& operator*(ElemType x);
ComplexNumber<ElemType>& operator/(ComplexNumber&);
ComplexNumber<ElemType>& operator/(ElemType x);
ComplexNumber<ElemType>& operator+=(ComplexNumber&);
ComplexNumber<ElemType>& operator-=(ComplexNumber&);
ComplexNumber<ElemType>& operator=(ComplexNumber&);

ComplexNumber<ElemType> create(ElemType _x,ElemType _y) {
ComplexNumber c(_x,_y);
return c;
}
// ComplexNumber<ElemType> create(ElemType& cn) {
// return ComplexNumber(cn);
// }

friend std::ostream& operator<<(std::ostream& out,ComplexNumber<ElemType>& cn) {
if (!cn.x && !cn.y) {
out << "0";
return out;
} else if (!cn.x) {
out << cn.y << "i";
return out;
} else if (!cn.y) {
out << cn.x;
return out;
}
out << cn.x << "+" << cn.y << "i";
return out;
}
friend std::istream& operator>>(std::istream& in, ComplexNumber<ElemType>& cn) {
in >> cn.x >> cn.y;
return in;
}



};


#endif /* complex_number_hpp */

//complex number.cpp

//constructor

template <typename ElemType>
ComplexNumber<ElemType>::ComplexNumber(ElemType _x,ElemType _y) {
x = _x;
y = _y;
}


template <typename ElemType>
ComplexNumber<ElemType>::ComplexNumber(ComplexNumber& cn) {
this->x = cn.x;
this->y = cn.y;
}


//overload operator

template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator+(ComplexNumber& cn) {
ComplexNumber c;
c.x = cn.x + this->x;
c.y = cn.y + this->y;
return c;
}
template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator+(ElemType x) {
ComplexNumber c;
c.x = this->x + x;
c.y = this->y;
return c;
}


template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator-(ComplexNumber& cn) {
ComplexNumber c;
c.x = this->x - cn.x;
c.y = this->y - cn.y;
return c;
}
template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator-(ElemType x) {
ComplexNumber c;
c.x = this->x - x;
c.y = this->y;
return c;
}


template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator*(ComplexNumber& cn) {
ComplexNumber<ElemType> c(this->x * cn.x - this->y * cn.y,0);
c.x = this->x * cn.x - this->y * cn.y;
c.y = 0;
return c;
}
template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator*(ElemType x) {
ComplexNumber<ElemType> c;
c.x = this->x * x;
c.y = this->y;
return c;
}


template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator/(ComplexNumber& cn) {
ComplexNumber<ElemType> c(this->x * 1.0 / cn.x,this->y * 1.0 / cn.y);
return c;
}




template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator=(ComplexNumber& cn) {
this->x = cn.x;
this->y = cn.y;
return *this;
}
template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator+=(ComplexNumber& cn) {
this->x += cn.x;
this->y += cn.y;
return *this;
}
template <typename ElemType>
ComplexNumber<ElemType>& ComplexNumber<ElemType>::operator-=(ComplexNumber& cn) {
this->x -= cn.x;
this->y -= cn.y;
return *this;
}
script>