Title: Convert double to string
1Convert double to string
//n the number of digits after decimal point
string d2char(double a, int n2) // n the
number of digits
// the decimal point. ...... int main()
string s double pi3.1415926 s
d2char(pi, 5) cout ltlt s ltlt endl s
d2char(pi) // same as s d2char(pi,2)
cout ltlt s ltlt endl ......
default value when omitted
3.14159 3.14
2Strategy
d2char(1234.123456789, 7)
1234.1234567.......
.1234567......
1234
.12344567...10 1.234567... int (1.234567)
1 1.234567... - 1 .234567...
1234 10 4
1234 / 10 123
.2344567...10 2.34567... int (2.34567)
2 2.34567... - 2 .34567...
123 10 3
123 / 10 12
12 10 2
.344567...10 3.4567... int (3.4567)
3 3.4567... - 3 .4567...
12 / 10 1
1 10 1
repeat 7 times
1 / 10 0
until 0
1 2 3 4 . 1 2 3 4 5 6 7
ltdequegt
3d2char(double a, int n)
//n the number of digits after decimal point
string d2char(double a, int n2) // n the
number of digits
// the decimal point. int i // the
integer part double f // the fraction
part dequeltchargt vs // for processing char
d // current digit string s
// string to return bool negativefalse //
to indicate sign. if (a lt 0) a -1
negativetrue i int(a) f a-i
................................
4d2char(double a, int n) (continue)
//n the number of digits after decimal point
string d2char(double a, int n2) // n the
number of digits
// the decimal point. ..... do // integer
part d (i 10) '0' i i /
10 vs.push_front(d) while (i !0 ) if
(negative) vs.push_front('-') if (n ! 0)
vs.push_back('.') for (i 0 iltni) f
f10 d '0' (int(f)10) vs.push_back(d)
s.resize(vs.size()) for (i0iltvs.size()i
) si vsi return s
5Printing a point
void printp(point p) cout ltlt "(" ltlt p.get_x()
ltlt "," ltlt p.get_y() ltlt ")" int main()
point p,q .... cout ltlt " Distance from
" printp(p) cout ltlt " to " printp(q)
cout ltlt " is " q.distance(p) .....
A better control
string printp(point p) return "("
d2char(p.get_x()) ", " d2char(p.get_y())
")" int main() ..... cout ltlt "
Distance from " ltlt printp(p) ltlt " to ltlt
printp(q) cout ltlt " is " ltlt q.distance(p)
.....
6Copy constructor copy the arguments
printp .... .... ....
p
string printp(point p) ...... int main()
point v .... cout ltlt printp(v) .....
..... .....
4
5
my_x my_y
Constructed by constructor
Constructed by copy constructor
v
..... .....
4
5
my_x my_y
7Define a copy constructor
class point public ..... point(const
point a) // copy constructor
..... private double my_x double my_y
..... .... pointpoint(const point a) //
1. copy constructor my_x a.my_x my_y
a.my_y
8The copy constructor defines how to copy
printp .... .... ....
p
string printp(point p) ...... int main()
point v .... cout ltlt printp(v) .....
..... .....
8
10
my_x my_y
Constructed by constructor
Constructed by copy constructor
v
..... .....
4
5
my_x my_y
pointpoint(point a) my_x 2a.my_x my_y
2a.my_y
9Define a friend
class point public ..... ..... friend
point middle (point, point) friend ...
friend ... private double my_x double
my_y .....
friends are not member functions
point middle (point a, point b) point
c c.my_x (a.my_x b.my_x)/2 c.my_y
(a.my_y b.my_y)/2 return c
10Logical operator on point
bool operator (point a, point b) return
(a.get_x() b.get_x() a.get_y()
b.get_y())
int main() point v1, v2 ...... if
(v1 v2) .... else
11 can be a friend of point
class point public ..... ..... friend
point middle (point, point) friend bool
operator (point a, point b) friend
... private double my_x double my_y
.....
bool operator (point a, point b) return
(a.my_x b.my_x a.my_y b.my_y)
12 can be a member function of point
may not be a good idea
class point public ..... bool operator
(point R) // L R, L is the calling object
..... private double my_x double my_y
.....
bool pointoperator (point R) return
(my_x R.my_x my_y R.my_y)
if (a b)
calling object
13Scalar less lt is a friend of point
class point public ..... ..... friend
point middle (point, point) friend bool
operator (point a, point b) friend bool
operator lt (point a, point b) private
double my_x double my_y .....
bool operator lt (point a, point b) return
(a.from_org() lt b.from_org())
14Overload assignment as a member function
class point public ..... point operator
(const point a) ..... private
double my_x double my_y .....
this is the pointer of the calling object C (A
B)
point pointoperator (const point a)
my_x a.my_x my_y a.my_y return this
// return as this so we can use abc
As my_x and my_y