Convert double to string - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Convert double to string

Description:

Convert double to string //n: the number of digits after decimal point string d2char(double a, int n=2) // n: the number of digits // the decimal point. – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 15
Provided by: ChungC3
Category:
Tags: convert | double | string

less

Transcript and Presenter's Notes

Title: Convert double to string


1
Convert 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
2
Strategy
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
3
d2char(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
................................
4
d2char(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
5
Printing 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)
.....
6
Copy 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
7
Define 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
8
The 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
9
Define 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
10
Logical 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
13
Scalar 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())
14
Overload 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
Write a Comment
User Comments (0)
About PowerShow.com