C++ Primer Plus第六版 第十一章 编程练习答案

来源:互联网 发布:简单java单线程程序 编辑:程序博客网 时间:2024/06/06 09:06
//第一题//windows下\r\n在txt中是换行...被坑了= = 话说开学了课好多啊= =....更新的都好迟。。。//vector.h#ifndef VECTOR_H_#define VECTOR_H_#include <iostream>namespace VECTOR{class Vector{public:enum Mode { RECT, POL };private:double x;double y;double mag;double ang;Mode mode;void set_mag();void set_ang();void set_x();void set_y();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; }double yval() const { return y; }double magval() const { return mag; }double angval() const { return ang; }void polar_mode();void rect_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);friend std::ostream &operator<<(std::ostream &os, const Vector &v);};}#endif//vector.cpp#include <cmath>#include "vector.h"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_mag(){mag = sqrt(x * x + y * y);}void Vector::set_ang(){if (x == 0.0 && y == 0.0)ang = 0.0;elseang = 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, double 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 0\n";x = y = mag = ang = 0.0;mode = RECT;}}void Vector::reset(double n1, double 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 0\n";x = y = mag = ang = 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::operator-() 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;}std::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.mag << ", " << v.ang * Rad_to_deg << ")";elseos << "Vector object mode is invalid";return os;}}//main.cpp#include <iostream>#include <fstream>#include <cstdlib>#include <ctime>#include "vector.h"int main(){std::fstream f;f.open("result.txt", 'w');srand(time(0));double direction;VECTOR::Vector step;VECTOR::Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;std::cout << "Enter target distance (q to quit): ";while (std::cin >> target){std::cout << "Enter step length: ";if (!(std::cin >> dstep))break;f << "Target Distance: " << target << "Step Size: " << dstep << "\r\n";while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, VECTOR::Vector::POL);result = result + step;f << steps << ": (x, y) = (" << step.xval() << ", " << step.yval() << ")\r\n";steps++;}f << "After " << steps << " steps, the subject has the following location:\r\n" << result << "\r\n";result.polar_mode();f << " or\r\n" << "Average outward distance per step = " << result.magval() / steps << "\r\n";steps = 0;std::cout << "Enter target distance (q to quit): ";}f.close();std::cout << "Bye!" << std::endl;std::cin.clear();while (std::cin.get() != '\n')continue;return 0;}





//第二题//vector.h#ifndef VECTOR_H_#define VECTOR_H_#include <iostream>namespace VECTOR{class Vector{public:enum Mode { RECT, POL };private:double x;double y;Mode mode;void set_x(double mag, double ang);void set_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();double xval() const { return x; }double yval() const { return y; }double magval() const { return sqrt(x * x + y * y); }double angval() const {if (x == 0.0 && y == 0.0)return 0.0;elsereturn atan2(y, x);}void polar_mode();void rect_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);friend std::ostream &operator<<(std::ostream &os, const Vector &v);};}#endif//vector.cpp#include <cmath>#include "vector.h"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 mag, double ang){x = mag * cos(ang);}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);set_y(n1, n2);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";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);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(){}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::operator-() 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;}std::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;}}//main.cpp#include <iostream>#include <cstdlib>#include <ctime>#include "vector.h"int main(){srand(time(0));double direction;VECTOR::Vector step;VECTOR::Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;std::cout << "Enter target distance (q to quit): ";while (std::cin >> target){std::cout << "Enter step length: ";if (!(std::cin >> dstep))break;while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, VECTOR::Vector::POL);result = result + step;steps++;}std::cout << "After " << steps << " steps, the subject has the following location:\n";std::cout << result << std::endl;result.polar_mode();std::cout << " or\n" << result << std::endl;std::cout << "Average outward distance per step = " << result.magval() / steps << std::endl;steps = 0;result.reset(0.0, 0.0);std::cout << "Enter target distance (q to quit): ";}std::cout << "Bye!\n";std::cin.clear();while(std::cin.get() != '\n')continue;return 0;}



//第三题//vector.h#ifndef VECTOR_H_#define VECTOR_H_#include <iostream>namespace VECTOR{class Vector{public:enum Mode { RECT, POL };private:double x;double y;Mode mode;void set_x(double mag, double ang);void set_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();double xval() const { return x; }double yval() const { return y; }double magval() const { return sqrt(x * x + y * y); }double angval() const {if (x == 0.0 && y == 0.0)return 0.0;elsereturn atan2(y, x);}void polar_mode();void rect_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);friend std::ostream &operator<<(std::ostream &os, const Vector &v);};}#endif//vector.cpp#include <cmath>#include "vector.h"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 mag, double ang){x = mag * cos(ang);}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);set_y(n1, n2);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";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);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(){}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::operator-() 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;}std::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;}}//main.cpp#include <iostream>#include <cstdlib>#include <ctime>#include "vector.h"int main(){srand(time(0));double direction;VECTOR::Vector step;VECTOR::Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;int N;unsigned long max = 0;unsigned long min = 999;double avg = 0;std::cout << "Enter N times to test: ";std::cin >> N;for (int i = 0; i < N; ++i){std::cout << "Enter target distance (q to quit): ";std::cin >> target;std::cout << "Enter step length: ";if (!(std::cin >> dstep))break;while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, VECTOR::Vector::POL);result = result + step;steps++;}max = (max < steps) ? steps : max;min = (min > steps) ? steps : min;avg += steps;result.polar_mode();steps = 0;result.reset(0.0, 0.0);}avg /= N;std::cout << "max: " << max << std::endl << "min: " << min << std::endl << "average: " << avg << std::endl;std::cout << "Bye!\n";std::cin.clear();while (std::cin.get() != '\n')continue;return 0;}


