This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplexNumber.cpp
More file actions
110 lines (94 loc) · 2.84 KB
/
Copy pathcomplexNumber.cpp
File metadata and controls
110 lines (94 loc) · 2.84 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
/*----------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-1250 Section F01 |
| Instructor: Dr. Afsaneh Minaie |
| |
| File: complexNumber.cpp |
| |
| Purpose: ComplexNumber implementation |
+----------------------------------------------------------------------------*/
#include <iostream>
#include <math.h>
using namespace std;
#include "complexNumber.h"
/**
* Purpose: Default constructor - to initialize data members
*
* Parameters: none
* Pre-conditions: none
* Post-conditions: none
* Returns : none
*/
ComplexNumber::ComplexNumber()
{
itsReal = 0.0;
itsImag = 0.0;
}
/**
* Purpose: Overloaded constructor
*
* Parameters: real the real part of the complex number
* imag the imaginary part of the complex number
*
* Pre-conditions: none
* Post-conditions: none
* Returns : none
*/
ComplexNumber::ComplexNumber( double real, double imag )
{
itsReal = real;
itsImag = imag;
}
/**
* Purpose: Add two complex numbers together
*
* Parameters: theCN a ComplexNumber object that will be used
* during the addition operation
*
* Pre-conditions: none
* Post-conditions: none
*
* Returns : a newly created ComplexNumber object that is the sum of the
* invoking object plus the object passed
*/
ComplexNumber ComplexNumber::Add( ComplexNumber theCN )
{
ComplexNumber tmpCN;
tmpCN.itsReal = itsReal + theCN.itsReal;
tmpCN.itsImag = itsImag + theCN.itsImag;
return tmpCN;
}
/**
* Purpose: Subtract two complex numbers
*
* Parameters: theCN a ComplexNumber object that will be used
* during the subtraction operation
*
* Pre-conditions: none
* Post-conditions: none
*
* Returns : a newly created ComplexNumber object that is the difference
* between the invoking object and the object passed
*/
ComplexNumber ComplexNumber::Sub( ComplexNumber theCN )
{
ComplexNumber tmpCN;
tmpCN.itsReal = itsReal - theCN.itsReal;
tmpCN.itsImag = itsImag - theCN.itsImag;
return tmpCN;
}
/**
* Purpose: Print the complex number using cout
*
* Parameters: none
* Pre-conditions: none
* Post-conditions: none
* Returns : none
*/
void ComplexNumber::Print()
{
if( itsImag >= 0 )
cout << itsReal << " + " << fabs( itsImag ) << "i";
else
cout << itsReal << " - " << fabs( itsImag ) << "i";
}