-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.cpp
More file actions
114 lines (94 loc) · 3.25 KB
/
complex.cpp
File metadata and controls
114 lines (94 loc) · 3.25 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
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
/*
author Alessandro Ferrante
* complex.cpp
*/
? to manage the algebra of complex numbers;
/*
? The example shows the redefinition of the operators + - \ *
? to manage the algebra of complex numbers;
? If I define an operator inside a class it becomes a method
*/
#include <iostream>
using namespace std;
class Complex{
private:
// attributes
float real, imaginary;
public:
// constructor
Complex(float r, float i){
real = r;
imaginary = i;
}
// default constructor
Complex() : Complex(0 ,0){}
// copy constructor
Complex(const Complex & c) : Complex(c.real, c.imaginary){ }
friend ostream& operator <<(ostream& output_stream, Complex & c){
output_stream << c.real;
if(c.imaginary < 0)
output_stream << c.imaginary;
else
output_stream << "+" << c.imaginary;
output_stream << "i";
return output_stream;
}
Complex operator+(Complex & rhs); //? sum between complex numbers
Complex operator+(double rhs); //? sum of complex numbers and reals
Complex operator-(Complex & rhs); // ? subtraction between complex numbers
Complex operator-(double rhs); // ? subtraction between complex numbers and reals
Complex operator*(Complex & rhs); // ? multiplication between complex numbers
Complex operator/(Complex & rhs);// ? division between complex numbers
};
Complex Complex::operator+(Complex & rhs){
// ? Initializes the complex number complex object
//* result.real = this->real + rhs.real;
//* resutl.imaginary = this->imaginary + rhs.imaginary;
// ? calls the constructor and initializes the complex number object
Complex result(real + rhs.real, imaginary + rhs.imaginary);
return result;
}
Complex Complex::operator+(double rhs){
// ? Initializes the complex number complex object
Complex result(real + rhs, imaginary);
return result;
}
Complex Complex::operator-(Complex & rhs){
// ? Initializes the complex number complex object
//* result.real = this->real - rhs.real;
//* resutl.imaginary = this->imaginary - rhs.imaginary;
// ? calls the constructor and initializes the complex number object
Complex result(real - rhs.real, imaginary - rhs.imaginary);
return result;
}
Complex Complex::operator-(double rhs){
// ? Initializes the complex number complex object
Complex result(real - rhs, imaginary);
return result;
}
Complex Complex::operator*(Complex & rhs){
Complex result(real*rhs.real - imaginary*rhs.imaginary, real*rhs.imaginary + imaginary*rhs.real);
return result;
}
Complex Complex::operator/(Complex & rhs){
float den = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary;
Complex result( (real*rhs.real + imaginary*rhs.imaginary) / den,(imaginary*rhs.real - real*rhs.imaginary) / den );
return result;
}
int main (int agrc, char ** argv){
Complex a, b(1,2), c(3,4);
cout << a << ", " << b << ", " << c << endl;
Complex d = b + c;
// Complex d = b.operator+(c);
cout << d << endl;
d = b - c;
cout << d << endl;
d = c + 3;
cout << d << endl;
d = c - 3.5;
cout << d << endl;
d = b * c;
cout << d << endl;
d = b / c;
cout << d << endl;
}