//第四题//Timer.h#ifndef TIMER_H_#define TIMER_H_#include <iostream>class Timer{private:int hours;int minutes;public:Timer();Timer(int h, int m = 0);void AddMin(int m);void AddHr(int h);void Reset(int h = 0, int m = 0);friend Timer operator+(const Timer &t1, const Timer &t2);friend Timer operator-(const Timer &t1, const Timer &t2);friend Timer operator*(const Timer &t, double n);friend Timer operator*(double n, const Timer &t);friend std::ostream &operator<<(std::ostream &os, const Timer &t);};#endif//Timer.cpp#include "Timer.h"Timer::Timer(){hours = minutes = 0;}Timer::Timer(int h, int m){hours = h;minutes = m;}void Timer::AddMin(int m){minutes += m;hours += minutes / 60;minutes %= 60;}void Timer::AddHr(int h){hours = (hours + h) % 24;}void Timer::Reset(int h, int m){hours = h;minutes = m;}Timer operator+(const Timer &t1, const Timer &t2){Timer t;t.minutes = t1.minutes + t2.minutes;t.hours = t1.hours + t2.hours + t.minutes / 60;t.minutes %= 60;t.hours %= 24;return t;}Timer operator-(const Timer &t1, const Timer &t2){Timer t;t.minutes = (t1.hours * 60 + t1.minutes) - (t2.hours * 60 + t2.minutes);t.hours = t.minutes / 60;t.minutes %= 60;return t;}Timer operator*(const Timer &t, double n){Timer Temp;Temp.minutes = t.hours * 60 * n + t.minutes * n;Temp.hours = Temp.minutes / 60;Temp.minutes %= 60;return Temp;}Timer operator*(double n, const Timer &t){return operator*(t, n);}std::ostream &operator<<(std::ostream &os, const Timer &t){os << t.hours << ":" << t.minutes;return os;}//main.cpp#include "Timer.h"#include <iostream>int main(){Timer t1(12, 53);Timer t2(14, 00);std::cout << t1 + t2 << std::endl;return 0;}

//第五题//main.cpp#include "stonewt.h"int main(){Stonewt s1(10);Stonewt s2(10);std::cout << s1 << std::endl;std::cout << s2 << std::endl;std::cout << s1 + s2 << std::endl;std::cout << s1 - s2 << std::endl;std::cout << s1 * s2 << std::endl;return 0;}//stonewt.h#ifndef STONEWT_H_#define STONEWT_H_#include <iostream>enum Mode { POUNDS, STONE };class Stonewt {private:static const int Lbs_per_stn = 14;Mode mode;int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt() {};void SetMode(Mode form);friend Stonewt operator+(const Stonewt &s1, const Stonewt &s2);friend Stonewt operator-(const Stonewt &s1, const Stonewt &s2);friend Stonewt operator*(const Stonewt &s1, const Stonewt &s2);friend std::ostream &operator<<(std::ostream &os, const Stonewt &s);};#endif//stonewt.cpp#include "stonewt.h"Stonewt::Stonewt(double lbs){mode = POUNDS;stone = (int)lbs / Lbs_per_stn;pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs;pounds = lbs;}Stonewt::Stonewt(int stn, double lbs){mode = POUNDS;stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn;}Stonewt::Stonewt(){mode = POUNDS;stone = pounds = pds_left = 0;}void Stonewt::SetMode(Mode form){mode = form;}Stonewt operator+(const Stonewt &s1, const Stonewt &s2){Stonewt s;if (s1.mode == POUNDS){s.pounds = s1.pounds + s2.pounds;s.SetMode(POUNDS);}else{s.stone = s1.stone + s2.stone;s.SetMode(STONE);}return s;}Stonewt operator-(const Stonewt &s1, const Stonewt &s2){Stonewt s;if (s1.mode == POUNDS){s.pounds = s1.pounds - s2.pounds;s.SetMode(POUNDS);}else{s.stone = s1.stone - s2.stone;s.SetMode(STONE);}return s;}Stonewt operator*(const Stonewt &s1, const Stonewt &s2){Stonewt s;if (s1.mode == POUNDS){s.pounds = s1.pounds * s2.pounds;s.SetMode(POUNDS);}else{s.stone = s1.stone * s2.stone;s.SetMode(STONE);}return s;}std::ostream &operator<<(std::ostream &os, const Stonewt &s){if (s.mode == POUNDS)os << s.pounds;elseos << s.stone;return os;}



