2020 我的C++学习之路 C++PrimerPlus第十一章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第十一章课后习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以C++ Primer Plus為參考書籍,自身歸納知識點,加深記憶。僅供參考,DEV C++已通過編譯運行
。第5題第6題改寫過于麻煩,機械性重復就不寫了,寫的犯惡心。
練習1
P2
//p2.h #include <iostream> namespace VECTOR {class Vector{public:enum Mode { RECT, POL };private:double x; // horizontal valuedouble y; // vertical valueMode mode; // RECT or POLvoid set_x(double n1,double n2);void set_y(double n1,double n2);public:Vector();Vector(double n1, double n2, Mode form = RECT);void reset(double n1, double n2, Mode form = RECT);~Vector();double xval() const { return x; } // report x valuedouble yval() const { return y; } // report y valuedouble magval()const; // report magnitudedouble angval()const; // report anglevoid polar_mode(); // set mode to POLvoid rect_mode(); // set mode to RECTVector 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);friend std::ostream& operator<<(std::ostream& os, const Vector& v);};} //p2.cpp#include <cmath> #include "p2.h" // includes <iostream> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout;namespace VECTOR {const double Rad_to_deg = 45.0 / atan(1.0);void Vector::set_x(double n1, double n2){x = n1 * cos(n2 / Rad_to_deg);}void Vector::set_y(double n1, double n2){y = n1 * sin(n2 / Rad_to_deg);}// public methodsVector::Vector() // default constructor{x = y = 0.0;mode = RECT;}// construct vector from rectangular coordinates if form is r// (the default) or else from polar coordinates if form is pVector::Vector(double n1, double n2, Mode form){mode = form;if (form == RECT){x = n1;y = n2;}else if (form == POL){set_x(n1, n2);set_y(n1, n2);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";x = y = 0.0;mode = RECT;}}// reset vector from rectangular coordinates if form is// RECT (the default) or else from polar coordinates if// form is POLvoid 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);set_y(n1, n2);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";x = y = 0.0;mode = RECT;}}Vector::~Vector() // destructor{}double Vector::magval()const{return sqrt(x * x + y * y);}double Vector::angval()const{if (x == 0 && y == 0)return 0.0;elsereturn atan2(y, x);}void Vector::polar_mode() // set to polar mode{mode = POL;}void Vector::rect_mode() // set to rectangular mode{mode = RECT;}// operator overloading// add two VectorsVector Vector::operator+(const Vector& b) const{return Vector(x + b.x, y + b.y);}// subtract Vector b from aVector Vector::operator-(const Vector& b) const{return Vector(x - b.x, y - b.y);}// reverse sign of VectorVector Vector::operator-() const{return Vector(-x, -y);}// multiply vector by nVector Vector::operator*(double n) const{return Vector(n * x, n * y);}// friend methods// multiply n by Vector aVector operator*(double n, const Vector& a){return a * n;}// display rectangular coordinates if mode is RECT,// else display polar coordinates if mode is POLstd::ostream& operator<<(std::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.magval() << ", "<< v.angval() * Rad_to_deg << ")";}elseos << "Vector object mode is invalid";return os;}} // end namespace VECTOR //p2_main.cpp #include <iostream> #include <cstdlib> // rand(), srand() prototypes #include <ctime> // time() prototype #include "p2.h" int main() {using namespace std;using VECTOR::Vector;srand(time(0)); // seed random-number generatordouble direction;Vector step;Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;cout << "Enter target distance (q to quit): ";while (cin >> target){cout << "Enter step length: ";if (!(cin >> dstep))break;while (result.magval() < target){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;result.polar_mode();cout << " or\n" << result << endl;cout << "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!\n";/* keep window opencin.clear();while (cin.get() != '\n')continue;cin.get();*/return 0; }P3
//p3_main.cpp #include <iostream> #include <cstdlib> // rand(), srand() prototypes #include <ctime> // time() prototype #include "p2.h" int main() {using namespace std;using VECTOR::Vector;srand(time(0)); // seed random-number generatordouble direction;Vector step;Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;int N, times;double temp, max, min; temp = 0.0;cout << "Enter N:";while (cin >> N && N > 0){double* steparr = new double[N];cout << "Enter target distance (q to quit): ";cin >> target;cout << "Enter step length: ";cin >> dstep;for (times = 0; times < N; ++times){while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}steparr[times] = steps;steps = 0;result.reset(0.0, 0.0);}max = min = steparr[0];for (int i = 0; i < N; ++i){temp += steparr[i];max = max < steparr[i] ? steparr[i] : max;min = min > steparr[i] ? steparr[i] : min;}cout << "MAX: " << max << endl;cout << "MIN: " << min << endl;cout << "AVERAGE: " << temp / N << endl;cout << "Enter N:";}cout << "Bye!\n";/* keep window opencin.clear();while (cin.get() != '\n')continue;cin.get();*/return 0; }P4
//p4.h #include<iostream>class Time { private:int hours;int minutes; public:Time();Time(int h, int m = 0);void AddMin(int m);void AddHr(int h);void Reset(int h = 0, int m = 0);friend Time operator+(const Time& t, const Time& m);friend Time operator-(const Time& t, const Time& m);friend Time operator*(double m, const Time& t);friend std::ostream& operator<<(std::ostream& os, const Time& t); }; //p4.cpp #include"p4.h"Time::Time() {hours = minutes = 0; }Time::Time(int h, int m) {hours = h;minutes = m; }void Time::AddMin(int m) {minutes += m;hours += minutes / 60;minutes %= 60; }void Time::AddHr(int h) {hours += h; }Time operator+(const Time& t, const Time& m) {Time result;result.minutes = t.minutes + m.minutes;result.hours = result.minutes / 60 + t.hours + m.hours;result.minutes %= 60;return result; }Time operator-(const Time& t, const Time& m) {Time result;int tot1, tot2;tot1 = t.hours * 60 + t.minutes;tot2 = m.hours * 60 + m.minutes;result.hours = (tot2 - tot1) / 60;result.minutes = (tot2 - tot1) % 60;return result; } Time operator*(double m, const Time& t) {Time result;int temp = (t.hours * 60 + t.minutes)*m;result.hours = temp / 60;result.minutes = temp % 60;return result; } std::ostream& operator<<(std::ostream& os, const Time& t) {os << t.hours << " hours, " << t.minutes << " minutes";return os; } //p4_main.cpp #include<iostream> #include"p4.h"int main() {using std::cout;using std::endl;Time aida(3, 35);Time tosca(2, 48);Time temp;cout << "AIDA and TOSCA:\n";cout << aida << "; " << tosca << endl;temp = aida + tosca;cout << "Aida + Tosca: " << temp << endl;temp = 1.17 * aida;cout << "Aida * 1.17: " << temp << endl;cout << "10.0 * Tosca: " << 10.0 * tosca << endl;// std::cin.get();return 0; }P7
//p7.h #include<iostream>class complex { private:double real;double imag; public:complex();complex(double n1, double n2);~complex();void reset(double n1, double n2);complex operator+(const complex& c)const;complex operator-(const complex& c)const;complex operator*(const complex& c)const;complex operator*(const double x)const;complex operator~()const;friend complex operator*(const double n, const complex& c);friend std::istream& operator>>(std::istream& is, complex& c);friend std::ostream& operator<<(std::ostream& os, const complex& c); }; //p7.cpp #include<iostream> #include"p7.h"complex::complex() {real = imag = 0; }complex::complex(double n1, double n2) {real = n1;imag = n2; }complex::~complex() {}void complex::reset(double n1, double n2) {real = n1;imag = n2; }complex complex::operator+(const complex& c)const {complex temp;temp.real = c.real + real;temp.imag = c.imag + imag;return temp; }complex complex::operator-(const complex& c)const {complex temp;temp.real = real - c.real;temp.imag = imag - c.imag;return temp; }complex complex::operator*(const complex& c)const {complex temp;temp.real = real * c.real - imag * c.imag;temp.imag = real * c.imag + imag * c.real;return temp; }complex complex::operator*(const double x)const {complex temp;temp.real = real * x;temp.imag = imag * x;return temp; }complex complex::operator~()const {complex temp;temp.real = real;temp.imag = -imag;return temp; }complex operator*(const double n, const complex& c) {complex temp;temp.real = c.real * n;temp.imag = c.imag * n;return temp; }std::istream& operator>>(std::istream& is, complex& c) {double n1, n2;std::cout << "REAL: ";is >> n1;std::cout << "IMAG: ";is >> n2;c.reset(n1, n2);return is; }std::ostream& operator<<(std::ostream& os, const complex& c) {os << "( " << c.real << "," << c.imag << "i)\n";return os; } //p7_main.cpp #include<iostream> using namespace std; #include"p7.h"int main() {complex a(3.0, 4.0);complex c;cout << "Enter a complex number(q to quit):\n";while (cin >> c){cout << "c is " << c << endl;cout << "complex conjugate is " << ~c << endl;cout << "a is " << a << endl;cout << "a+c is " << a + c << endl;cout << "a-c is " << a - c << endl;cout << "a*c is " << a * c << endl;cout << "2*c is " << 2 * c << endl;cout << "Enter a complex number(q to quit):\n";}cout << "Done!\n";return 0; }總結
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第十一章课后习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++的学习之路 第十章
- 下一篇: 数据结构 数组