Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102856470
ComplexNumber.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 24, 22:01
Size
3 KB
Mime Type
text/x-c
Expires
Wed, Feb 26, 22:01 (2 d)
Engine
blob
Format
Raw Data
Handle
24433597
Attached To
R1106 Programming Concept Rouaze
ComplexNumber.cpp
View Options
#include "ComplexNumber.hpp"
#include <cmath>
int
main
(
int
argc
,
char
*
argv
[])
{
ComplexNumber
z1
(
4.0
,
3.0
);
std
::
cout
<<
"z1 = "
<<
z1
<<
"
\n
"
;
std
::
cout
<<
"Modulus z1 = "
<<
z1
.
CalculateModulus
()
<<
"
\n
"
;
std
::
cout
<<
"Argument z1 = "
<<
z1
.
CalculateArgument
()
<<
"
\n
"
;
return
0
;
}
// Override default constructor
// Set real and imaginary parts to zero
ComplexNumber
::
ComplexNumber
()
{
mRealPart
=
0.0
;
mImaginaryPart
=
0.0
;
}
// Constructor that sets complex number z=x+iy
ComplexNumber
::
ComplexNumber
(
double
x
,
double
y
)
{
mRealPart
=
x
;
mImaginaryPart
=
y
;
}
// Constructor that sets real double number into im form nuber
ComplexNumber
::
ComplexNumber
(
double
x
)
{
mRealPart
=
x
;
mImaginaryPart
=
0.0
;
}
// Method for acces to the corresponding real part of z
double
ComplexNumber
::
GetRealPart
()
{
return
mRealPart
;
}
// Method for acces to the corresponding real part of z
double
ComplexNumber
::
GetImaginaryPart
()
{
return
mImaginaryPart
;
}
// Method for computing the conjugate of a complex number
ComplexNumber
ComplexNumber
::
CalculateConjugate
()
const
{
ComplexNumber
z
(
mRealPart
,
-
mImaginaryPart
);
return
z
;
}
/*
// Method for computing the conjugate of a complex number
void ComplexNumber::setConjugate(ComplexNumber& z);
{
ComplexNumber conjugate = CalculateConjugate();
return z;
}
*/
// Method for computing the modulus of a
// complex number
double
ComplexNumber
::
CalculateModulus
()
const
{
return
std
::
sqrt
(
mRealPart
*
mRealPart
+
mImaginaryPart
*
mImaginaryPart
);
}
// Method for computing the argument of a
// complex number
double
ComplexNumber
::
CalculateArgument
()
const
{
return
atan2
(
mImaginaryPart
,
mRealPart
);
}
// Method for raising complex number to the power n
// using De Moivre's theorem - first complex
// number must be converted to polar form
ComplexNumber
ComplexNumber
::
CalculatePower
(
double
n
)
const
{
double
modulus
=
CalculateModulus
();
double
argument
=
CalculateArgument
();
double
mod_of_result
=
pow
(
modulus
,
n
);
double
arg_of_result
=
argument
*
n
;
double
real_part
=
mod_of_result
*
cos
(
arg_of_result
);
double
imag_part
=
mod_of_result
*
sin
(
arg_of_result
);
ComplexNumber
z
(
real_part
,
imag_part
);
return
z
;
}
double
**
allocateMatrix
(
int
m
,
int
n
)
{
double
**
mat
=
new
double
*
[
m
];
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
mat
[
i
]
=
new
double
[
n
];
}
return
mat
;
}
void
deallocateMatrix
(
double
**
mat
,
int
m
,
int
n
)
{
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
delete
[]
mat
[
i
];
}
delete
[]
mat
;
}
// Overloading the = (assignment) operator
ComplexNumber
&
ComplexNumber
::
operator
=
(
const
ComplexNumber
&
z
)
{
mRealPart
=
z
.
mRealPart
;
mImaginaryPart
=
z
.
mImaginaryPart
;
return
*
this
;
}
// Overloading the unary - operator
ComplexNumber
ComplexNumber
::
operator
-
()
const
{
ComplexNumber
w
;
w
.
mRealPart
=
-
mRealPart
;
w
.
mImaginaryPart
=
-
mImaginaryPart
;
return
w
;
}
// Overloading the binary + operator
ComplexNumber
ComplexNumber
::
operator
+
(
const
ComplexNumber
&
z
)
const
{
ComplexNumber
w
;
w
.
mRealPart
=
mRealPart
+
z
.
mRealPart
;
w
.
mImaginaryPart
=
mImaginaryPart
+
z
.
mImaginaryPart
;
return
w
;
}
// Overloading the binary - operator
ComplexNumber
ComplexNumber
::
operator
-
(
const
ComplexNumber
&
z
)
const
{
ComplexNumber
w
;
w
.
mRealPart
=
mRealPart
-
z
.
mRealPart
;
w
.
mImaginaryPart
=
mImaginaryPart
-
z
.
mImaginaryPart
;
return
w
;
}
// Overloading the insertion << operator
std
::
ostream
&
operator
<<
(
std
::
ostream
&
output
,
const
ComplexNumber
&
z
)
{
// Format as "(a + bi)" or as "(a - bi)"
output
<<
"("
<<
z
.
mRealPart
<<
" "
;
if
(
z
.
mImaginaryPart
>=
0.0
)
{
output
<<
"+ "
<<
z
.
mImaginaryPart
<<
"i)"
;
}
else
{
// z.mImaginaryPart < 0.0
// Replace + with minus sign
output
<<
"- "
<<
-
z
.
mImaginaryPart
<<
"i)"
;
}
}
//Code from Chapter06.tex line 779 save as ComplexNumber.cpp
Event Timeline
Log In to Comment