-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmy_complex.cpp
More file actions
61 lines (55 loc) · 1.68 KB
/
Copy pathmy_complex.cpp
File metadata and controls
61 lines (55 loc) · 1.68 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
#include <string>
#include <sstream>
#include <iostream>
using std::string, std::ostringstream, std::cin, std::cout, std::endl;
class MyComplex
{
private:
double real = 0;
double imag = 0;
public:
MyComplex(double real_ = 0, double imag_ = 0) : real(real_), imag(imag_) {}
MyComplex(const MyComplex &z1)
{
real = z1.real;
imag = z1.imag;
}
string to_string()
{
ostringstream buffer;
buffer << real << " + " << imag << "i";
return string("MyComplex(") + string(buffer.str()) + string(")");
}
MyComplex operator+(const MyComplex &z1) { return MyComplex(real + z1.real, imag + z1.imag); }
MyComplex operator-(const MyComplex &z1) { return MyComplex(real - z1.real, imag - z1.imag); }
MyComplex operator*(const MyComplex &z1)
{
double new_real = real * z1.real - imag * z1.imag;
double new_imag = imag * z1.real + real * z1.imag;
return MyComplex(new_real, new_imag);
}
};
void print_string()
{
cout << endl
<< "test print_string" << endl;
MyComplex z1(3, 5), z2(5, 7), z3;
cout << z1.to_string() << endl;
cout << z2.to_string() << endl;
cout << z3.to_string() << endl;
}
void operator_overload()
{
cout << endl
<< "test operator_overload" << endl;
MyComplex z1(3, 5), z2(5, 7), z3;
cout << z1.to_string() << " + " << z2.to_string() << " = " << (z1 + z2).to_string() << endl;
cout << z1.to_string() << " - " << z2.to_string() << " = " << (z1 - z2).to_string() << endl;
cout << z1.to_string() << " * " << z2.to_string() << " = " << (z1 * z2).to_string() << endl;
}
int main()
{
print_string();
operator_overload();
return 0;
}