-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.cpp
119 lines (94 loc) · 3.84 KB
/
complex.cpp
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
#include "complex.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
// print function to output results
void complex::print()
{
std::cout << "complex number: " << "(" << real << ", " << img << ")" << std::endl;
}
void complex::print(std::string frm)
{
if(frm == "std")
{
if(img < 0)
std::cout << "complex number(std form): " << real << img << "i" << std::endl;
else
std::cout << "complex number(std form): " << real << "+" << img << "i" << std::endl;
}
else if(frm == "polr")
{
if(arg < 0)
std::cout << "complex number(polr form): " << std::setprecision(2) << mod << "[cos" << "(" << arg * -1 << ") - isin(" << arg * -1 << ")]" << std::endl;
else
std::cout << "complex number(polr form): " << std::setprecision(2) << mod << "[cos" << "(" << arg << ") + isin(" << arg << ")]" << std::endl;
};
}
/* Getters */
double complex::get_real() { return real; } // Returns value of the real part
double complex::get_img() { return img; } // Returns value of the imaginary part
double complex::get_mod() { return mod; } // Returns value of the modulus
double complex::get_arg() { return arg; } // Returns value of the argument
/* Operator Overloaders */
//addition operator overloading
complex complex::operator +(complex c){
complex temp;
//real and img holds value of the expression to the left of the + operator
// c.real and c.img holds value of the expression to the rigth of the + operator
temp.real = real + c.real;
temp.img = img + c.img;
return temp;
}
// Conjugate operator: returns the conjugate of a complex number
complex complex::operator ~(){
complex temp;
temp.real = real;
temp.img = img * -1;
return temp;
}
// Negator
complex complex::operator -(){
complex temp;
temp.real = real * -1;
temp.img = img * -1;
return temp;
}
//Subtraction operator
complex complex::operator -(complex c){
complex temp;
//real and img holds value of the expression to the left of the - operator
// c.real and c.img holds value of the expression to the rigth of the - operator
temp.real = real - c.real;
temp.img = img - c.img;
return temp;
}
// Multiplication operator
complex complex::operator *(complex c){
complex temp;
//real and img holds value of the expression to the left of the * operator
// c.real and c.img holds value of the expression to the rigth of the * operator
// z = (a+bi) , w = (x+yi)
// z*w = (ax - by) + i(ay + bx)
temp.real = (real * c.real) - (img * c.img);
temp.img = (real * c.img) + (img * c.real);
return temp;
}
complex complex::operator /(complex c){
complex temp;
//real and img holds value of the expression to the left of the / operator
// c.real and c.img holds value of the expression to the rigth of the / operator
// z = (a+bi) , w = (x+yi)
// z/w = [ (ax + by)/(x^2 + y^2) ] + i[ (yb - xa)/(x^2 + y^2) ]
temp.real = ( (real * c.real) + (img * c.img) ) / ( (c.real * c.real) + (c.img * c.img) );
temp.img = ( (c.real * img) - (c.img * real) ) / ( (c.real * c.real) + (c.img * c.img) );
return temp;
}
bool complex::operator ==(complex c){
complex temp;
if (temp.real == c.real && temp.img == c.img)
{
return true;
}
return false;
};