1.题目中应该是程序清单11.15,先附上书中vector的源码(未加命名空间)
Vector.h:
#include <iostream>
class Vector
{
public:
Vector();
Vector(double x, double y, char mode = 'r');
void Set(double n1, double n2, char form = 'r');
~Vector();
double x()const { return m_dX; }
double y()const { return m_dY; }
double magitude()const { return m_dMagitude; }
double angle()const { return m_dAngle; }
void PolarMode();
void RectMode();
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);
private:
double m_dX;
double m_dY;
double m_dMagitude;
double m_dAngle;
char m_cMode;
void set_magitude();
void set_angle();
void set_x();
void set_y();
};
Vector.cpp:
#include <cmath>
#include "Vector.h"
using std::sin;
using std::cos;
using std::atan2;
using std::cout;
const double Rad_to_deg = 57.2957795130823;
Vector::Vector()
{
m_dX = m_dY = m_dMagitude = m_dAngle = 0.0;
m_cMode = 'r';
}
Vector::~Vector()
{
}
void Vector::set_magitude() { m_dMagitude = sqrt(m_dX*m_dX + m_dY * m_dY); }
void Vector::set_angle() {
if (m_dX == 0.0 && m_dY == 0.0)
m_dAngle = 0.0;
else
m_dAngle = atan2(m_dY, m_dX);
}
void Vector::set_x() { m_dX = m_dMagitude * cos(m_dAngle); }
void Vector::set_y() { m_dY = m_dMagitude * sin(m_dAngle); }
Vector::Vector(double x, double y, char mode) {
m_cMode = mode;
if (m_cMode == 'r') {
m_dX = x;
m_dY = y;
set_magitude();
set_angle();
}
else if (m_cMode == 'p') {
m_dMagitude = x;
m_dAngle = y / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector()--";
cout << "Vector set to 0\n";
m_dX = m_dY = m_dMagitude = m_dAngle = 0.0;
m_cMode = 'r';
}
}
void Vector::Set(double n1, double n2, char form) {
m_cMode = form;
if (m_cMode == 'r') {
m_dX = n1;
m_dY = n2;
set_magitude();
set_angle();
}
else if (m_cMode == 'p') {
m_dMagitude = n1;
m_dAngle = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector()--";
cout << "Vector set to 0\n";
m_dX = m_dY = m_dMagitude = m_dAngle = 0.0;
m_cMode = 'r';
}
}
void Vector::PolarMode() { m_cMode = 'p'; }
void Vector::RectMode() { m_cMode = 'r'; }
Vector Vector::operator+(const Vector& b) const { return Vector(m_dX + b.m_dX, m_dY + b.m_dY); }
Vector Vector::operator-(const Vector& b) const { return Vector(m_dX - b.m_dX, m_dY - b.m_dY); }
Vector Vector::operator-() const { return Vector(-m_dX, -m_dY); }
Vector Vector::operator*(double n) const { return Vector(n*m_dX, n*m_dY); }
Vector operator*(double n, const Vector& a) { return a * n; }
std::ostream& operator<<(std::ostream& os, const Vector& v) {
if (v.m_cMode == 'r')
os << "(x,y) = (" << v.m_dX << "," << v.m_dY << ")";
else if (v.m_cMode == 'p')
os << "(m,a) = (" << v.m_dMagitude << "," << v.m_dAngle *Rad_to_deg << ")";
else
os << "Vector object mode is invalid";
return os;
}
main函数
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Vector.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Target Distancee:";
while (cin >> target) {
cout << ", Step Size:";
if (!(cin >> dstep))
break;
while (result.magitude()<target)
{
cout << result << endl;
direction = rand() % 360;
step.Set(dstep, direction, 'p');
result = result + step;
steps++;
}
cout << "After " << steps << "steps,the subject has the following location:\n";
cout << result << endl;
result.PolarMode();
cout << "or\n" << result << endl;
cout << "Average outward distance per step = " <<result.magitude()/steps <<endl;
}
return 0;
}
运行结果:
2.由于共有接口不变,因此main函数中的不变,修改vector为(该定义仍写在.h文件):
double magitude() {
m_dMagitude = sqrt(m_dX*m_dX + m_dY * m_dY);
return m_dMagitude;
}
double angle() {
if (m_dX == 0.0 && m_dY == 0.0)
m_dAngle = 0.0;
else
m_dAngle = atan2(m_dY, m_dX);
return m_dAngle;
}
3.main函数的代码为:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Vector.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
unsigned long max_steps = 0;
unsigned long min_steps = 0;
unsigned long total_steps = 0;
double average_steps = 0;
int test_times = 0;
cout << "Target Distancee:";
while (cin >> target) {
cout << ", Step Size:";
if (!(cin >> dstep))
break;
cout << ",Enter test times:";
cin >> test_times;
for (int i = 0; i < test_times; i++) {
cout << "第" << i + 1 << "次测试:"<<endl;
while (result.magitude() < target)
{
cout << result << endl;
direction = rand() % 360;
step.Set(dstep, direction, 'p');
result = result + step;
steps++;
cout << result << endl;
}
if (i == 0)
min_steps = steps;
cout << "After " << steps << "steps,the subject has the following location:\n";
cout << result << endl;
result.PolarMode();
cout << "or\n" << result << endl;
cout << "Average outward distance per step = " << result.magitude() / steps << endl;
total_steps += steps;
if (steps > max_steps)
max_steps = steps;
if (steps < min_steps)
min_steps = steps;
steps = 0;
result.Set(0, 0);
}
average_steps = total_steps*1.0 / test_times;
cout << "Test" << test_times << "times,最多走" << max_steps << "步,最少走"
<< min_steps << "步,平均走" << average_steps << "步。"<< endl;
}
return 0;
}
4.重载操作符有加、减、负,乘已经有友元函数实现方法:h文件增加:
friend Vector operator+(Vector& a, const Vector& b);
friend Vector operator-(Vector& a, const Vector& b);
friend Vector operator-(Vector& a);
cpp文件增加:
Vector operator+(Vector& a,const Vector& b) { return a + b; }
Vector operator-(Vector& a, const Vector& b) { return a - b; }
Vector operator-(Vector& a) { return -a; }
5.Stonewt.h:
class Stonewt
{
public:
enum format{STONES,IPOUNDS,DPOUNDS};
Stonewt();
Stonewt(double n, format f = STONES);
void Update();
~Stonewt();
Stonewt operator+(double n)const;
Stonewt operator-(double n)const;
Stonewt operator*(double n)const;
friend Stonewt operator+(double n,const Stonewt& st);
friend Stonewt operator-(double n, const Stonewt& st);
friend Stonewt operator*(double n, const Stonewt& st);
friend std::ostream& operator<<(std::ostream& os, const Stonewt& st);
private:
enum{Lbs_per_stn = 14};
double m_dStone;
int m_nPounds;
double m_dPounds;
double m_dTotal;
format m_format;
};
Stonewt.cpp:
#include <iostream>
#include "Stonewt.h"
Stonewt::Stonewt()
{
m_nPounds = m_dPounds = m_dStone = m_dTotal = 0;
m_format = STONES;
}
Stonewt::~Stonewt()
{
}
Stonewt::Stonewt(double n, format f) {
m_format = f;
m_dTotal = n;
Update();
}
void Stonewt::Update() {
if (m_format == STONES) {
m_dStone = m_dTotal;
m_nPounds = (int)m_dTotal * Lbs_per_stn;
m_dPounds = m_dTotal*Lbs_per_stn;
}
else if (m_format == IPOUNDS) {
m_nPounds = m_dTotal;
m_dStone = m_dTotal / Lbs_per_stn;
m_dPounds = m_dTotal;
}
else if (m_format == DPOUNDS) {
m_dPounds = m_dTotal;
m_nPounds = (int)m_dTotal;
m_dStone = m_dTotal / Lbs_per_stn;
}
}
Stonewt Stonewt::operator+(double n)const { return Stonewt(m_dTotal+n,m_format); }
Stonewt Stonewt::operator-(double n)const { return Stonewt(m_dTotal - n, m_format); }
Stonewt Stonewt::operator*(double n)const { return Stonewt(m_dTotal*n, m_format); }
Stonewt operator+(double n, const Stonewt& st) { return st + n; }
Stonewt operator-(double n, const Stonewt& st) { return st - n; }
Stonewt operator*(double n, const Stonewt& st) { return st * n; }
std::ostream& operator<<(std::ostream& os, const Stonewt& st) {
if(st.m_format == st.STONES){
std::cout <<"stones:"<< st.m_dStone << std::endl;
}
else if (st.m_format == st.IPOUNDS) {
std::cout << "pounds in int:" << st.m_nPounds << std::endl;
}
else if (st.m_format == st.DPOUNDS) {
std::cout << "pounds in double:" << st.m_dPounds << std::endl;
}
return os;
}
main函数:
#include <iostream>
#include "Stonewt.h"
using std::cout;
using std::endl;
int main()
{
Stonewt stone(100);
Stonewt intPounds(12.3, Stonewt::IPOUNDS);
Stonewt doublePounds(12.3, Stonewt::DPOUNDS);
Stonewt st1 = stone + 52;
Stonewt st2 = intPounds - 2;
Stonewt st3 = doublePounds * 2.3;
Stonewt st4 = 2.3 + intPounds;
Stonewt st5 = 53 - doublePounds;
Stonewt st6 = 5.6*stone;
cout << stone << endl;
cout << intPounds << endl;
cout << doublePounds << endl;
cout << st1 << endl;
cout << st2 << endl;
cout << st3 << endl;
cout << st4 << endl;
cout << st5 << endl;
cout << st6 << endl;
return 0;
}
6.stonewt重写为:
class StonewtB
{
public:
StonewtB();
StonewtB(double lbs);
StonewtB(int stn, double lbs);
~StonewtB();
void ShowPounds()const;
void ShowStone()const;
bool operator==(const StonewtB& st)const;
bool operator!=(const StonewtB& st)const;
bool operator<(const StonewtB& st)const;
bool operator>(const StonewtB& st)const;
bool operator<=(const StonewtB& st)const;
bool operator>=(const StonewtB& st)const;
private:
enum { Lbs_per_stn = 14 };
int m_nStone;
double m_dPdsLeft;
double m_dPounds;
};
#include <iostream>
#include "Stonewt.h"
StonewtB::StonewtB() { m_nStone = m_dPdsLeft = m_dPounds = 0;}
StonewtB::StonewtB(double lbs) {
m_nStone = int(lbs) / Lbs_per_stn;
m_dPdsLeft = int(lbs) % Lbs_per_stn - int(lbs);
m_dPounds = lbs;
}
StonewtB::StonewtB(int stn, double lbs) {
m_nStone = stn;
m_dPdsLeft = lbs;
m_dPounds = stn * Lbs_per_stn + lbs;
}
StonewtB::~StonewtB() {}
void StonewtB::ShowPounds()const {
std::cout <<m_dPounds << "pounds:" << std::endl;
}
void StonewtB::ShowStone()const { std::cout <<m_nStone<<"stone,"<< m_dPdsLeft << "pounds" << std::endl; }
bool StonewtB::operator==(const StonewtB& st)const { return m_dPounds == st.m_dPounds; }
bool StonewtB::operator!=(const StonewtB& st)const { return m_dPounds != st.m_dPounds; }
bool StonewtB::operator<(const StonewtB& st)const { return m_dPounds < st.m_dPounds; }
bool StonewtB::operator>(const StonewtB& st)const { return m_dPounds > st.m_dPounds; }
bool StonewtB::operator<=(const StonewtB& st)const { return m_dPounds <= st.m_dPounds; }
bool StonewtB::operator>=(const StonewtB& st)const { return m_dPounds >= st.m_dPounds; }
输出结果为:
7.complex.h:
#pragma once
#include <iostream>
class Complex
{
public:
Complex();
Complex(double real,double imag);
~Complex();
Complex operator+(Complex& com);
Complex operator-(Complex& com);
Complex operator*(Complex& com);
Complex operator*(double d);
friend Complex operator*(double d, Complex& com);
friend Complex operator~(Complex& com);
friend std::ostream& operator<<(std::ostream& os, const Complex& com);
friend std::istream& operator>>(std::istream& is, Complex& com);
private:
double m_dReal;
double m_dImag;
};
complex.cpp:
#include "Complex.h"
Complex::Complex()
{
m_dReal = m_dImag = 0;
}
Complex::~Complex()
{
}
Complex::Complex(double real, double imag):m_dReal(real),m_dImag(imag) {}
Complex Complex::operator+(Complex& com) { return Complex(m_dReal + com.m_dReal, m_dImag + com.m_dImag);}
Complex Complex::operator-(Complex& com) { return Complex(m_dReal - com.m_dReal, m_dImag - com.m_dImag); }
Complex Complex::operator*(Complex& com) { return Complex(m_dReal*com.m_dReal-m_dImag*com.m_dImag,m_dReal*com.m_dImag+m_dImag*com.m_dReal); }
Complex Complex::operator*(double d) { return Complex(m_dReal*d, m_dImag*d); }
Complex operator*(double d, Complex& com) { return com * d; }
Complex operator~(Complex& com) { return Complex(com.m_dReal,-com.m_dImag); }
std::ostream& operator<<(std::ostream& os, const Complex& com) {
std::cout << "(" << com.m_dReal << "," << com.m_dImag << ")";
return os;
}
std::istream& operator>>(std::istream& is, Complex& com) {
std::cout << "实部:";
is >> com.m_dReal;
std::cout << "虚部:";
is >> com.m_dImag;
return is;
}