《C++ Primer Plus(第6版)中文版》编程练习第11-12章答案

更新时间:2023-04-09 12:48:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第11章使用类

//ex11.1

//vector.h

#ifndef VECTOR_H_

#define VECTOR_H_

#include

namespace VECTOR

{

class Vector

{

public:

enum Mode {RECT, POL};

// RECT for rectangular, POL for Polar modes

private:

double x; // horizontal value

double y; // vertical value

double mag; // length of vector

double ang; // direction of vector in degrees

Mode mode; // RECT or POL

// private methods for setting values

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;} // report x value

double yval() const {return y;} // report y value

double magval() const {return mag;} // report magnitude

double angval() const {return ang;} // report angle

void polar_mode(); // set mode to POL

void rect_mode(); // set mode to RECT

// operator overloading

Vector operator+(const Vector & b) const;

Vector operator-(const Vector & b) const;

Vector operator-() const;

Vector operator*(double n) const;

// friends

friend Vector operator*(double n, const Vector & a);

friend std::ostream & operator<<(std::ostream & os, const Vector & v);

};

} // end namespace VECTOR

#endif

// vector.cpp

#include

#include"vector.h"// includes using std::sqrt;

using std::sin;

using std::cos;

using std::atan;

using std::atan2;

using std::cout;

namespace VECTOR

{

// compute degrees in one radian

const double Rad_to_deg = 45.0 / atan(1.0);

// should be about 57.2957795130823

// private methods

// calculates magnitude from x and y

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);

}

// set x from polar coordinate

void Vector::set_x()

{

x = mag * cos(ang);

}

// set y from polar coordinate

void Vector::set_y()

{

y = mag * sin(ang);

}

// public methods

Vector::Vector() // default constructor

{

x = y = mag = ang = 0.0;

mode = RECT;

}

// construct vector from rectangular coordinates if form is r // (the default) or else from polar coordinates if form is p 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;

}

}

// reset vector from rectangular coordinates if form is

// RECT (the default) or else from polar coordinates if

// form is POL

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() // destructor

{

}

void Vector::polar_mode() // set to polar mode

{

mode = POL;

}

void Vector::rect_mode() // set to rectangular mode {

mode = RECT;

}

// operator overloading

// add two Vectors

Vector Vector::operator+(const Vector & b) const

{

return Vector(x + b.x, y + b.y);

}

// subtract Vector b from a

Vector Vector::operator-(const Vector & b) const

{

return Vector(x - b.x, y - b.y);

}

// reverse sign of Vector

Vector Vector::operator-() const

{

return Vector(-x, -y);

}

// multiply vector by n

Vector Vector::operator*(double n) const

{

return Vector(n * x, n * y);

}

// friend methods

// multiply n by Vector a

Vector operator*(double n, const Vector & a)

{

return a * n;

}

// display rectangular coordinates if mode is RECT,

// else display polar coordinates if mode is POL

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 << ")";

}

else

os << "Vector object mode is invalid";

return os;

}

} // end namespace VECTOR

//main.cpp

#include

#include

#include// rand(), srand() prototypes

#include// time() prototype

#include"vector.h"

int main()

{

using namespace std;

using VECTOR::Vector;

srand(time(0)); // seed random-number generator double direction;

Vector step;

Vector result(0.0, 0.0);

unsigned long steps = 0;

double target;

double dstep;

ofstream fout;

fout.open("thewalk.txt");

cout << "Enter target distance: ";

cin >> target;

cout << "Enter step length: ";

cin >> dstep;

fout << "Target Distance: " << target << ", "

<< "Step Size: " << dstep << endl;

fout << steps << ": " << result << endl;

while (result.magval() < target)

{

direction = rand() % 360;

step.reset(dstep, direction, Vector::POL);

result = result + step;

steps++;

cout << steps << ": " << result << endl;

fout << steps << ": " << result << endl;

}

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 << result << endl;

result.polar_mode();

cout << " or\n" << result << endl;

cout << "Average outward distance per step = "

<< result.magval()/steps << endl;

fout << " or\n" << result << endl;

fout << "Average outward distance per step = "

<< result.magval()/steps << endl;

cout << "Bye!\n";

fout.close();

return 0;

}

