-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.cpp
More file actions
123 lines (103 loc) · 2.86 KB
/
ComplexNumber.cpp
File metadata and controls
123 lines (103 loc) · 2.86 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
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <math.h>
#include "ComplexNumber.h"
#include <iostream>
/*
Author: Gene Silva
Class: CSC220 Summer 2015
Project 2: Complex Number Class
*/
using namespace std;
ComplexNumber ComplexNumber::operator+ (const ComplexNumber &cn)
{
ComplexNumber ret(this->getreal() + cn.getreal(), this->getimag() + cn.getimag());
return ret;
}
ComplexNumber ComplexNumber::operator- (const ComplexNumber &cn)
{
ComplexNumber ret(this->getreal() - cn.getreal(), this->getimag() - cn.getimag());
return ret;
}
ComplexNumber ComplexNumber::operator* (const ComplexNumber &cn)
{
ComplexNumber ret(this->getreal() * cn.getreal() - (this->getimag() * cn.getimag()),
this->getreal() * cn.getimag() + (this->getimag() * cn.getreal()));
return ret;
}
ComplexNumber ComplexNumber::operator/ (const ComplexNumber &cn)
{
float x1, y1, x2, y2, r, r1, r2, theta, theta1, theta2, a, b;
x1 = this->getreal();
y1 = this->getimag();
x2 = cn.getreal();
y2 = cn.getimag();
r1 = sqrt(pow(x1, 2) + pow(y1, 2));
r2 = sqrt(pow(x2, 2) + pow(y2, 2));
if(x1 > 0)
theta1 = atanf(y1/x1);
else if( x1 < 0 && y1 >= 0)
theta1 = atanf(y1/x1) + 3.14;
else if(x1 < 0 && y1 < 0)
theta1 = atanf(y1/x1) - 3.14;
else if(x1 == 0 && y1 > 0)
theta1 = 3.14/2;
else if(x1 == 0 && y1 < 0)
theta1 = -3.14/2;
if(x2 > 0)
theta2 = atanf(y2/x2);
else if(x2 < 0 && y2 >= 0)
theta2 = atanf(y2/x2) + 3.14;
else if(x2 < 0 && y2 < 0)
theta2 = atanf(y2/x2) - 3.14;
else if(x2 == 0 && y2 > 0)
theta2 = 3.14/2;
else if(x2 == 0 && y2 < 0)
theta2 = -3.14/2;
r = r1 / r2;
theta = theta1 - theta2;
a = r*cos(theta);
b = r*sin(theta);
ComplexNumber ret(a, b);
return ret;
}
std::ostream& operator<< (std::ostream &strm, const ComplexNumber &cn)
{
strm << cn.getreal() << " + " << cn.getimag() << "i" << endl;
return strm;
}
ComplexNumber ComplexNumber::operator*(float f)
{
return ComplexNumber(this->getreal() * f, this->getimag() * f);
}
void ComplexNumber::print()
{
if(imag<0)
cout << real << imag << "i" << endl;
else
cout << real << " + " << imag << "i" << endl;
}
void ComplexNumber::demo()
{
cout << "----------Complex Number Class by Gene Silva----------" << endl;
cout << "My two complex numbers:" << endl;
ComplexNumber cn1(-12, 3);
ComplexNumber cn2(4, 5);
cn1.print();
cn2.print();
ComplexNumber sum = cn1 + cn2;
ComplexNumber sub = cn1 - cn2;
ComplexNumber mult = cn1*cn2;
ComplexNumber div = cn1/cn2;
cout << "Adding the two complex numbers: ";
sum.print();
cout << "Subtracting the two complex numbers: ";
sub.print();
cout << "Multiplying the two complex numbers: ";
mult.print();
cout << "Dividing the two complex numbers: ";
div.print();
cout << "Multiplying first complex number by a scalar (-5.3): ";
ComplexNumber scal = cn1 * -5.3;
cout << scal << endl;
cout << "----------End of Complex Number Class by Gene Silva----------" << endl;
}