C++ Primer Plus(第六版)第十一章课后习题

十年热恋 提交于 2020-01-11 20:46:29

C++ Primer Plus(第六版)第十一章课后习题

11.15(练习题中的11.5应为11.15,此处提供11.9.1, 11.9.2, 11.9.3需要的程序11.15)

vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
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;}
double yval() const {return y;}
double magval() const {return mag;}
double angval() const {return ang;}
void polar_mod();
void rect_mod();
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
#include “vector.h”
using std::sqrt;
using std::cin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
Vector::Vector()
{
x=y=mag=ang=0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
Vector::~Vector()
{
}
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
void Vector::set_mag()
{
mag=sqrt(xx+yy);
}
void Vector::set_ang()
{
if (x0.0&&y0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void Vector::set_x()
{
x=magcos(ang);
}
void Vector::set_y()
{
y=mag
sin(ang);
}
void Vector::polar_mod()
{
mode=POL;
}
void Vector::rect_mod()
{
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(nx,ny);
}
std::ostream & operator<< (std::ostream & os,const Vector & v)
{
if(v.modeVector::RECT)
os<<"(x,y)= " << v.x << ", " << v.y;
else if (v.mode
Vector::POL)
os<<"(m,a)= " << v.mag << ", " << v.ang;
else
os << “Vector object mod is invalid”;
return os;
}

randwalk.cpp
#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;
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_mod();
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”;
cin.clear();
while (cin.get() !=’\n’)
continue;
return 0;
}

11.9.1

vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
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;}
double yval() const {return y;}
double magval() const {return mag;}
double angval() const {return ang;}
void polar_mod();
void rect_mod();
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
#include “vector.h”
using std::sqrt;
using std::cin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
Vector::Vector()
{
x=y=mag=ang=0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
Vector::~Vector()
{
}
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
void Vector::set_mag()
{
mag=sqrt(xx+yy);
}
void Vector::set_ang()
{
if (x0.0&&y0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void Vector::set_x()
{
x=magcos(ang);
}
void Vector::set_y()
{
y=mag
sin(ang);
}
void Vector::polar_mod()
{
mode=POL;
}
void Vector::rect_mod()
{
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(nx,ny);
}
std::ostream & operator<< (std::ostream & os,const Vector & v)
{
if(v.modeVector::RECT)
os<<"(x,y)= " << v.x << ", " << v.y;
else if (v.mode
Vector::POL)
os<<"(m,a)= " << v.mag << ", " << v.ang;
else
os << “Vector object mod is invalid”;
return os;
}
}

randwalk.cpp
#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 outfile;
outfile.open(“m.txt”);
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if(!(cin>>dstep))
break;
outfile << “Target Distance is: " << target <<”, "
<< "Step Size is " << dstep << endl;
outfile << 0 << “: (x,y)= (”<<step.xval() << “, " << step.yval() << “)” << endl;
while(result.magval() < target)
{
direction=rand()/360;
step.reset(dstep,direction,Vector::POL);
result=result+step;
steps++;
outfile << steps << “: (x,y)= (”<<step.xval() << “, " << step.yval() << “)” << endl;
}
cout << “After " << steps << " steps, the subject has”
" the following location:\n”;
cout << result << endl;
outfile << “After " << steps << " steps, the subject has”
" the following location:\n”;
outfile << result << endl;
result.polar_mod();
cout << “or\n” << result << endl;
cout << "Average outward distance per step = "
<< result.magval()/steps << endl;
outfile << “or\n” << result << endl;
outfile << "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”;
cin.clear();
while (cin.get() !=’\n’)
continue;
outfile.close();
return 0;
}

11.9.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;
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;}
double yval() const {return y;}
double magval() const {return sqrt(xx+yy);}
double angval() const
{
if (x0.0&&y0.0)
return 0.0;
else
return atan2(y,x);
}
void polar_mod();
void rect_mod();
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
#include “vector.h”
using std::sqrt;
using std::cin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
Vector::Vector()
{
x=y=0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
break;
}
case POL:
{
x=n1cos(n2);
y=n2
sin(n2);
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=0;
mode=RECT;
}
}
}
Vector::~Vector()
{
}
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
break;
}
case POL:
{
x=n1cos(n2);
y=n2
sin(n2);
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=0;
mode=RECT;
}
}
}
void Vector::polar_mod()
{
mode=POL;
}
void Vector::rect_mod()
{
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(nx,ny);
}
std::ostream & operator<< (std::ostream & os,const Vector & v)
{
if(v.modeVector::RECT)
os<<"(x,y)= " << v.x << ", " << v.y;
else if (v.mode
Vector::POL)
os<<"(m,a)= " << v.magval() << ", " << v.angval();
else
os << “Vector object mod is invalid”;
return os;
}
}