//ex11.2

//vector.h

#ifndef VECTOR_H_

#define VECTOR_H_

#include

namespace VECTOR

{

class Vector

{

public:

enum Mode {RECT, POL};

// RECT for rectangular, POL for Polar modes private:

double x; // horizontal value

double y; // vertical value

Mode mode; // RECT or POL

// private methods for setting values

void set_x(double, double);

void set_y(double, double);

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 value

double yval() const {return y;} // report y value

double magval() const; // report magnitude

double angval() const; // report angle

void polar_mode(); // set mode to POL

void rect_mode(); // set mode to RECT

// operator overloading

Vector operator+(const Vector & b) const;

Vector operator-(const Vector & b) const;

Vector operator-() const;

Vector operator*(double n) const;

// friends

friend Vector operator*(double n, const Vector & a);

friend std::ostream & operator<<(std::ostream & os, const Vector & v);

};

} // end namespace VECTOR

#endif

// vector.cpp

#include

#include"vector.h"// includes

using std::sqrt;

using std::sin;

using std::cos;

using std::atan;

using std::atan2;

using std::cout;

namespace VECTOR

{

// compute degrees in one radian

const double Rad_to_deg = 45.0 / atan(1.0);

// should be about 57.2957795130823

// private methods

// calculates magnitude from x and y

// set x from polar coordinate

void Vector::set_x(double mag, double ang)

{

x = mag * cos(ang);

}

// set y from polar coordinate

void Vector::set_y(double mag, double ang)

{

y = mag * sin(ang);

}

// public methods

Vector::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 p 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 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 POL

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 set to 0\n";

x = y = 0.0;

mode = RECT;

}

}

Vector::~Vector() // destructor

{

}

double Vector::magval() const// report magnitude {

return sqrt(x * x + y * y);

}

double Vector::angval() const// report angle

{

if (x == 0.0 && y == 0.0)

return 0;

else

return 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 Vectors

Vector Vector::operator+(const Vector & b) const

{

return Vector(x + b.x, y + b.y);

}

// subtract Vector b from a

Vector Vector::operator-(const Vector & b) const

{

return Vector(x - b.x, y - b.y);

}

// reverse sign of Vector

Vector Vector::operator-() const

{

return Vector(-x, -y);

}

// multiply vector by n

Vector Vector::operator*(double n) const

{

return Vector(n * x, n * y);

}

// friend methods

// multiply n by Vector a

Vector operator*(double n, const Vector & a)

{

return a * n;

}

// display rectangular coordinates if mode is RECT,

// else display polar coordinates if mode is POL

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 << ")";

}

else

os << "Vector object mode is invalid";

return os;

}

} // end namespace VECTOR

//main.cpp

#include

#include// rand(), srand() prototypes

#include// time() prototype

#include"vector.h"

int main()

{

using namespace std;

using VECTOR::Vector;

srand(time(0)); // seed random-number generator double 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";

return 0;

}

//ex11.3

//注:头文件vector.h和实现文件vector.cpp同ex11.1,以下为main函数:

//main.cpp

#include

#include// rand(), srand() prototypes

#include// time() prototype

#include"vector.h"

int main()

{

using namespace std;

using VECTOR::Vector;

srand(time(0)); // seed random-number generator int N;

int Max = 0;

int Min = 1000;

int Sum = 0;

double Average;

double direction;

Vector step;

Vector result(0.0, 0.0);

unsigned long steps = 0;

double target;

double dstep;

cout << "Enter target distance: ";

cin >> target;

cout << "Enter step length: ";

cin >> dstep;

cout << "Enter the number of test: ";

cin >> N;

int number = N;

while (number--)

{

while (result.magval() < target)

{

direction = rand() % 360;

step.reset(dstep, direction, Vector::POL);

result = result + step;

steps++;

}

cout << N - number << " steps: " << steps << endl;

Sum += steps;

if (Max < steps)

Max = steps;

if (Min > steps)

Min = steps;

steps = 0;

result.reset(0.0, 0.0);

}

Average = double(Sum) / N;

cout << "Max steps: " << Max << endl;

cout << "Min steps: " << Min << endl;

cout << "Average steps: " << Average << endl;

cout << "Bye!\n";

return 0;

}

