11.9 第十一章编程练习

11-1

#include
#include
#include
#include "vector.h"
#include
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream of;
of.open("D:/练习/其他/ConsoleApplication12/ConsoleApplication12/test.txt");
if (!of)
{
cout << "failed\n" << endl;
exit(0);
}
cout << "Enter target distance (q to quit) : ";
while(cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
{
break;
}
of << "Target Distance: " << target << ", Stepp Size: " << dstep << endl;
while(result.magval() < target)
{
direction = rand() % 360; // 随机角度
step.reset(dstep, direction, Vector::POL); // 随机的矢量
// 将随机的矢量加入
result = result + step;
of << steps << ": " << result << endl;
steps++;
}
of << "After " << steps << " steps, the subject "
"has the following location:\n";
of << result << endl;
result.polar_mode();
of << " or\n" << result << endl;
of << "Average outward distance per step = "
<< result.magval()/steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
of.close();
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}

11-2

vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
private:
double x;
double y;
Mode mode;
double get_mag() const;
double get_ang() const;
void set_x(double mag, double ang);
void set_y(double mag, double ang);
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
~Vector();
void reset(double n1, double n2, Mode form = RECT);
double xval() const { return x; }
double yval() const { return y; }
double magval() const;
double angval() const;
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
#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);
double Vector::get_mag() const
{
return sqrt(x * x + y * y);
}
double Vector::get_ang() const
{
if (x == 0 && y == 0)
return 0.0;
else
return atan2(y, x);
}
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)
{
double mag = n1;
double ang = n2 / Rad_to_deg;
set_x(mag, ang);
set_y(mag, ang);
}
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)
{
double mag = n1;
double ang = n2 / Rad_to_deg;
set_x(mag, ang);
set_y(mag, ang);
}
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.get_mag() << ", " << v.get_ang() * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
double Vector::magval() const
{
return get_mag();
}
double Vector::angval() const
{
return get_ang();
}
}

11-3

#include
#include
#include
#include "vector.h"
#include
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
unsigned long Max_step = 0;
unsigned long Min_step = 0;
ofstream of;
of.open("D:/练习/其他/ConsoleApplication12/ConsoleApplication12/test.txt");
if (!of)
{
cout << "failed\n" << endl;
exit(0);
}
cout << "Enter target distance (q to quit) : ";
while(cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
{
break;
}
of << "Target Distance: " << target << ", Stepp Size: " << dstep << endl;
while(result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
of << steps << ": " << result << endl;
steps++;
}
if (Max_step < steps || Max_step == 0)
{
Max_step = steps;
}
if (Min_step > steps || Min_step == 0)
{
Min_step = steps;
}
of << "After " << steps << " steps, the subject "
"has the following location:\n";
of << result << endl;
result.polar_mode();
of << " or\n" << result << endl;
of << "Average outward distance per step = "
<< result.magval()/steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
of << "Max: " << Max_step << endl;
of << "Min: " << Min_step << endl;
of.close();
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}

11-4

mytime3.h
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include
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 & t1, const Time & t2);
friend Time operator -(const Time & t1, const Time & t2);
friend Time operator *(double m, const Time & t);
friend Time operator *(const Time & t, double m);
friend std::ostream & operator <<(std::ostream & os, const Time & t);
}
#endif
mytime3.cpp
#include "mytime3.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;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
std::ostream & operator <<(std::ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
Time operator +(const Time & t1, const Time & t2)
{
Time t;
t.AddHr(t1.hours + t2.hours);
t.AddMin(t1.minutes + t2.minutes);
return t;
}
Time operator -(const Time & t1, const Time & t2)
{
Time t;
t.AddHr(t1.hours - t2.hours);
t.AddMin(t1.minutes - t2.minutes);
return t;
}
Time operator *(double m, const Time & t)
{
Time result;
long totalminutes = m * t.hours * 60 + m * t.minutes;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
Time operator *(const Time & t, double m)
{
Time result;
long totalminutes = m * t.hours * 60 + m * t.minutes;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}

11-5

Stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include
class Stonewt
{
private:
enum {Lbs_per_stn = 14};
enum Mode {STONE, N_POUND, D_POUND};
Mode form;
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
friend std::ostream & operator <<(std::ostream & os, const Stonewt & s);
Stonewt operator +(const Stonewt & s) const;
Stonewt operator -(const Stonewt & s) const;
Stonewt operator *(double m) const;
friend Stonewt operator *(double m, const Stonewt & s);
};
#endif
Stonewt.cpp
#include "stonewt.h"
Stonewt::Stonewt(double lbs) // 英磅
{
form = STONE;
stone = int (lbs) / Lbs_per_stn;
pds_left = int (lbs) % Lbs_per_stn;
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs) // 英石和英磅
{
form = STONE;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
form = STONE;
stone = 0;
pds_left = 0;
pounds = 0;
}
Stonewt::~Stonewt()
{
}
std::ostream & operator <<(std::ostream & os, const Stonewt & s)
{
switch (s.form)
{
case Stonewt::STONE:
{
os << s.stone << " Stones " << s.pds_left << " pounds ";
break;
}
case Stonewt::N_POUND:
{
os << (int)s.pounds << " pounds ";
break;
}
case Stonewt::D_POUND:
{
os << s.pounds << " pounds ";
break;
}
default:
{
os << "invalidate Mode";
}
}
return os;
}
Stonewt Stonewt::operator +(const Stonewt & s) const
{
return Stonewt(pounds + s.pounds);
}
Stonewt Stonewt::operator -(const Stonewt & s) const
{
return Stonewt(pounds - s.pounds);
}
Stonewt Stonewt::operator *(double m) const
{
return Stonewt(m * pounds);
}
Stonewt operator *(double m, const Stonewt & s)
{
return Stonewt(s.pounds * m);
}
main.cpp
#include
#include "stonewt.h"
int main()
{
using namespace std;
Stonewt s1 = 17.4;
cout << s1 << endl;
Stonewt s2(4, 2);
cout << s2 << endl;
Stonewt s3 = s1 + s2;
cout << s3 << endl;
s3 = 2.0 * s3;
cout << s3 << endl;
s3 = s3 * 0.5;
cout << s3 << endl;
cin.get();
return 0;
}

11-6