This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplex.cpp
More file actions
154 lines (125 loc) · 3.77 KB
/
Copy pathcomplex.cpp
File metadata and controls
154 lines (125 loc) · 3.77 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// complex.cpp
// Mandelbrot
//
// Created by Benjamin Mayes on 10/12/11.
// Copyright 2011 RIT. All rights reserved.
//
#include "complex.h"
void test(void);
complex::complex() : _x(0.0), _y(0.0) {}
complex::complex(const double& x, const double& y) : _x(x), _y(y) {}
complex::complex(const double& x) : _x(x), _y(0.0) {}
complex::complex(const complex& c) : _x(c._x), _y(c._y) {}
complex::~complex() {}
double complex::real() const {
return _x;
}
double complex::imag() const {
return _y;
}
void complex::set_real( const double& r ) {
_x = r;
}
void complex::set_imag( const double& i ) {
_y = i;
}
complex complex::operator*(const complex &rhs) const {
double real = _x * rhs._x - _y * rhs._y;
double imag = _x * rhs._y + _y * rhs._x;
return complex(real,imag);
}
complex complex::operator/(const complex &rhs) const {
//rhs is the denominator, find it's conjugate
complex conj(rhs._x,-rhs._y);
complex top = *this * conj;
double bottom = rhs._x * rhs._x + rhs._y * rhs._y;
top._x /= bottom;
top._y /= bottom;
return top;
}
complex complex::operator+(const complex &rhs) const {
return complex(_x+rhs._x,_y+rhs._y);
}
complex complex::operator-(const complex &rhs) const {
return complex(_x-rhs._x,_y-rhs._y);
}
complex& complex::operator=(const complex& rhs) {
if( this != &rhs ) {
_x = rhs._x;
_y = rhs._y;
}
return *this;
}
complex& complex::operator+=(const complex& rhs) {
_x += rhs._x;
_y += rhs._y;
return *this;
}
complex& complex::operator-=(const complex& rhs) {
_x -= rhs._x;
_y -= rhs._y;
return *this;
}
complex& complex::operator*=(const complex& rhs) {
double real = _x * rhs._x - _y * rhs._y;
_y = _x * rhs._y + _y * rhs._x;
_x = real;
return *this;
}
complex& complex::operator/=(const complex &rhs) {
//rhs is the denominator, find it's conjugate
complex conj(rhs._x,-rhs._y);
complex top = *this * conj;
double bottom = rhs._x * rhs._x + rhs._y * rhs._y;
top._x /= bottom;
top._y /= bottom;
_x = top._x;
_y = top._y;
return *this;
}
complex complex::operator-() const {
return complex(-_x,-_y);
}
int complex::operator<(const complex &rhs) const {
return (_x < rhs._x) || (_x == rhs._x && _y < rhs._y);
}
int complex:: operator>(const complex &rhs) const {
return rhs < *this;
}
int complex::operator<=(const complex &rhs) const {
return (_x <= rhs._x) || (_x == rhs._x && _y <= rhs._y);
}
int complex:: operator>=(const complex &rhs) const {
return rhs <= *this;
}
int complex::operator==(const complex &rhs) const {
return (_x == rhs._x && _y == rhs._y);
}
int complex::operator!=(const complex &rhs) const {
return (_x != rhs._x || _y != rhs._y);
}
std::ostream& operator<<( std::ostream& out, const complex& z ) {
out << "(" << z._x << "+" << z._y << "i)";
return out;
}
void test(void) {
complex c1(3.0,4.0), c2(1.0,-1.0);
std::cout << c1 << "*" << c2 << "=" << c1*c2 << std::endl;
std::cout << c1 << "+" << c2 << "=" << c1+c2 << std::endl;
std::cout << c1 << "-" << c2 << "=" << c1-c2 << std::endl;
std::cout << c1 << "/" << c2 << "=" << c1/c2 << std::endl;
std::cout << complex(2,6)/complex(4,1) << std::endl;
std::cout << complex(3,4)/complex(5,-1) << std::endl;
std::cout << "-" << c2 << "=" << -c2 << std::endl;
std::cout << (c1 = c1 * c1 + c2) << std::endl;
std::cout << (c1 = c1 * c1 + c2) << std::endl;
std::cout << (c1 = c1 * c1 + c2) << std::endl;
complex c(-0.35,-1.95);
complex z(c);
complex z2 = c;
std::cout << c << " " << z << std::endl;
//std::cout << (z = z*z + c ) << std::endl;
std::cout << (z = z*z) << " " << (z += c) << std::endl;
std::cout << (z2 *= z2) << " " << (z2 += c) << std::endl;
}