//第六题//main.cpp#include "stonewt.h"#include <iostream>int main(){Stonewt s[6] = { {10}, {20}, {30} };for (int i = 3; i < 6; ++i){double t;std::cin >> t;s[i] = Stonewt(t);}Stonewt min(999), max(-1), st11(11);int num = 0;for (int i = 0; i < 6; ++i){min = min > s[i] ? s[i] : min;max = max < s[i] ? s[i] : max;if (s[i] >= st11)num++;}std::cout << "min: ";min.show_stn();std::cout << "max: ";max.show_stn();std::cout << ">=11: ";std::cout << num;system("PAUSE");return 0;}//stonewt.h#ifndef STONEWT_H_#define STONEWT_H_class Stonewt {private:enum{Lbs_per_stn = 14};int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void show_lbs() const;void show_stn() const;friend bool operator>(Stonewt &s1, Stonewt &s2);friend bool operator<(Stonewt &s1, Stonewt &s2);friend bool operator>=(Stonewt &s1, Stonewt &s2);friend bool operator<=(Stonewt &s1, Stonewt &s2);friend bool operator==(Stonewt &s1, Stonewt &s2);friend bool operator!=(Stonewt &s1, Stonewt &s2);};#endif//stonewt.cpp#include "stonewt.h"#include <iostream>Stonewt::Stonewt(double lbs){stone = (int)lbs / Lbs_per_stn;pds_left = (int)lbs % Lbs_per_stn;pounds = lbs;}Stonewt::Stonewt(int stn, double lbs){stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn + lbs;}Stonewt::Stonewt(){stone = pounds = pds_left = 0;}Stonewt::~Stonewt(){}void Stonewt::show_stn() const{std::cout << stone << " stone, " << pds_left << " pounds\n";}void Stonewt::show_lbs() const{std::cout << pounds << " pounds\n";}bool operator>(Stonewt &s1, Stonewt &s2){return s1.pounds > s2.pounds ? true : false;}bool operator<(Stonewt &s1, Stonewt &s2){return s1.pounds < s2.pounds ? true : false;}bool operator>=(Stonewt &s1, Stonewt &s2){return s1.pounds >= s2.pounds ? true : false;}bool operator<=(Stonewt &s1, Stonewt &s2){return s1.pounds <= s2.pounds ? true : false;}bool operator==(Stonewt &s1, Stonewt &s2){return s1.pounds == s2.pounds ? true : false;}bool operator!=(Stonewt &s1, Stonewt &s2){return s1.pounds != s2.pounds ? true : false;}



//第七题//main.cpp#include <iostream>using namespace std;#include "Complex0.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 << '\n';cout << "complex conjugate is " << ~c << '\n';cout << "a is " << a << '\n';cout << "a + c is " << a + c << '\n';cout << "a - c is " << a - c << '\n';cout << "a * c is " << a * c << '\n';cout << "2 * c is " << 2 * c << '\n';cout << "Enter a complex number (q to quit):\n";}cout << "Done!\n";return 0;}//complex0.h#ifndef COMPLEX0_H_#define COMPLEX0_H_#include <iostream>class complex {private:double a, b;public:complex(const double x = 0, const double y = 0) { a = x, b = y; }complex(const complex &c);void reset(const double x, const double y) { a = x, b = y; }complex &operator=(const complex &c);friend complex operator+(const complex &c1, const complex &c2);friend complex operator-(const complex &c1, const complex &c2);friend complex operator*(const complex &c1, const complex &c2);friend complex operator*(const double x, const complex &c1);friend complex operator*(const complex &c1, const double x);friend complex operator~(const complex &c);friend std::ostream &operator<<(std::ostream &os, const complex &c);friend std::istream &operator>>(std::istream &is, complex &c);virtual ~complex() {}};#endif//complex0.cpp#include "Complex0.h"complex::complex(const complex & c){a = c.a;b = c.b;}complex & complex::operator=(const complex & c){if (this == &c)return *this;a = c.a;b = c.b;return *this;}complex operator+(const complex &c1, const complex &c2){complex c;c.a = c1.a +c2.a;c.b = c1.b + c2.b;return c;}complex operator-(const complex &c1, const complex &c2){complex c;c.a = c1.a - c2.a;c.b = c1.b - c2.b;return c;}complex operator*(const complex &c1, const complex &c2){complex c;    c.a = c1.a * c2.a - c1.b * c2.b;c.b = c1.a * c2.b + c1.b * c2.a;return c;}complex operator*(const double x, const complex &c1){complex c;c.a = c1.a * x;c.b = c1.b * x;return c1;}complex operator*(const complex &c1, const double x){return x * c1;}complex operator~(const complex &c){complex tc;tc.b = -c.b;return tc;}std::ostream &operator<<(std::ostream &os, const complex &c){os << "(" << c.a << ", " << c.b << "i)";return os;}std::istream &operator>>(std::istream &is, complex &c){std::cout << "real: ";is >> c.a;std::cout << "imaginary: ";is >> c.b;return is;}


0 0
原创粉丝点击