randwalk.cpp
#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;
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_mod();
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”;
cin.clear();
while (cin.get() !=’\n’)
continue;
return 0;
}

11.9.3

vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
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;}
double yval() const {return y;}
double magval() const {return mag;}
double angval() const {return ang;}
void polar_mod();
void rect_mod();
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
#include “vector.h”
using std::sqrt;
using std::cin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
Vector::Vector()
{
x=y=mag=ang=0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
Vector::~Vector()
{
}
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
switch(mode)
{
case RECT:
{
x=n1;
y=n2;
set_mag();
set_ang();
break;
}
case POL:
{
mag=n1;
ang=n2;
set_x();
set_y();
break;
}
default:
{
cout <<“Incorrect third argument to Vector() --”;
cout <<“vector set to 0\n”;
x=y=mag=ang=0;
mode=RECT;
}
}
}
void Vector::set_mag()
{
mag=sqrt(xx+yy);
}
void Vector::set_ang()
{
if (x0.0&&y0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void Vector::set_x()
{
x=magcos(ang);
}
void Vector::set_y()
{
y=mag
sin(ang);
}
void Vector::polar_mod()
{
mode=POL;
}
void Vector::rect_mod()
{
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(nx,ny);
}
std::ostream & operator<< (std::ostream & os,const Vector & v)
{
if(v.modeVector::RECT)
os<<"(x,y)= " << v.x << ", " << v.y;
else if (v.mode
Vector::POL)
os<<"(m,a)= " << v.mag << ", " << v.ang;
else
os << “Vector object mod is invalid”;
return os;
}
}

randwalk.cpp
#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;
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if(!(cin>>dstep))
break;
int N;
cout << "Please enter the times you want to test: “;
cin >>N;
int * time=new int [N];
for(int j=0;j<N;j++)
{
while(result.magval() < target)
{
direction=rand()/360;
step.reset(dstep,direction,Vector::POL);
result=result+step;
steps++;
}
cout << “After " << steps << " steps, the subject has”//begin
" the following location:\n”;
cout << result << endl;
result.polar_mod();
cout << “or\n” << result << endl;
cout << "Average outward distance per step = "
<< result.magval()/steps << endl;//end //you can delete this content
time[j]=steps;
result.reset(0.0,0.0);
}
int max,min;
max=min=time[0];
double average,sum=0.0;
for(int j=0;j< N;j++)
{
max=max>time[j]?max:time[j];
min=min<time[j]?min:time[j];
sum+=time[j];
}
average=sum/N;
cout << "Max= " << max << endl;
cout << "Min= " << min << endl;
cout << "Average= " << average << endl;
steps =0;
result.reset(0.0,0.0);
cout << "Enter target distance (q to quit): ";
}
cout << “Bye\n”;
cin.clear();
while (cin.get() !=’\n’)
continue;
return 0;
}

11.12(此处提供11.9.4需要的程序11.12)

mytime3.h
#ifndef MYTIME_H
#define MYTIME_H
#include
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
~Time();
void AddHr(int h);
void AddMin(int m);
void Reset(int h,int m=0);
Time operator+(const Time &t) const;
Time operator-(const Time &t) const;
Time operator*(const double n) const;
friend Time operator*(const double m,const Time &t);
friend std::ostream & operator << (std::ostream &os,const Time & t);
};
#endif

mytime3.cpp
#include"mytime3.h"
#include
Time::Time()
{
hours=0;
minutes=0;
}
Time::Time(int h,int m)
{
hours=h;
minutes=m;
}
Time::~Time()
{
}
void Time::AddHr(int h)
{
hours=hours+h;
}
void Time::AddMin(int m)
{
hours=hours+(minutes+m)/60;
minutes=(minutes+m)%60;
}
void Time::Reset(int h,int m)
{
hours=h;
minutes=m;
}
Time Time::operator+(const Time &t) const
{
Time sum;
sum.hours=hours+t.hours+(minutes+t.minutes)/60;
sum.minutes=(minutes+t.minutes)%60;
return sum;
}
Time Time::operator-(const Time &t) const
{
Time diff;
int tot1,tot2;
tot1=r.hours60+r.minutes;
tot2=t.hours
60+t.minutes;
diff.hours=(tot1-tot2)/60;
diff.minutes=(tot1-tot2)%60;
return diff;
}
Time Time::operator*(double n) const
{
int tot;
Time muti;
tot=hours60+minutes;
tot
=n;
muti.hours=tot/60;
muti.minutes=tot%60;
return muti;
}
Time operator*(const double m,const Time &t)
{ return t*m; }
std::ostream & operator << (std::ostream &os,const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}

