-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZ3.CPP
53 lines (45 loc) · 1.06 KB
/
Z3.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
Listing 5 - A rudimentary class for complex numbers using a
mutable member to implement "lazy" evaluation and caching
for polar form
// z3.cpp
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
class complex
{
public:
complex(double r, double i);
complex(const complex &z);
complex &operator=(const complex &z);
~complex();
double real() const;
double imag() const;
double rho() const;
double theta() const;
private:
double re, im;
struct polar;
mutable polar *p;
};
// ... same as Listing 4 ...
double complex::rho() const
{
if (p == 0)
p = new polar(sqrt(re*re + im*im), atan2(im, re));
return p->rho;
}
double complex::theta() const
{
if (p == 0)
p = new polar(sqrt(re*re + im*im), atan2(im, re));
return p->theta;
}
complex operator+(const complex &z1, const complex &z2)
{
return complex
(z1.real() + z2.real(), z1.imag() + z2.imag());
}
int main()
{
// same as Listings 3 and 4
}