第十一章 部分答案
11.9.1
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
//rect FOR rectangul,POL for Polar modes
private:
double x; // horizontal value
double y; // bertical value
double mag; // length of vector
double ang; //direction of vector in degress
Mode mode; // RECT or POL
//private methods for setting value
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);
};
}
#endif
#include
#include "vect.h" //include
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
//compute degree inone 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 * sin(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 feom 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 << "Vctor 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()
{
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
#include
#include"vect.h"
#include //rand
#include //time
#include
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0)); //seed rand-number generator
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream openFile;
openFile.open("Result.txt");
target = 100.0;
dstep = 20.0;
openFile << "Target Distance: " << target << ", Step Size: " << dstep << endl;
while (result.magval()
11.9.2
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
//rect FOR rectangul,POL for Polar modes
private:
double x; // horizontal value
double y; // bertical value
Mode mode; // RECT or POL
//private methods for setting value
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1=0.0, double n2=0.0, 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);
};
}
#endif
#include
#include "vect.h" //include
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
//compute degree inone radian
const double Rad_to_deg = 45.0 / atan(1.0);
//should be about 57.2957795130823
double Vector::magval() const
{
return sqrt(x*x + y*y);
}
double Vector::angval() const
{
if (x == 0.0 && y == 0.0)
return 0.0;
else
return atan2(y, x);
}
//public methods
Vector::Vector() //default constructor
{
x = y = 0.0;
mode = RECT;
}
//construct vector feom 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;
x = n1;
y = n2;
}
//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)
{
if (mode == form)
{
x = n1;
y = n2;
}
else
{
double mag = n1;
double ang = n2 / Rad_to_deg;
x = mag*cos(ang);
y = mag*sin(ang);
}
}
Vector::~Vector() //destructor
{
}
void Vector::polar_mode() //set to polar mode
{
mode = POL;
}
void Vector::rect_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
#include
#include"vect.h"
#include //rand
#include //time
#include
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0)); //seed rand-number generator
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream openFile;
openFile.open("Result.txt");
target = 100.0;
dstep = 20.0;
openFile << "Target Distance: " << target << ", Step Size: " << dstep << endl;
while (result.magval()
11.9.3
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
//rect FOR rectangul,POL for Polar modes
private:
double x; // horizontal value
double y; // bertical value
double mag; // length of vector
double ang; //direction of vector in degress
Mode mode; // RECT or POL
//private methods for setting value
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();
void clear();
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);
};
}
#endif
//vect.cpp ---- methods for the Vector class
#include
#include "vect.h" //include
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
//compute degree inone 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 * sin(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 feom 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 << "Vctor 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()
{
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;
}
void Vector::clear()
{
x = 0;
y = 0;
set_mag();
set_ang();
}
} //end namespace VECTOR
#include
#include"vect.h"
#include //rand
#include //time
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0)); //seed rand-number generator
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps = 0;
double target;
double dstep;
target = 100.0;
dstep = 20.0;
cout << "enter the times: ";
int time;
double Average_steps=0.0;
int steps_max =0;
int steps_min = INT_MAX;
cin >> time;
cout << "Target Distance: " << target << ", Step Size: " << dstep << endl;
for (int i = 0; i < time; i++)
{
while (result.magval()steps_max)
{
steps_max = steps;
}
if (steps
11.9.4
#ifndef MYTIME_H_
#define MYTIME_H_
namespace TIME
{
class Time
{
public:
Time();
Time(int h, int m = 0);
~Time();
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
//friend
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);
private:
int m_hours;
int m_minutes;
};
}
#endif
#include
#include "mytime.h"
namespace TIME
{
Time::Time()
{
m_hours = m_minutes = 0;
}
Time::Time(int h,int m)
{
m_hours = h;
m_minutes = m;
}
Time::~Time()
{
}
void Time::AddMin(int m)
{
m_minutes += m;
m_hours += m_minutes / 60;
m_minutes %= 60;
}
void Time::AddHr(int h)
{
m_hours += h;
}
void Time::Reset(int h, int m )
{
m_hours = h;
m_minutes = m;
}
Time operator+(const Time & t1, const Time & t2)
{
Time sum;
sum.m_minutes = t1.m_minutes + t2.m_minutes;
sum.m_hours = t1.m_hours + t2.m_hours + sum.m_minutes / 60;
sum.m_minutes %= 60;
return sum;
}
Time operator-(const Time & t1, const Time & t2)
{
Time diff;
int tot1, tot2;
tot1 = t1.m_minutes + 60 * t1.m_hours;
tot2 = t2.m_minutes + 60 * t2.m_hours;
diff.m_minutes = (tot1 - tot2) % 60;
diff.m_hours = (tot1 - tot2) / 60;
return diff;
}
Time operator*(double m, const Time & t)
{
Time res;
long totalminutes = t.m_minutes*m + t.m_hours * 60 * m;
res.m_hours = totalminutes / 60;
res.m_minutes = totalminutes % 60;
return res;
}
Time operator*(const Time & t, double m)
{
Time res;
long totalminutes = t.m_minutes*m + t.m_hours * 60 * m;
res.m_hours = totalminutes / 60;
res.m_minutes = totalminutes % 60;
return res;
}
std::ostream & operator<<(std::ostream &os, const Time & t)
{
os << t.m_hours << " hours, " << t.m_minutes << " minutes.";
return os;
}
}
#include
#include "mytime.h"
int main()
{
using namespace std;
using namespace TIME;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca: " << endl;
cout << aida << " ; " << tosca << endl;
temp = aida + tosca;
cout << "Aida+tosca =: " << temp;
cin.get();
return 0;
}
11.9.7
#ifndef FYSHU_H_
#define FUSHU_H_
#include
class fushu
{
public:
fushu();
fushu(double r,double i);
~fushu();
fushu operator=(const fushu & f) const;
fushu operator+(const fushu & f) const;
fushu operator-(const fushu & f) const;
fushu operator*(const fushu & f) const;
fushu operator*(const double x) const;
friend fushu operator~(fushu & f);
friend fushu operator*(double x, const fushu & f);
friend std::ostream & operator<<(std::ostream & os, const fushu & f);
friend std::istream & operator>>(std::istream & is, fushu & f);
private:
double m_r;
double m_i;
};
fushu::fushu()
{
m_r = 0;
m_i = 0;
}
fushu::fushu(double r,double i)
{
m_r = r;
m_i = i;
}
fushu::~fushu()
{
}
fushu fushu::operator=(const fushu & f) const
{
fushu temp;
temp.m_r = f.m_r;
temp.m_i = f.m_i;
return temp;
}
fushu fushu::operator+(const fushu & f) const
{
fushu temp;
temp.m_r = m_r + f.m_r;
temp.m_i = m_i + f.m_i;
return temp;
}
fushu fushu::operator-(const fushu & f) const
{
fushu temp;
temp.m_r = m_r - f.m_r;
temp.m_i = m_i - f.m_i;
return temp;
}
fushu fushu::operator*(const fushu & f) const
{
fushu temp;
temp.m_r = (m_r*f.m_r - m_i*f.m_i);
temp.m_i = (m_r*f.m_i + m_i*f.m_r);
return temp;
}
fushu fushu::operator*(const double x) const
{
fushu temp;
temp.m_r = x*m_r;
temp.m_i = x*m_i;
return temp;
}
fushu operator~(fushu & f)
{
fushu temp;
temp.m_r = f.m_r;
temp.m_i = -f.m_i;
return temp;
}
fushu operator*(double x, const fushu & f)
{
fushu temp;
temp.m_r = x*f.m_r;
temp.m_i = x*f.m_i;
return temp;
}
std::ostream & operator<<(std::ostream & os, const fushu & f)
{
os <<"("<< f.m_r << ", " << f.m_i<<"i " <<")"<< std::endl;
return os;
}
std::istream & operator>>(std::istream & is,fushu & f)
{
is >> f.m_r >> f.m_i;
return is;
}
#endif
#include
#include"fushu.h"
int main()
{
using namespace std;
fushu a(3.0, 4.0);
fushu c;
cout << "Enter a complex number (q to quit)" << endl;
while (cin >> c)
{
cout << "c is " << c << endl;
cout << "complex conjugate is " << ~c << endl;
cout << "a si " << a << endl;
cout << "a +c " << a + c << endl;
cout << "a -c " << a - c << endl;
cout << "a *c " << a * c << endl;
cout << "2*c " << 2* c << endl;
cout << "enter a complex number: (q to quit): " << endl;
}
cout << "Done" << endl;
cin.clear();
cin.get();
cin.sync();
cin.get();
}