使用程序模拟醉鬼走路问题,将一个人领到街灯柱下,这个人开始走动,但每一步的方向都是随机的,
。这个问题的一种表述是,这个人走到离柱灯50英尺处需要多少步。从矢量角度看,这相当于不断将方向
随机的矢量相加,直至长度超过50英尺。
randwalk.cpp
//randwalk.cpp -- using the Vector class
// compile with the Vector.h
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "vector.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand ( time ( 0 ) );
double direction;
Vector step;
Vector result;
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance ( q to quit);";
while ( cin >> target)
{
result.set(0, 0,'r');
cout << "Enter step length: ";
if(!(cin >> dstep))
break;
while ( result.magval() < target)
{
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.polar_mode();
cout << "or \n" << result << endl;
cout << "Average outward distance per step = "<< result.magval()/steps << endl;
steps = 0;
result.set(0.0, 0.0 );
cout << "Enter target distance (q to quit) :";
}
cout << "Bye!\n";
return 0;
}
这个小程序使用修改后的Vector类
Vector.cpp
#include <cmath>
#include "vector.h"
using std::sin;
using std::cos;
using std::atan2;
using std::cout;
namespace VECTOR
{
const double Rad_to_deg = 57.2957795130823;
void Vector::set_mag()
{
mag = sqrt(x * x + y * y);
}
void Vector::set_ang()
{
if(x == 0 || y == 0)
ang = 0.0;
else ang = atan2 (y ,x);
}
//set x from polar coordinate
void Vector::set_x()
{
x = mag * cos (ang);
}
void Vector::set_y()
{
y = mag * sin (ang);
}
//public methods
Vector::Vector()
{
x = y = mag = ang = 0.0;
mode = 'r';
}
//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, char form)
{
mode = form;
if( form == 'r')
{
x =n1;
y = n2;
set_mag();
set_ang();
}
else if(form == 'p')
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect srd argument to Vector()--";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = 'r';
}
}
//set vector from rectangle coordinates if form is r
//(the default) or else from polar coordinates if form is p
void Vector::set (double n1, double n2 ,char form)
{
mode = form;
if(form == 'r')
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if(form == 'p')
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect srd argument to Vector()--";
cout << "vector set to 0\n";
x = y = mag = ang =0.0;
mode = 'r';
}
}
Vector::~Vector()// sestructor
{
}
void Vector::polar_mode()//成员函数可以访问私有成员变量
{
mode = 'p';
}
void Vector::rect_mode()
{
mode = 'r';
}
// operator overloading
// add two vectors
Vector Vector::operator+ (const Vector & b)const
{
return Vector (x + b.x,y + b.y,'r');//根据需要做了一下修改,如果不将这里的第三个参数设置成'r'的话,调用的函数将会重置Vector对象的数据。
}
//substract 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);
}
//multiple vector by n
Vector Vector::operator* (double n)const
{
return Vector (n * x, n * y);
}
//friend methods
//multiple n by vector a
Vector operator* (double n, const Vector & a)
{
return a * n;
}
//display rectangle coordinates if mode is r,
// else display polar coordinates if mode is p
std::ostream & operator<< (std::ostream & os, const Vector & v)
{
if(v.mode == 'r')
os << "(x, y) = (" << v.x <<","<< v.y << ")";
else if(v.mode== 'p')
{
os << " (m,a) = (" << v.mag << ", " << v.ang*Rad_to_deg<<")";
}
else
os << "Vector object mode is invalid ";
return os;
}
}// end namespace VECTOR
vectot.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
private:
double x;
double y;
double mag;
double ang;
char mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1,double n2, char form = ' r ');
void set(double n1,double n2, char form = 'r');
~Vector();
double xval()const { return x; }
double yval()const { return y; }
double magval()const { return mag;}
double angval()const {return ang;}
void polar_mode();
void rect_mode();
Vector operator+ (const Vector & b) const;
Vector operator- (const Vector & c) 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);
};
}//end namespcae VECTOR
#endif
实验结果与分析
进行1000次试验(走50英尺,步长为2英尺)时,平均步数为636(与理论值625非常接近),但实际步数位于91~3951之间。同样,进行1000次试验(走50英尺,步长为1英尺)时,平均步数为2557(与理论值2500非常接近),但实际步数位于345~10882之间。因此,如果发现自己在随机行走时,请保持自信,迈大步走。虽然在晚宴前进的过程中仍旧无法控制前进的方向,但至少会走得远一点(这句话在某些场合还是很有道理的)。