-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.h
More file actions
75 lines (48 loc) · 1.08 KB
/
Copy pathComplexNumber.h
File metadata and controls
75 lines (48 loc) · 1.08 KB
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
/*
* ComplexNumber.h
*
* Created on: Mar 16, 2019
* Author: Claire
*/
#ifndef COMPLEXNUMBER_H_
#define COMPLEXNUMBER_H_
#include <iostream>
struct ComplexNumber{
double a = 0;
double b = 0;
ComplexNumber& operator= (const ComplexNumber &c)
{
a = c.a;
b = c.b;
return *this;
}
inline friend std::ostream & operator << (std::ostream &os, const ComplexNumber &c){
os << c.a << " + " << c.b << "i" << std::endl;
return os;
}
};
inline ComplexNumber operator +(ComplexNumber x, ComplexNumber y){
ComplexNumber sum;
sum.a = x.a + y.a;
sum.b = x.b + y.b;
return sum;
}
inline ComplexNumber operator +(double x, ComplexNumber y){
ComplexNumber sum;
sum.a = x + y.a;
sum.b = y.b;
return sum;
}
inline ComplexNumber operator *(ComplexNumber x, ComplexNumber y){
ComplexNumber product;
product.a = x.a * y.a - x.b * y.b;
product.b = x.a * y.b + x.b * y.a;
return product;
}
inline ComplexNumber operator *(double x, ComplexNumber y){
ComplexNumber product;
product.a = x * y.a;
product.b = x * y.b;
return product;
}
#endif /* COMPLEXNUMBER_H_ */