C++ primer plus中文编程练习答案第11章.docx
《C++ primer plus中文编程练习答案第11章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus中文编程练习答案第11章.docx(21页珍藏版)》请在三一办公上搜索。
1、C+ primer plus中文编程练习答案第11章1、 /vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #include #include #include #include #include using namespace std; namespace VECTOR class Vector public: enum Mode RECT, POL ; private: double x; double y; double mag; doubleang; Mode mode; voidset_mag; voidset_ang; voidset_x;
2、 voidset_y; public: Vector; Vector(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); Vector; doublexvalconst return x; doubleyvalconst return y; doublemagvalconst return mag; doubleangvalconst return ang; voidpolar_mode; voidrect_mode; Vector operator+(cons
3、t Vector &b)const; Vector operator-(const Vector &b)const; Vector operator-const; Vector operator*(double n)const; friend Vector operator*(double n, const Vector &a); friendostream& operator(ostream&os, const Vector &v); ; #endif /vector.cpp #include vector.h namespace VECTOR const double Rad_to_deg
4、 = 45.0 / atan(1.0); void Vector:set_mag mag = sqrt(x * x + y * y); void Vector:set_ang if (x = 0.0 & y = 0.0) ang = 0.0; else ang = atan2(y, x); void Vector:set_x x = mag * cos(ang); void Vector:set_y y = mag * sin(ang); Vector:Vector x = y = mag = ang = 0.0; mode = RECT; Vector:Vector(double n1, d
5、ouble n2, Mode form) mode = form; if (form = RECT) x = n1; y = n2; set_mag; set_ang; else if (form = POL) mag = n1; ang = n2 / Rad_to_deg; set_x; set_y; else cout Incorrect 3rd argument to Vector - ; cout vector set to 0n; x = y = mag = ang = 0.0; mode = RECT; void Vector:reset(double n1, double n2,
6、 Mode form) mode = form; if (form = RECT) x = n1; y = n2; set_mag; set_ang; else if (form = POL) mag = n1; ang = n2 / Rad_to_deg; set_x; set_y; else cout Incorrect 3rd argument to Vector - ; cout vector set to 0n; x = y = mag = ang = 0.0; mode = RECT; Vector:Vector void Vector:polar_mode mode = POL;
7、 void Vector:rect_mode mode = RECT; Vector Vector:operator+(const Vector &b)const return Vector(x + b.x, y + b.y); Vector Vector:operator-(const Vector &b)const return Vector(x - b.x, y - b.y); Vector Vector:operator-const return Vector(-x, -y); Vector Vector:operator*(double n)const return Vector(n
8、*x, n*y); Vector operator*(double n, const Vector &a) return a*n; ostream&operator(ostream&os, const Vector &v) if (v.mode = Vector:RECT) os (x,y) = ( v.x , v.y ); else if (v.mode = Vector:POL) os (m,a) = ( v.mag , v.ang*Rad_to_deg ); else os Vector object mode is invalid; returnos; /randwalk.cpp #i
9、nclude vector.h int main using VECTOR:Vector; srand(time(0); double direction; Vector step; Vector result(0.0, 0.0); unsigned long steps = 0; double target; doubledstep; ofstreamfout; fout.open(savesteps.txt); cout target) coutdstep) break; fout Target Distance: target Step Size: dstependl; while (r
10、esult.magval target) fout steps : result endl; direction = rand % 360; step.reset(dstep, direction, Vector:POL); result = result + step; steps+; cout After steps steps, the subject has the following location:n; cout result endl; fout After steps steps, the subject has the following location:n; fout
11、result endl; result.polar_mode; cout orn result endl; cout Average outward distance per step = result.magval / steps endl; fout orn result endl; fout Average outward distance per step = result.magval / steps endl; steps = 0; result.reset(0.0, 0.0); cout Enter target distance (q to quit): ; cout Bye!
12、n; cin.clear; while (cin.get != n) continue; cin.get; return 0; 2、 /vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #include #include #include #include using namespace std; namespace VECTOR class Vector public: enum Mode RECT, POL ; private: double x; double y; Mode mode; doubleset_magconst; doubleset_
13、angconst; voidset_x(double mag, double ang); voidset_y(double mag, double ang); public: Vector; Vector(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); Vector; doublexvalconst return x; doubleyvalconst return y; doublemagvalconst return set_mag; doubleangv
14、alconst return set_ang; voidpolar_mode; voidrect_mode; Vector operator+(const Vector &b)const; Vector operator-(const Vector &b)const; Vector operator-const; Vector operator*(double n)const; friend Vector operator*(double n, const Vector &a); friendostream& operator(ostream&os, const Vector &v); ; #
15、endif /vector.cpp #include vector.h namespace VECTOR const double Rad_to_deg = 45.0 / atan(1.0); double Vector:set_magconst returnsqrt(x * x + y * y); double Vector:set_angconst if (x = 0.0 & y = 0.0) return 0.0; else return atan2(y, x); void Vector:set_x(double mag, double ang) x = mag * cos(ang);
16、void Vector:set_y(double mag, double ang) y = mag * sin(ang); Vector:Vector x = y = 0.0; mode = RECT; Vector:Vector(double n1, double n2, Mode form) mode = form; if (form = RECT) x = n1; y = n2; else if (form = POL) set_x(n1, n2 / Rad_to_deg); set_y(n1, n2 / Rad_to_deg); else cout Incorrect 3rd argu
17、ment to Vector - ; cout vector set to 0n; x = y = 0.0; mode = RECT; void Vector:reset(double n1, double n2, Mode form) mode = form; if (form = RECT) x = n1; y = n2; else if (form = POL) set_x(n1, n2 / Rad_to_deg); set_y(n1, n2 / Rad_to_deg); else cout Incorrect 3rd argument to Vector - ; cout vector
18、 set to 0n; x = y = 0.0; mode = RECT; Vector:Vector void Vector:polar_mode mode = POL; void Vector:rect_mode mode = RECT; Vector Vector:operator+(const Vector &b)const return Vector(x + b.x, y + b.y); Vector Vector:operator-(const Vector &b)const return Vector(x - b.x, y - b.y); Vector Vector:operat
19、or-const return Vector(-x, -y); Vector Vector:operator*(double n)const return Vector(n*x, n*y); Vector operator*(double n, const Vector &a) return a*n; ostream&operator(ostream&os, const Vector &v) if (v.mode = Vector:RECT) os (x,y) = ( v.x , v.y ); else if (v.mode = Vector:POL) os (m,a) = ( v.set_m
20、ag , v.set_ang*Rad_to_deg ); else os Vector object mode is invalid; returnos; /randwalk.cpp #include vector.h int main using VECTOR:Vector; srand(time(0); double direction; Vector step; Vector result(0.0, 0.0); unsigned long steps = 0; double target; doubledstep; cout target) coutdstep) break; while
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ primer plus中文编程练习答案第11章 plus 中文 编程 练习 答案 11
链接地址:https://www.31ppt.com/p-3060584.html