//ex11.4

//time.h

#ifndef TIME_H_

#define TIME_H_

#include

class Time

{

private:

int hours;

int minutes;

public:

Time();

Time(int h, int m = 0);

void Reset(int h = 0, int m = 0);

Time operator*(double n) const;

friend Time operator+(const Time & t1, const Time & t2);

friend Time operator-(const Time & t1, const Time & t2);

friend Time operator*(double n, const Time & t)

{

return t * n;

}

friend std::ostream & operator<<(std::ostream & os, const Time & t);

};

#endif

//time.cpp

#include"time.h"

Time::Time()

{

hours = minutes = 0;

}

Time::Time(int h, int m)

{

hours = h;

minutes = m;

}

void Time::Reset(int h, int m)

{

hours = h;

minutes = m;

}

Time Time::operator*(double mult) const

{

Time result;

long totalminutes = hours * mult * 60 + minutes * mult;

result.hours = totalminutes / 60;

result.minutes = totalminutes % 60;

return result;

}

Time operator+(const Time & t1, const Time & t2)

{

Time sum;

sum.minutes = t1.minutes + t2.minutes;

sum.hours = t1.hours + t2.hours + sum.minutes / 60;

sum.minutes %= 60;

return sum;

}

Time operator-(const Time & t1, const Time & t2)

{

Time diff;

int tot1, tot2;

tot1 = t1.minutes + 60 * t1.hours;

tot2 = t2.minutes + 60 * t2.hours;

diff.minutes = (tot1 - tot2) % 60;

diff.hours = (tot1 - tot2) / 60;

return diff;

}

std::ostream & operator<<(std::ostream & os, const Time & t)

{

os << t.hours << " hours, " << t.minutes << " minutes";

return os;

}

//main.cpp

#include

#include"time.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 = aida - tosca;

cout << "Aida - Tosca: " << temp << endl;

temp = aida * 1.17;

cout << "Aida * 1.17: " << temp << endl;

cout << "10.0 * Tosca: " << 10.0 * tosca << endl;

return 0;

}

//ex11.5

//stonewt.h

#ifndef STONEWT_H_

#define STONEWT_H_

#include

class Stonewt

{

public:

enum Mode {STONES, POUNDS};

private:

enum {Lbs_per_stn = 14};

int stone; // whole stones

double pds_left; // fractional pounds

double pounds; // entire weight in pounds

Mode mode; // display STONES or POUNDS

public:

Stonewt(double lbs, Mode form = STONES); // constructor for double pounds

Stonewt(int stn, double lbs, Mode form = STONES); // constructor for stone, lbs

Stonewt(); // default constructor

~Stonewt();

void stone_mode();

void pound_mode();

//operator overloading

Stonewt operator+(const Stonewt & sw) const;

Stonewt operator-(const Stonewt & sw) const;

Stonewt operator*(double m) const;

//friends

friend Stonewt operator*(double m, const Stonewt & sw)

{

return sw * m;

}

friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);

};

#endif

//stonewt.cpp

#include"stonewt.h"

using std::cout;

// construct Stonewt object from double value

Stonewt::Stonewt(double lbs, Mode form)

{

mode = form;

stone = int (lbs) / Lbs_per_stn; // integer pision

pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);

pounds = lbs;

}

// construct Stonewt object from stone, double values

Stonewt::Stonewt(int stn, double lbs, Mode form)

{

mode = form;

stone = stn;

pds_left = lbs;

pounds = stn * Lbs_per_stn +lbs;

}

Stonewt::Stonewt() // default constructor, wt = 0

{

stone = pounds = pds_left = 0;

mode = STONES;

}

Stonewt::~Stonewt() // destructor

{

}

void Stonewt::stone_mode() //set to stone mode

