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;
}