c++学习日记之使用类来模拟随机漫步

本文主要介绍运算符重载和友元的类设计,通过定义一个矢量类来模拟随机漫步。矢量类分两种模式,直角坐标模式和极坐标模式。该函数允许用户选择行走的距离和补偿。该程序用一个变量表示位置,并报告到达指定距离住所需的部署。
本文的重点是是理解运算符重载和友元函数
代码如下:
vector.h文件
#ifndef VECTOR_H_
#define VECTOR_H_
#include
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode{RECT,POL};
		// RECT  for rectangular ,POL for ploar 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_mage();
		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 * (double n) const;
		Vector operator - () const;
		//friends
		friend Vector operator * (double n,const Vector &a);
		friend std::ostream & operator <<(std::ostream & os,const Vector &v);
	};
}

#endif


vector.cpp文件
#include
#include"vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
	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_mage()
	{
		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,y 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=RECT;
	}
	Vector::Vector(double n1,double n2,Mode form)
	{
		mode = form;
		if(form==RECT)
		{
			x=n1;
			y=n2;
			set_mage();
			set_ang();
		}
		else if(form==POL)
		{
			mag=n1;
			ang=n2;
			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;
		}
	}
	void Vector::reset(double n1,double n2,Mode form)
	{
		mode = form;
		if(form==RECT)
		{
			x=n1;
			y=n2;
			set_mage();
			set_ang();
		}
		else if(form==POL)
		{
			mag=n1;
			ang=n2;
			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()
	{

	}
	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*(double n) const
	{
		return Vector(n*x,n*y);
	}
	Vector Vector::operator - () const
	{
		return Vector(-x,-y);
	}
	Vector operator *(double n,const Vector &a)
	{
		return a*n;
	}
	std::ostream & operator << (std::ostream &os,const Vector &v)
	{
		if (v.mode==Vector::RECT)
		{
			os << "(x,y) = (" <


main()函数
#include 
#include 
#include
#include "vector.h"
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()


运行结果如下:
 
 

你可能感兴趣的:(c/c++)