{

mode = STONES;

}

void Stonewt::pound_mode() //set to pound mode

{

mode = POUNDS;

}

//operator overloading

Stonewt Stonewt::operator+(const Stonewt & sw) const

{

return (pounds + sw.pounds);

}

Stonewt Stonewt::operator-(const Stonewt & sw) const

{

return (pounds - sw.pounds);

}

Stonewt Stonewt::operator*(double m) const

{

return (pounds * m);

}

//friend methods

std::ostream & operator<<(std::ostream & os, const Stonewt & sw) {

if (sw.mode == Stonewt::STONES)

os << sw.stone << " stone, " << sw.pds_left<< " pounds\n";

else if (sw.mode == Stonewt::POUNDS)

os << sw.pounds << " pounds\n";

else

os << "Stonewt object mode is invalid";

return os;

}

//main.cpp

#include

#include"stonewt.h"

using std::cout;

int main()

{

Stonewt sw1(285.7);

Stonewt sw2(3, 8);

cout << "sw1: " << sw1

<< "sw2: " << sw2;

Stonewt sw3 = sw1 + sw2;

Stonewt sw4 = sw1 - sw2;

Stonewt sw5 = sw1 * 2;

cout << "sw1 + sw2 = " << sw3;

sw3.pound_mode();

cout << "or\n" << sw3;

cout << "sw1 - sw2 = " << sw4;

sw4.pound_mode();

cout << "or\n" << sw4;

cout << "sw1 * 2 = " << sw5;

sw5.pound_mode();

cout << "or\n" << sw5;

return 0;

}

//ex11.6

//stonewt.h

#ifndef STONEWT_H_

#define STONEWT_H_

#include

class Stonewt

{

public:

enum Mode {STONES, POUNDS};

private:

enum {Lbs_per_stn = 14};

int stone; // whole stones

double pds_left; // fractional pounds

double pounds; // entire weight in pounds

Mode mode; // display STONES or POUNDS

public:

Stonewt(double lbs, Mode form = STONES); // constructor for double pounds

Stonewt(int stn, double lbs, Mode form = STONES); // constructor for stone, lbs

Stonewt(); // default constructor

~Stonewt();

void stone_mode();

void pound_mode();

//operator overloading

Stonewt operator+(const Stonewt & sw) const;

Stonewt operator-(const Stonewt & sw) const;

Stonewt operator*(double m) const;

bool operator<(const Stonewt & sw) const;

bool operator>(const Stonewt & sw) const;

bool operator==(const Stonewt & sw) const;

bool operator<=(const Stonewt & sw) const;

bool operator>=(const Stonewt & sw) const;

bool operator!=(const Stonewt & sw) const;

//friends

friend Stonewt operator*(double m, const Stonewt & sw)

{

return sw * m;

}

friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);

};

#endif

//stonewt.cpp

#include"stonewt.h"

using std::cout;

// construct Stonewt object from double value

Stonewt::Stonewt(double lbs, Mode form)

{

mode = form;

stone = int (lbs) / Lbs_per_stn; // integer pision

pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);

pounds = lbs;

}

// construct Stonewt object from stone, double values Stonewt::Stonewt(int stn, double lbs, Mode form)

{

mode = form;

stone = stn;

pds_left = lbs;

pounds = stn * Lbs_per_stn +lbs;

}

Stonewt::Stonewt() // default constructor, wt = 0 {

stone = pounds = pds_left = 0;

mode = STONES;

}

Stonewt::~Stonewt() // destructor

{

}

void Stonewt::stone_mode() //set to stone mode

{

mode = STONES;

}

void Stonewt::pound_mode() //set to pound mode

{

mode = POUNDS;

}

//operator overloading

Stonewt Stonewt::operator+(const Stonewt & sw) const

{

return (pounds + sw.pounds);

}

Stonewt Stonewt::operator-(const Stonewt & sw) const

{

return (pounds - sw.pounds);

}

Stonewt Stonewt::operator*(double m) const

{

return (pounds * m);

本文来源:https://www.bwwdw.com/article/s0bl.html

Top