C++PrimePlus(第六版)第十一章课后编程练习
1.修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标识。另外,让该程序将初始条件(目标距离和步长)以及结果小计写入到该文件中。
该程序头文件与成员函数文件与随机漫步程序文件一样,只需修改主函数即可代码如下:
重点在于第六章讲解的头文件fstream定义一个用于处理输出的文件输出类ofstream类。
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
int main()
{
using namespace std;
ofstream outFile;
outFile.open("carinfo.txt");
using VECTOR::Vector;
srand(time(0));
double direction;
Vector result(0.0, 0.0);
Vector step;
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;
outFile << "Target Distance:" <<target<<", Step Size: "<<dstep << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
cout << steps << ": (x,y) = (" << result.xval() << "," << result.yval() <<")"<< endl;
outFile << steps << ": (x,y) = (" << result.xval() << "," << result.yval() << ")" << endl;
}
//output the screen
cout << "After " << steps << " steps ,the subject "
"has the following locaion\n";
cout << result << endl;
//output the file
outFile << "After " << steps << " steps ,the subject "
"has the following locaion\n";
outFile << result << endl;
result.polar_mode();
//output the screen
cout << " or\n" << result << endl;
cout << " Average outward distance per step = "
<< result.magval() / steps << endl;
//output the file
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;
}
2、
#pragma once
#ifndef VECOTR_H_
#define VECTOR_H_
#include<iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
Mode mode;
void set_x(double n1,double n2);
void set_y(double n1,double n2);
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(x * x + y * y)); }
double angval()const;
void polar_mode();
void rect_mode();
//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<cmath>
#include"vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
//compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
//should be about 57.2957795130823
//private methods
double Vector::angval()const
{
if (x == 0.0&&y == 0.0)
return 0.0;
else
return atan2(y, x);
}
//set x from polar coordinate
void Vector::set_x(double n1,double n2)
{
x = n1*cos(n2);
}
void Vector::set_y(double n1, double n2)
{
y = n1 * sin(n2);
}
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)
{
n2 = n2 / Rad_to_deg;
set_x(n1,n2);
set_y(n1,n2);
}
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)
{
n2 = n2 / Rad_to_deg;
set_x(n1, n2);
set_y(n1, n2);
}
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);
}
//friend methods
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;
}
}
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
int main()
{
using namespace std;
ofstream outFile;
outFile.open("carinfo.txt");
using VECTOR::Vector;
srand(time(0));
double direction;
Vector result(0.0, 0.0);
Vector step;
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;
outFile << "Target Distance:" <<target<<", Step Size: "<<dstep << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
cout << steps << ": (x,y) = (" << result.xval() << "," << result.yval() <<")"<< endl;
outFile << steps << ": (x,y) = (" << result.xval() << "," << result.yval() << ")" << endl;
}
//output the screen
cout << "After " << steps << " steps ,the subject "
"has the following locaion\n";
cout << result << endl;
//output the file
outFile << "After " << steps << " steps ,the subject "
"has the following locaion\n";
outFile << result << endl;
result.polar_mode();
//output the screen
cout << " or\n" << result << endl;
cout << " Average outward distance per step = "
<< result.magval() / steps << endl;
//output the file
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;
}
3、本题的头文件与成员函数与第二题相同
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
//using namespace std;
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector result(0.0, 0.0);
Vector step;
unsigned long steps = 0;
double target;
double dstep;
unsigned long min, max, N,sum=0;
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
cout << "Enter test times: ";
cin >> N;
for (int i = 0; i < N; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
}
if (i == 0)
{
min = max = steps;
}
else
{
if (steps > max)
max = steps;
if (steps < min)
min = steps;
}
sum += steps;
steps = 0;
result.reset(0.0, 0.0, Vector::RECT);
}
cout << "max steps= " << max
<< "min steps= " << min
<< "average steps= " << sum / N
<< endl;
sum = 0;
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
4、重新编写Time类,使用友元函数。
ifndef MYTIME0_H_
#define MYTIME0_H_
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 &a,const Time & t);
friend Time operator-(const Time &a, const Time & t);
friend Time operator*(const Time &a, const Time & t);
friend Time operator*(double n, const Time & t);
friend Time operator*( const Time & t,double n );
friend std::ostream & operator<<(std::ostream &os, const Time &t);
};
#endif
#include<iostream>
#include"mytime0.h"
using namespace std;
Time::Time()
{
minutes = hours = 0;
}
Time::Time(int h, int m)
{
minutes = m;
hours = h;
}
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 = minutes = 0;
}
Time operator+(const Time &t,const Time &a)
{
Time sum;
sum.minutes = a.minutes + t.minutes;
sum.hours = a.hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time &a, const Time & t)
{
Time diff;
int tot1, tot2;
tot1 = a.minutes + a.hours * 60;
tot2 = t.minutes + t.hours * 60;
diff.minutes= (tot1 - tot2)%60;
diff.hours = (tot1 - tot2) / 60;
return diff;
}
Time operator*(const Time &a, const Time & t)
{
Time mult;
int mu1, mu2;
mu1 = a.minutes*t.minutes;
mu2 = a.hours*t.hours;
mult.hours = mu2 + mu1 / 60;
mult.minutes = mu1 % 60;
return mult;
}
Time operator*(double n, const Time & t)
{
Time result;
result.minutes = n * t.minutes;
result.hours = n * t.hours + result.minutes / 60;
result.minutes %= 60;
return result;
}
Time operator*(const Time & t, double n)
{
return n * t;
}
std::ostream &operator<<(std::ostream &os, const Time &t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
#include<iostream>
#include"mytime0.h"
using namespace std;
int main()
{
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
Time Mult;
int n = 2;
cout << "planning time = ";
cout << planning;
cout << endl;
total = coding + fixing;
cout << "coding time = ";
cout << total << endl;
Mult = coding * fixing;
cout << "Mult time = ";
cout << Mult << endl;
Mult = 2 * fixing;
cout << "Mult time = ";
cout << Mult << endl;
Mult = fixing*2;
cout << "Mult time = ";
cout << Mult << endl;
return 0;
}
5、
/*******************STONEWT.H*********************/
#pragma once
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum Mode { STONE, 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 stone_mode()
{
mode = STONE;
}
void pounds_mode()
{
mode = POUNDS;
}
//conversion function
operator int()const;
operator double()const;
Stonewt operator+(double l);
Stonewt operator+(int l);
Stonewt operator+(const Stonewt &s);
Stonewt operator*(double n);
Stonewt operator-(double l);
Stonewt operator-(int l);
Stonewt operator-(const Stonewt &l);
//friends
friend std::ostream &operator << (std::ostream &os, const Stonewt &s);
friend Stonewt operator*(double n, const Stonewt &t)
{
return Stonewt(t.pounds*n);
}
friend Stonewt operator-(double n, const Stonewt &t)
{
return Stonewt(n-t.pounds);
}
friend Stonewt operator+(double a, const Stonewt &s)
{
return Stonewt(a + s.pounds);
}
};
#endif
/*******************STONEWT.CPP*********************/
#include<iostream>
#include"stonewt.h"
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs)
{
mode = POUNDS;
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
mode = POUNDS;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0.0;
mode = POUNDS;
}
Stonewt::~Stonewt()
{
}
Stonewt::operator int()const
{
return int(pounds + 0.5);
}
Stonewt::operator double()const
{
return pounds;
}
Stonewt Stonewt::operator-(const Stonewt &l)
{
return Stonewt(pounds - l.pounds);
}
Stonewt Stonewt::operator-(double l)
{
pounds -= l;
return Stonewt(pounds);
}
Stonewt Stonewt::operator-(int l)
{
pounds -= l;
return Stonewt(pounds);
}
Stonewt Stonewt::operator+(const Stonewt &l)
{
return Stonewt(pounds + l.pounds);
}
/*
Stonewt Stonewt::operator+(const Stonewt &s)
{
Stonewt sum;
sum.pounds = pounds + s.pounds;
sum.pds_left = int(sum.pounds) %Lbs_per_stn + sum.pounds - (int)sum.pounds;
sum.stone = sum.pounds / Lbs_per_stn;
return sum;
}
*/
Stonewt Stonewt::operator*(double n)
{
return Stonewt(n*pounds);
}
Stonewt Stonewt::operator+(double l)
{
pounds += l;
return Stonewt(pounds);
}
Stonewt Stonewt::operator+(int l)
{
pounds += l;
return Stonewt(pounds);
}
//friends
std::ostream &operator << (std::ostream &os, const Stonewt &s)
{
if (s.mode == Stonewt::POUNDS)
os << s.pounds << " pounds ;" << endl;
else if (s.mode == Stonewt::STONE)
os << s.stone << " stone " << s.pds_left << " pounds;" << endl;
else
os << "no correct methods";
return os;
}
/*******************STONE.CPP*********************/
#include<iostream>
#include"stonewt.h"
using std::cout;
using std::endl;
void display(const Stonewt & st, int n);
int main()
{
Stonewt incognito = 275;
Stonewt wolfe(285.7);
Stonewt taft(21, 8);
Stonewt mult;
cout << "The celebrity weighed";
cout << incognito;
cout << "The detective weighed ";
wolfe.stone_mode();
cout << wolfe;
cout << "The President weighed ";
mult = wolfe + taft;
mult.pounds_mode();
cout << mult;
incognito = 276.8;
taft = 325;
//member substraction function
cout << "member substraction function\n";
taft=taft-30;
cout << taft;
//friend subtraction method;
cout << "friend substraction function\n";
taft= 400.0-taft;
cout << taft;
//member multiplication function
cout << "member multiplication function\n";
mult= taft*3.0;
cout << mult << endl;
//friend multiplication function
cout << "friend multiplication function\n";
mult =3.0* taft ;
cout << mult ;
//member add function
mult = mult + 50;
cout << mult;
//friend add function
mult = 50.0+mult;
cout << mult;
cout << "Afer dinner, the President weighed ";
display(taft, 2);
cout << "become big: ";
mult = 2.0*mult;
display(mult,1);
cout << "The werstler weighed enen 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! ";
cout<<st;
}
}
/*****************COMPLEX.H*******************/
#pragma once
#ifndef COMPLEX_H_
#define COMPLEX_H_
#include<iostream>
using std::ostream;
using std::istream;
class Complex
{
private:
double A;
double B;
public:
Complex();
Complex(double x, double y);
~Complex();
void operator+(const Complex &c);
void operator-(const Complex &c);
void operator*(const Complex &c);
void operator*(double c);
void operator~();
double Xval()const { return A; }
double Yval()const { return B; }
//friends
friend ostream & operator<< (ostream &os, const Complex &c);
friend istream & operator>>(istream &is, Complex &c);
friend Complex operator*(double c,Complex &x);
};
#endif // !COMPLEX_H_
/*****************COMPLEX.CPP*******************/
#include<iostream>
#include"complex.h"
#include<cmath>
using std::endl;
using std::cout;
using std::cin;
Complex::Complex()
{
A = B = 0.0;
}
Complex::Complex(double x, double y)
{
A = x;
B = y;
}
Complex::~Complex()
{
}
void Complex::operator+(const Complex &c)
{
A = A + c.A;
B = B + c.A;
}
void Complex::operator-(const Complex &c)
{
A = A - c.A;
B = B - c.A;
}
void Complex::operator*(const Complex &c)
{
double x, y;
x = A;
y = B;
A = x*c.A-y*c.A;
B = x*c.B+y*c.A;
}
void Complex::operator*(double c)
{
A = c * A;
B = c * B;
}
ostream & operator<< (ostream &os, const Complex &c)
{
os << "(" << c.Xval() << " , " << c.Yval() << "i)" << endl;
return os;
}
istream & operator>>(istream &is, Complex &c)
{
cout << "real: ";
is >> c.A;
cout << "\n";
cout << "imaginary: ";
is >> c.B;
return is;
}
Complex operator*(double c, Complex &x)
{
return Complex(c*x.A,c*x.B);
}
void Complex::operator~()
{
A = A;
B = -B;
}
/*****************MAINER.CPP*******************/
#include<iostream>
#include"complex.h"
using std::endl;
using std::cin;
using std::cout;
int main()
{
Complex X(1, 2);
Complex Y(3, 4);
Complex Z;
cout << "Enter a complex number (q to quit):\n";
while (cin >> Z)
{
cout << "Z is " << Z << '\n';
//cout <<"complex conjugate is "<<
cout << "X is " << X << '\n';
X + Y;
cout << "X + Y is " << X << '\n';
X - Y;
cout << "X - Y is " << X << '\n';
X * Y;
cout << "X * Y is " << X << '\n';
5.0*X ;
cout << "5.0 * X is" << X << '\n';
~X;
cout << "~X is" << X << '\n';
cout << "Enter a complex number (q to quit):\n";
}
cout << "done\n";
return 0;
}