usetime3.cpp
#include
#include “mytime3.h”
int main()
{
using std::cout;
using std::endl;
Time aida(3,45);
Time tosca(2,48);
Time temp;
cout << “Aida and Tosca:\n”;
cout << aida << “;” << tosca << endl;
temp=aida+tosca;
cout << "Aida + Tosca: " << temp << endl;
temp=aida1.17;
cout << "10.0
Tosca: " << 10.0*tosca << endl;
return 0;
}

11.9.4

mytime3.h
#ifndef MYTIME_H
#define MYTIME_H
#include
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
~Time();
void AddHr(int h);
void AddMin(int m);
void Reset(int h,int m=0);
friend Time operator+(const Time &t,const Time &r);
friend Time operator-(const Time &t,const Time &r);
friend Time operator*(const Time &r,const double n);
friend Time operator*(const double m,const Time &t);
friend std::ostream & operator << (std::ostream &os,const Time & t);
};
#endif

mytime3.cpp
#include"mytime3.h"
#include
Time::Time()
{
hours=0;
minutes=0;
}
Time::Time(int h,int m)
{
hours=h;
minutes=m;
}
Time::~Time()
{
}
void Time::AddHr(int h)
{
hours=hours+h;
}
void Time::AddMin(int m)
{
hours=hours+(minutes+m)/60;
minutes=(minutes+m)%60;
}
void Time::Reset(int h,int m)
{
hours=h;
minutes=m;
}
Time operator+(const Time &t,const Time &r)
{
Time sum;
sum.hours=r.hours+t.hours+(r.minutes+t.minutes)/60;
sum.minutes=(r.minutes+t.minutes)%60;
return sum;
}
Time operator-(const Time &t,const Time &r)
{
Time diff;
int tot1,tot2;
tot1=r.hours60+r.minutes;
tot2=t.hours
60+t.minutes;
diff.hours=-(tot1-tot2)/60;
diff.minutes=-(tot1-tot2)%60;
return diff;
}
Time operator*(const Time &r,const double n)
{
int tot;
Time muti;
tot=r.hours60+r.minutes;
tot
=n;
muti.hours=tot/60;
muti.minutes=tot%60;
return muti;
}
Time operator*(const double m,const Time &t)
{
return t*m;
}
std::ostream & operator << (std::ostream &os,const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}

usetime3.cpp
#include
#include “mytime3.h”
int main()
{
using std::cout;
using std::endl;
Time aida(3,45);
Time tosca(2,48);
Time temp;
cout << “Aida and Tosca:\n”;
cout << aida << “;” << tosca << endl;
temp=aida+tosca;
cout << "Aida + Tosca: " << temp << endl;
temp=aida1.17;
cout << "10.0
Tosca: " << 10.0*tosca << endl;
return 0;
}

11.17(此处提供11.9.5, 11.9.6需要的程序11.17)

stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum {Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
};
#endif

