Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
private:
double x; // horizontal value double y; // vertical value
char mode; // 'r' = rectangular, 'p' = polar // private methods for setting values void set_mag(); void set_ang();
void set_x(double, double); void set_y(double, double); public:
Vector();
Vector(double n1, double n2, char form = 'r'); void set(double n1, double n2, char form = 'r'); ~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 'p' void rect_mode(); // set mode to 'r' // 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 ostream & operator<<(ostream & os, const Vector & v); };
} // end namespace VECTOR #endif
// pe11-2.cpp -- modified methods for Vector class #include
#include %using std::sqrt; using std::sin; using std::cos; using std::atan2; using std::cout;
namespace VECTOR {
const double Rad_to_deg = 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
SP 26 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
Vector::Vector() // default constructor {
x = y = 0.0; mode = 'r'; }
// 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, char form) {
mode = form;
if (form == 'r') {
x = n1; y = n2; }
else if (form == 'p') {
set_x(n1, n2 / Rad_to_deg); set_y(n1, n2 / Rad_to_deg); } else {
cout << \ cout << \ x = y = 0.0; mode = 'r'; } }
// set vector from rectangular coordinates if form is r (the // default) or else from polar coordinates if form is p void Vector:: set(double n1, double n2, char form) {
mode = form;
if (form == 'r') {
x = n1; y = n2; }
else if (form == 'p') {
set_x(n1, n2 / Rad_to_deg); set_y(n1, n2 / Rad_to_deg); } else {
cout << \ cout << \ x = y = 0.0; mode = 'r'; } }
Vector::~Vector() // destructor { }
double Vector::magval() const // report magnitude {
return sqrt(x*x +y*y); }
double Vector::angval() const // report angle
SP 27 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
{
if (x == 0.0 && y == 0.0) return 0; else
return atan2(y, x); }
void Vector::polar_mode() // set to polar mode {
mode = 'p'; }
void Vector::rect_mode() // set to rectangular mode {
mode = 'r'; }
// 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); }
// multiple 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 r, // else display polar coordinates if mode is p
ostream & operator<<(ostream & os, const Vector & v) {
if (v.mode == 'r')
os << \ else if (v.mode == 'p') {
os << \ << v.angval() * Rad_to_deg << \ } else
os << \ return os; }
SP 28 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
} // end namespace VECTOR
// pe11-2walk.cpp -- use the modified Vector class // compile with the vect.cpp file #include
#include
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 << \ while (cin >> target) {
cout << \ if (!(cin >> dstep)) break;
while (result.magval() < target) {
direction = rand() % 360;
step.set(dstep, direction, 'p'); result = result + step; steps++; }
cout << \ \ cout << result << endl; result.polar_mode();
cout << \
cout << \ << result.magval()/steps << endl; steps = 0;
result.set(0.0, 0.0);
cout << \ }
cout << \
return 0; }
PE 11-5
// pe11ston.h -- definition for Stonewt class (for pe 11-5) #ifndef PE11STONEWT_H_ #define PE11STONEWT_H_ #include
private:
enum {Lbs_per_stn = 14}; // pounds per stone int stone; // whole stones
double pds_left; // fractional pounds
SP 29 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
double pounds; // entire weight in pounds char mode; // display mode for weight
// 's' = stone, 'f' = float, 'w' = whole pounds public:
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs Stonewt(); // default constructor ~Stonewt();
void set_mode(char m) {mode = m; }
Stonewt operator+(const Stonewt & sw) const; Stonewt operator-(const Stonewt & sw) const; Stonewt operator*(double m) const;
friend Stonewt operator*(double m, const Stonewt & sw) { return sw * m; }
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw); };
#endif
// pe11ston.h -- definition for Stonewt class (for pe 11-5) #ifndef PE11STONEWT_H_ #define PE11STONEWT_H_ #include
private:
enum {Lbs_per_stn = 14}; // pounds per stone int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds char mode; // display mode for weight
// 's' = stone, 'f' = float, 'w' = whole pounds public:
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs Stonewt(); // default constructor ~Stonewt();
void set_mode(char m) {mode = m; }
Stonewt operator+(const Stonewt & sw) const; Stonewt operator-(const Stonewt & sw) const; Stonewt operator*(double m) const;
friend Stonewt operator*(double m, const Stonewt & sw) { return sw * m; }
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw); };
#endif
// pe11-5.cpp
#include
// link with pe11ston.cpp int main(void) {
using std::cout;
Stonewt fullback(245.5);
Stonewt cornerback(13, 5.2); cout << fullback; cout << cornerback;
cornerback.set_mode('w'); cout << cornerback; Stonewt lump;
lump = fullback + cornerback; cout << lump;
fullback = fullback * 1.1; cout << fullback;
SP 30 of 65 September 2, 2004