-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS60 HW2.cpp
More file actions
152 lines (125 loc) · 8.01 KB
/
CS60 HW2.cpp
File metadata and controls
152 lines (125 loc) · 8.01 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
// CS60 HW2.cpp : Defines the entry point for the console application.
//
//
// main.cpp
// Ex3-Complex
//
// In this example we will create a complex number class. The purpose of this
// example is to become familiar with operator overloading, and should not be
// used in practice as C++ already has a complex number class.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double real, double imag);
Complex();
//Assignment operators
void operator +=(Complex rhs);
void operator -=(Complex rhs);
void operator *=(Complex rhs);
void operator /=(Complex rhs);
//Unary operators
void operator +();
void operator -();
//Getter Declarations
double get_real();
double get_imag();
private:
double real_;
double imag_;
};
//Operator Overloads
ostream& operator <<(ostream& os, Complex z);
Complex operator +(Complex lhs, Complex rhs);
Complex operator *(Complex lhs, Complex rhs);
Complex operator -(Complex lhs, Complex rhs);
Complex operator /(Complex lhs, Complex rhs);
bool operator ==(Complex lhs, Complex rhs);
bool operator !=(Complex lhs, Complex rhs);
Complex::Complex(double real, double imag) {
real_ = real;
imag_ = imag;
}
Complex::Complex() {
real_ = 0;
imag_ = 0;
}
//Getter Functions
double Complex::get_real() {
return real_;
}
double Complex::get_imag() {
return imag_;
}
ostream& operator<<(ostream& os, Complex z) {
os << z.get_real() << " + " << z.get_imag() << "i";
return os;
}
Complex operator +(Complex lhs, Complex rhs) {
return Complex(lhs.get_real() + rhs.get_real(),
lhs.get_imag() + rhs.get_imag());
}
Complex operator *(Complex lhs, Complex rhs) {
return Complex(
lhs.get_real() * rhs.get_real() - lhs.get_imag() * rhs.get_imag(),
lhs.get_imag() * rhs.get_real() + lhs.get_real() * rhs.get_imag());
}
Complex operator -(Complex lhs, Complex rhs) {
return Complex( lhs.get_real() - rhs.get_real(), lhs.get_imag() - rhs.get_imag() );
}
Complex operator /(Complex lhs, Complex rhs) {
return Complex( ( (lhs.get_real() * rhs.get_real() )+(lhs.get_imag() * rhs.get_imag()) ) / ((rhs.get_real() *rhs.get_real()) + (rhs.get_imag() * rhs.get_imag())),
((lhs.get_imag() * rhs.get_real()) - (lhs.get_real() * rhs.get_imag())) / ((rhs.get_real() *rhs.get_real()) + (rhs.get_imag() * rhs.get_imag())));
}
bool operator ==(Complex lhs, Complex rhs) {
return(lhs.get_real() == rhs.get_real() && lhs.get_imag() == rhs.get_imag());
}
bool operator !=(Complex lhs, Complex rhs) {
return(lhs.get_real() != rhs.get_real() || lhs.get_imag() != rhs.get_imag());
}
void Complex::operator +=(Complex rhs) {
real_ += rhs.get_real();
imag_ += rhs.get_imag();
}
void Complex::operator -=(Complex rhs) {
real_ -= rhs.get_real();
imag_ -= rhs.get_imag();
}
void Complex::operator *=(Complex rhs) {
real_ = real_ * rhs.get_real() - imag_ * rhs.get_imag();
imag_ = imag_ * rhs.get_real() + real_ * rhs.get_imag();
}
void Complex::operator /=(Complex rhs) {
real_ = (((real_ * rhs.get_real()) + (imag_ * rhs.get_imag())) / ((rhs.get_real() *rhs.get_real()) + (rhs.get_imag() * rhs.get_imag())));
imag_ = ((imag_ * rhs.get_real()) - (real_ * rhs.get_imag())) / ((rhs.get_real() *rhs.get_real()) + (rhs.get_imag() * rhs.get_imag()));
}
void Complex::operator +() {
imag_ = imag_;
real_ = real_;
}
void Complex::operator -() {
imag_ = 0 - imag_;
real_ = 0 - real_;
}
int main() {
Complex z(3, 2);
/*z. operator -();*/
Complex w(2, 2);
cout << "z = " << z << endl
<< "w = " << w << endl
<< "z + w = " << z + w << endl
<< "z * w = " << z * w << endl
<< "z - w = " << z - w << endl
<< "z / w = " << z / w << endl
<< "z == w = " << (z == w) << endl
<< "z != w = " << (z != w) << endl
<< "z + w == w + z " << (z + w == w + z) << endl
<< "z + w != w + z " << (z + w != w + z) << endl;
Complex i(0, 1);
cout << "i = " << i << endl << "i^2 = " << i * i << endl;
system("pause");
return 0;
}