140 likes | 274 Views
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. { ...... } int main() { string s; double pi=3.1415926; s = d2char(pi, 5);
E N D
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. { ...... } int main() { string s; double pi=3.1415926; s = d2char(pi, 5); cout << s << endl; s = d2char(pi); // same as s = d2char(pi,2); cout << s << endl; ...... } default value when omitted 3.14159 3.14
d2char(1234.123456789, 7) Strategy 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 <deque>
d2char(double a, int n) //n: the number of digits after decimal point string d2char(double a, int n=2) // n: the number of digits // the decimal point. { int i; // the integer part double f; // the fraction part deque<char> vs; // for processing char d; // current digit string s; // string to return bool negative=false; // to indicate sign. if (a < 0) { a *= -1; negative=true; } i = int(a); f = a-i; ................................
d2char(double a, int n) (continue) //n: the number of digits after decimal point string d2char(double a, int n=2) // 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; i<n;i++) { f = f*10; d = '0' + (int(f)%10); vs.push_back(d); } s.resize(vs.size()); for (i=0;i<vs.size();i++) s[i] = vs[i]; return s; }
Printing a point void printp(point p) { cout << "(" << p.get_x() << "," << p.get_y() << ")"; } int main() { point p,q; .... cout << " Distance from "; printp(p); cout << " to "; printp(q); cout << " is "; q.distance(p) ..... } A better control string printp(point p) { return "(" + d2char(p.get_x()) + ", " + d2char(p.get_y()) + ")"; } int main() { ..... cout << " Distance from " << printp(p) << " to << printp(q); cout << " is " << q.distance(p); ..... }
Copy constructor: copy the arguments printp .... .... .... p: string printp(point p) { ...... } int main() { point v; .... cout << printp(v); ..... } ..... ..... my_x my_y Constructed by constructor Constructed by copy constructor v: ..... ..... my_x my_y
Define a copy constructor class point { public: ..... point(constpoint& a); // copy constructor ..... private: double my_x; double my_y; ..... }; .... point::point(constpoint & a) // 1. copy constructor; { my_x = a.my_x; my_y = a.my_y; }
The copy constructor defines how to copy printp .... .... .... p: string printp(point p) { ...... } int main() { point v; .... cout << printp(v); ..... } ..... ..... my_x my_y Constructed by constructor Constructed by copy constructor v: ..... ..... my_x my_y point::point(point a) { my_x = 2*a.my_x; my_y = 2*a.my_y; }
Define a friend class point { public: ..... ..... friend point middle (point, point); friend ... friend ... private: doublemy_x; doublemy_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; }
Logical == operator on point booloperator == (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 }
== can be a friend of point class point { public: ..... ..... friend point middle (point, point); friend bool operator == (point a, point b); friend ... private: doublemy_x; doublemy_y; ..... }; booloperator == (point a, point b) { return (a.my_x == b.my_x && a.my_y == b.my_y); }
may not be a good idea == can be a member function of point class point { public: ..... bool operator == (point R);// L == R, L is the calling object ..... private: doublemy_x; doublemy_y; ..... }; boolpoint::operator == (point R) { return (my_x == R.my_x && my_y == R.my_y); } if (a == b) calling object
Scalar less < is a friend of point class point { public: ..... ..... friend point middle (point, point); friend bool operator == (point a, point b); friendbool operator < (point a, point b); private: doublemy_x; doublemy_y; ..... }; booloperator < (point a, point b) { return (a.from_org() < b.from_org()); }
Overload assignment = as a member function class point { public: ..... point &operator = (const point & a); ..... private: doublemy_x; doublemy_y; ..... }; this is the pointer of the calling object C = (A = B) point & point::operator = (const point & a) { my_x = a.my_x; my_y = a.my_y; return*this; // return as this so we can use a=b=c; } A’s my_x and my_y