《C++ Primer Plus(第6版)中文版》编程练习第11-12章答案
更新时间:2023-04-09 12:48:01 阅读量: 实用文档 文档下载
- 财阀家的小儿子推荐度:
- 相关推荐
第11章使用类
//ex11.1
//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT for rectangular, POL for Polar modes
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// private methods for setting values
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);
};
} // end namespace VECTOR
#endif
// vector.cpp
#include
#include"vector.h"// includes
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// compute degrees in one 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 * cos(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 from 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 << "vector 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() // set to rectangular 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
//main.cpp
#include
#include
#include
#include
#include"vector.h"
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;
ofstream fout;
fout.open("thewalk.txt");
cout << "Enter target distance: ";
cin >> target;
cout << "Enter step length: ";
cin >> dstep;
fout << "Target Distance: " << target << ", "
<< "Step Size: " << dstep << endl;
fout << steps << ": " << result << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
cout << steps << ": " << result << endl;
fout << steps << ": " << result << endl;
}
cout << "After " << steps << " steps, the subject "
"has the following location:\n";
cout << result << endl;
fout << "After " << steps << " steps, the subject "
"has the following location:\n";
fout << result << endl;
result.polar_mode();
cout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval()/steps << endl;
fout << " or\n" << result << endl;
fout << "Average outward distance per step = "
<< result.magval()/steps << endl;
cout << "Bye!\n";
fout.close();
return 0;
}
//ex11.2
//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT for rectangular, POL for Polar modes private:
double x; // horizontal value
double y; // vertical value
Mode mode; // RECT or POL
// private methods for setting values
void set_x(double, double);
void set_y(double, double);
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; // 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);
};
} // end namespace VECTOR
#endif
// vector.cpp
#include
#include"vector.h"// includes
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 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
Vector::Vector() // default constructor
{
x = y = 0.0;
mode = RECT;
}
// 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, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = 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;
}
else if (form == POL)
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
Vector::~Vector() // destructor
{
}
double Vector::magval() const// report magnitude {
return sqrt(x * x + y * y);
}
double Vector::angval() const// report angle
{
if (x == 0.0 && y == 0.0)
return 0;
else
return atan2(y, x);
}
void Vector::polar_mode() // set to polar mode
{
mode = POL;
}
void Vector::rect_mode() // set to rectangular 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
//main.cpp
#include
#include
#include
#include"vector.h"
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 << "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_mode();
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";
return 0;
}
//ex11.3
//注:头文件vector.h和实现文件vector.cpp同ex11.1,以下为main函数:
//main.cpp
#include
#include
#include
#include"vector.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator int N;
int Max = 0;
int Min = 1000;
int Sum = 0;
double Average;
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance: ";
cin >> target;
cout << "Enter step length: ";
cin >> dstep;
cout << "Enter the number of test: ";
cin >> N;
int number = N;
while (number--)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
}
cout << N - number << " steps: " << steps << endl;
Sum += steps;
if (Max < steps)
Max = steps;
if (Min > steps)
Min = steps;
steps = 0;
result.reset(0.0, 0.0);
}
Average = double(Sum) / N;
cout << "Max steps: " << Max << endl;
cout << "Min steps: " << Min << endl;
cout << "Average steps: " << Average << endl;
cout << "Bye!\n";
return 0;
}
//ex11.4
//time.h
#ifndef TIME_H_
#define TIME_H_
#include
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void Reset(int h = 0, int m = 0);
Time operator*(double n) const;
friend Time operator+(const Time & t1, const Time & t2);
friend Time operator-(const Time & t1, const Time & t2);
friend Time operator*(double n, const Time & t)
{
return t * n;
}
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif
//time.cpp
#include"time.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
Time operator+(const Time & t1, const Time & t2)
{
Time sum;
sum.minutes = t1.minutes + t2.minutes;
sum.hours = t1.hours + t2.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time & t1, const Time & t2)
{
Time diff;
int tot1, tot2;
tot1 = t1.minutes + 60 * t1.hours;
tot2 = t2.minutes + 60 * t2.hours;
diff.minutes = (tot1 - tot2) % 60;
diff.hours = (tot1 - tot2) / 60;
return diff;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//main.cpp
#include
#include"time.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca:\n";
cout << aida<<"; " << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca: " << temp << endl;
temp = aida - tosca;
cout << "Aida - Tosca: " << temp << endl;
temp = aida * 1.17;
cout << "Aida * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
return 0;
}
//ex11.5
//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include
class Stonewt
{
public:
enum Mode {STONES, POUNDS};
private:
enum {Lbs_per_stn = 14};
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode; // display STONES or POUNDS
public:
Stonewt(double lbs, Mode form = STONES); // constructor for double pounds
Stonewt(int stn, double lbs, Mode form = STONES); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void stone_mode();
void pound_mode();
//operator overloading
Stonewt operator+(const Stonewt & sw) const;
Stonewt operator-(const Stonewt & sw) const;
Stonewt operator*(double m) const;
//friends
friend Stonewt operator*(double m, const Stonewt & sw)
{
return sw * m;
}
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);
};
#endif
//stonewt.cpp
#include"stonewt.h"
using std::cout;
// construct Stonewt object from double value
Stonewt::Stonewt(double lbs, Mode form)
{
mode = form;
stone = int (lbs) / Lbs_per_stn; // integer pision
pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs, Mode form)
{
mode = form;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
stone = pounds = pds_left = 0;
mode = STONES;
}
Stonewt::~Stonewt() // destructor
{
}
void Stonewt::stone_mode() //set to stone mode
{
mode = STONES;
}
void Stonewt::pound_mode() //set to pound mode
{
mode = POUNDS;
}
//operator overloading
Stonewt Stonewt::operator+(const Stonewt & sw) const
{
return (pounds + sw.pounds);
}
Stonewt Stonewt::operator-(const Stonewt & sw) const
{
return (pounds - sw.pounds);
}
Stonewt Stonewt::operator*(double m) const
{
return (pounds * m);
}
//friend methods
std::ostream & operator<<(std::ostream & os, const Stonewt & sw) {
if (sw.mode == Stonewt::STONES)
os << sw.stone << " stone, " << sw.pds_left<< " pounds\n";
else if (sw.mode == Stonewt::POUNDS)
os << sw.pounds << " pounds\n";
else
os << "Stonewt object mode is invalid";
return os;
}
//main.cpp
#include
#include"stonewt.h"
using std::cout;
int main()
{
Stonewt sw1(285.7);
Stonewt sw2(3, 8);
cout << "sw1: " << sw1
<< "sw2: " << sw2;
Stonewt sw3 = sw1 + sw2;
Stonewt sw4 = sw1 - sw2;
Stonewt sw5 = sw1 * 2;
cout << "sw1 + sw2 = " << sw3;
sw3.pound_mode();
cout << "or\n" << sw3;
cout << "sw1 - sw2 = " << sw4;
sw4.pound_mode();
cout << "or\n" << sw4;
cout << "sw1 * 2 = " << sw5;
sw5.pound_mode();
cout << "or\n" << sw5;
return 0;
}
//ex11.6
//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include
class Stonewt
{
public:
enum Mode {STONES, POUNDS};
private:
enum {Lbs_per_stn = 14};
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode; // display STONES or POUNDS
public:
Stonewt(double lbs, Mode form = STONES); // constructor for double pounds
Stonewt(int stn, double lbs, Mode form = STONES); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void stone_mode();
void pound_mode();
//operator overloading
Stonewt operator+(const Stonewt & sw) const;
Stonewt operator-(const Stonewt & sw) const;
Stonewt operator*(double m) const;
bool operator<(const Stonewt & sw) const;
bool operator>(const Stonewt & sw) const;
bool operator==(const Stonewt & sw) const;
bool operator<=(const Stonewt & sw) const;
bool operator>=(const Stonewt & sw) const;
bool operator!=(const Stonewt & sw) const;
//friends
friend Stonewt operator*(double m, const Stonewt & sw)
{
return sw * m;
}
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);
};
#endif
//stonewt.cpp
#include"stonewt.h"
using std::cout;
// construct Stonewt object from double value
Stonewt::Stonewt(double lbs, Mode form)
{
mode = form;
stone = int (lbs) / Lbs_per_stn; // integer pision
pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// construct Stonewt object from stone, double values Stonewt::Stonewt(int stn, double lbs, Mode form)
{
mode = form;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
}
Stonewt::Stonewt() // default constructor, wt = 0 {
stone = pounds = pds_left = 0;
mode = STONES;
}
Stonewt::~Stonewt() // destructor
{
}
void Stonewt::stone_mode() //set to stone mode
{
mode = STONES;
}
void Stonewt::pound_mode() //set to pound mode
{
mode = POUNDS;
}
//operator overloading
Stonewt Stonewt::operator+(const Stonewt & sw) const
{
return (pounds + sw.pounds);
}
Stonewt Stonewt::operator-(const Stonewt & sw) const
{
return (pounds - sw.pounds);
}
Stonewt Stonewt::operator*(double m) const
{
return (pounds * m);
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- C++
- 中文版
- 练习
- 答案
- 编程
- Primer
- Plus
- 11
- 12
- 17、上海金地高行项目活性立体社区整合推广策略(世纪瑞博,含平面
- 2022高压电工证考试及高压电工模拟考试题
- 广东省雷州市第二中学2022-2022学年高二上学期第一次月考物理试
- 贯彻执行《江苏省中小学管理规范》督查问题整改情况汇报 赣榆县
- 1-touch for HP-UX安装配置操作手册
- 收益法及市场法案例
- 2022年315食品安全知识竞赛题库
- 2022-2022学年统编版中外历史纲要下册第19课资本主义国家的新变
- 高中数学必修四期末试题及答案电子教案
- 【2022-2022】我的妈妈作文450字作文大全-优秀word范文 (3页)
- 曲水县职称论文发表网-企业管理企业发展前景重要性论文选题题目
- 中国生物产业报告2010-2011
- 思想道德修养与法律基础试题库
- 结构力学计算题及答案
- 恒力弹簧支吊架的选型
- 小学三年级英语:we love animals教学设计
- 《戴斯班克:培根》攻略
- 某知名公司KPI指标库-大小公司都适用,很全
- 一个人用英语去旅行:实用旅游英语口语系列
- 铁路建设工程监理规范实施细则(2007版核对)