stonewt.cpp
#include
using std::cout;
#include “stonewt.h”
Stonewt::Stonewt(double lbs)
{
stone=int (lbs)/Lbs_per_stn;
pds_left=int (lbs)%Lbs_per_stn+lbs-int (lbs);
pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone=stn;
pds_left=lbs;
pounds=stn*Lbs_per_stn+lbs;
}
Stonewt::Stonewt()
{
stone=pounds=pds_left=0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_stn() const
{
cout << stone << " stones, " << pds_left << " pounds\n";
}
void Stonewt::show_lbs() const
{
cout << pounds << " pounds \n";
}

stone.cpp
#include
using std::cout;
#include “stonewt.h”
void display(const Stonewt & st,int n);
int main()
{
Stonewt incognito=275;
Stonewt wolfe(285.7);
Stonewt taft(21,8);
cout << "The celebrity weighted ";
incognito.show_stn();
cout << "The detective weighted ";
wolfe.show_stn();
cout << "The President weighted ";
taft.show_stn();
incognito=276.8;
taft=325;
cout << "After dinner, the celebrity weighted ";
incognito.show_stn();
display(taft,2);
cout << “The wrestler weighted even more.\n”;
display(422,2);
cout << “No stone left unearned\n”;
return 0;
}
void display(const Stonewt & st, int n)
{
for(int i=0;i<n;i++)
{
cout << "Wow! ";
st.show_stn();
}
}

11.9.5

stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include
class Stonewt
{
private:
enum Mode{stone_pounds,pounds_};
enum {Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
Mode mode;

public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void Stonewt::stone_mode();
void Stonewt::pounds_mode();
friend std::ostream & operator<< (std::ostream &os,Stonewt &t);
Stonewt operator +(const Stonewt &n) const;
Stonewt operator -(const Stonewt &n) const;
Stonewt operator (double n) const;
friend Stonewt operator
(double n,const Stonewt &t);
};
#endif

stonewt.cpp
#include
using std::cout;
#include “stonewt.h”
Stonewt::Stonewt(double lbs)
{
stone=int (lbs)/Lbs_per_stn;
pds_left=int (lbs)%Lbs_per_stn+lbs-int (lbs);
pounds=lbs;
mode=stone_pounds;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone=stn;
pds_left=lbs;
pounds=stnLbs_per_stn+lbs;
mode=stone_pounds;
}
Stonewt::Stonewt()
{
stone=pounds=pds_left=0;
mode=stone_pounds;
}
Stonewt::~Stonewt()
{
}
void Stonewt::stone_mode()
{
mode=stone_pounds;
}
void Stonewt::pounds_mode()
{
mode=pounds_;
}
std::ostream & operator<< (std::ostream &os,Stonewt &t)
{
switch(t.mode)
{
case Stonewt::stone_pounds:
{
os << t.stone << " stones, " << t.pds_left << " pounds\n";
break;
}
case Stonewt::pounds_:
{
os << t.pounds << " pounds \n";
break;
}
default: os <<“Error!\n”;
}
return os;
}
Stonewt Stonewt::operator +(const Stonewt &n) const
{
Stonewt sum;
sum.pounds=pounds+n.pounds;
sum.stone=sum.pounds/Lbs_per_stn;
sum.pds_left=int (sum.pounds)%Lbs_per_stn+sum.pounds-int (sum.pounds);
return sum;
}
Stonewt Stonewt::operator -(const Stonewt &n) const
{
Stonewt diff;
diff.pounds=pounds-n.pounds;
diff.stone=diff.pounds/Lbs_per_stn;
diff.pds_left=int (diff.pounds)%Lbs_per_stn+diff.pounds-int (diff.pounds);
return diff;
}
Stonewt Stonewt::operator (double n) const
{
Stonewt multi;
multi.pounds=pounds
n;
multi.stone=multi.pounds/Lbs_per_stn;
multi.pds_left=int (multi.pounds)%Lbs_per_stn+multi.pounds-int (multi.pounds);
return multi;
}
Stonewt operator
(double n,const Stonewt &t)
{
return t*n;
}

stone.cpp
#include
using std::cout;
#include “stonewt.h”
int main()
{
enum Mode_{stone_pounds,pounds_};
Stonewt incognito=275;
Stonewt wolfe(285.7);
Stonewt taft(21,8);
cout << "The celebrity weighted ";
cout << incognito;
cout << "The detective weighted ";
cout << wolfe;
cout << "The President weighted ";
cout << wolfe;
incognito=276.8;
taft=325;
cout << "After dinner, the celebrity weighted ";
cout << incognito;
cout << “The wrestler weighted even more.\n”;
cout << “No stone left unearned\n”;
Stonewt sum,diff,multi;
sum=wolfe+taft;
sum.stone_mode();
cout << "Sum= " << sum;
sum.pounds_mode();
cout << "Sum= " << sum;
diff=wolfe-taft;
cout << "Diff= " << diff;
multi=wolfe2.5;
cout << "Multi= " << multi;
multi=1.2
wolfe;
cout << "Multi= " << multi;
return 0;
}

11.9.6

stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum {Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
bool operator >(const Stonewt &t);
bool operator >=(const Stonewt &t);
bool operator <(const Stonewt &t);
bool operator <=(const Stonewt &t);
bool operator ==(const Stonewt &t);
bool operator !=(const Stonewt &t);
void show_lbs() const;
void show_stn() const;
};
#endif

stonewt.cpp
#include
using std::cout;
#include “stonewt.h”
Stonewt::Stonewt(double lbs)
{
stone=int (lbs)/Lbs_per_stn;
pds_left=int (lbs)%Lbs_per_stn+lbs-int (lbs);
pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone=stn;
pds_left=lbs;
pounds=stn*Lbs_per_stn+lbs;
}
Stonewt::Stonewt()
{
stone=pounds=pds_left=0;
}
Stonewt::~Stonewt()
{
}
bool Stonewt::operator >(const Stonewt &t)
{
if(pounds>t.pounds)
return true;
else
return false;
}
bool Stonewt::operator >=(const Stonewt &t)
{
if(pounds>=t.pounds)
return true;
else
return false;
}
bool Stonewt::operator <(const Stonewt &t)
{
if(pounds<t.pounds)
return true;
else
return false;
}
bool Stonewt::operator <=(const Stonewt &t)
{
if(pounds<=t.pounds)
return true;
else
return false;
}
bool Stonewt::operator (const Stonewt &t)
{
if(pounds
t.pounds)
return true;
else
return false;
}
bool Stonewt::operator !=(const Stonewt &t)
{
if(pounds!=t.pounds)
return true;
else
return false;
}
void Stonewt::show_stn() const
{
cout << stone << " stones, " << pds_left << " pounds\n";
}
void Stonewt::show_lbs() const
{
cout << pounds << " pounds \n";
}

stone.cpp
#include
using std::cout;
using std::cin;
using std::endl;
#include “stonewt.h”
int main()
{
Stonewt a[6]={600,10,5};
int n;
cout << “Enter 3 number: \n”;
for(int i=3;i<6;i++)
{
cin >> n;
a[i]=n;
}
Stonewt b(11),max,min;
max=min=a[0];
int c=0;
for(int i=0;i<6;i++)
{
max=max>a[i]?max:a[i];
min=min<a[i]?min:a[i];
if(a[i]>=b)
c++;
}
cout << "Max= " ;
max.show_stn();
cout << "Min= " ;
min.show_stn();
cout << "There is(are) " << c << " members > 11 stones. "<< endl;
}

11.9.7

conplex0.h
#ifndef complex0
#define complex0
#include
class complex
{
private:
double real;
double imaginary;
public:
complex(double a,double b=0);
complex();
~complex();
complex operator+(const complex c) const;
complex operator-(const complex c) const;
complex operator*(const complex c) const;
complex operator*(double x) const;
friend complex operator*(double x,const complex c);
complex operator~() const;
friend std::ostream & operator << (std::ostream &os,const complex &c);
friend std::istream & operator >> (std::istream &is,complex &c);
};
#endif;

conplex0.cpp
#include
#include"complex0.h"
complex::complex(double a,double b)
{
real=a;
imaginary=b;
}
complex::complex()
{
real=0;
imaginary=0;
}
complex::~complex()
{
}
complex complex::operator+(const complex c) const
{
complex sum;
sum.real=real+c.real;
sum.imaginary=imaginary+c.imaginary;
return sum;
}
complex complex::operator-(const complex c) const
{
complex diff;
diff.real=real+c.real;
diff.imaginary=imaginary+c.imaginary;
return diff;
}
complex complex::operator*(const complex c) const
{
complex multi;
multi.real=realc.real-imaginaryc.imaginary;
multi.imaginary=realc.imaginary+imaginaryc.real;
return multi;
}
complex complex::operator*(double x) const
{
complex multi;
multi.real=realx;
multi.imaginary=imaginary
x;
return multi;
}
complex operator*(double x,const complex c)
{
return c*x;
}
complex complex::operator~() const
{
complex cp;
cp.real=real;
cp.imaginary=-imaginary;
return cp;
}
std::ostream & operator << (std::ostream &os,const complex &c)
{
os <<"(" <<c.real << “, " << c.imaginary <<”)";
return os;
}
std::istream & operator >> (std::istream &is,complex &c)
{
std::cout<<“Please enter real figure:”;
is >>c.real;
std::cout<<“Please enter imaginary figure:”;
is >>c.imaginary;
return is;
}

conplex_test.cpp
#include
using namespace std;
#include “complex0.h”
int main()
{
complex a(3.0,4.0);
complex c;
cout << “Enter a complex number (q to quit):\n”;
while(cin >> c)
{
cout << "c is " << c << ‘\n’;
cout << "complex conjugate is " << ~c <<’\n’;
cout << "a is " << a <<’\n’;
cout << "a + c is " << a+c <<’\n’;
cout << "a - c is " << a-c <<’\n’;
cout << "a * c is " << ac <<’\n’;
cout << "2 * c is " << 2
c <<’\n’;
cout << “Enter a complex number (q to quit):\n”;
}
cout << “Done!\n”;
return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!