C++类的实例:一个向量类(Vector)

 

背景知识:

在数学中,向量(也称为欧几里得向量、几何向量、矢量),指具有大小(magnitude)和方向的量.

百度百科--向量

本例实现一个二维向量。二维向量通常表示的方法有 直角坐标(x,y)和极坐标(a,m)   // (a为长度,m为角度)。

创建一个类首先考虑类包含的数据。

一个向量(Vector)可以使用两个double表示直角坐标的x,y;用两个double表示极坐标的a,m。

还需要一个东西用于存储向量的形式(直角or极坐标)。使用枚举enum 。

则有:

	public:
		enum Mode{RECT,POL};
private:
		double x;
		double y;
		double mag;
		double ang;
		Mode mode;

接着考虑对向量的操作,一个类最基本的 应该有构造函数和析构函数。

在创建构造函数时,我们遇到了一个问题,由于同一个向量有两个形式,那么创建时应该怎么做呢?

最容易想到的办法就是选择其一,如直角坐标。那么,构造函数应该像是这种形式,

  Vector(double n1, double n2); 

然后让x=n1,y=n2;这样就可以设置向量的直角坐标,但是,极坐标就被忽略了。

考虑到向量的直角坐标和极坐标是一一对应的,于是想让程序自动设置对应的极坐标。

在Vector类中添加函数(在private中,因为不需要给外部提供接口)

		void set_mag();
		void set_ang();

具体实现为:

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

 

然而,如果用户想要用极坐标形式输入向量,当前的构造函数无法满足需求。

于是,改造构造函数,添加一个参数 form 用来选择形式。

Vector(double n1, double n2, Mode form= RECT);

相应的,构造函数的实现改为:

	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";
			x = y = mag = ang = 0;
			mode = RECT;
		}
	}

相应的,在Vector类添加set_x();set_t()方法。

	void set_x();
	void set_y();
	void Vector::set_x()
	{
		x = mag * cos(ang);
	}
	void Vector::set_y()
	{
		y = mag * sin(ang);
	}

如此,完成了构造函数。

析构函数使用默认的即可。或者自己加上一个构造函数        ~Vector(); 然后在对应的实现函数函数体中留下空格即可。

                                                                                                Vector::~Vector()   { }
 

完成了构造函数和析构函数,考虑向量需要的其他功能/接口。

两个向量相加减,向量的数乘,改变向量的坐标。输出向量。

获取向量的坐标值(提供接口才可访问,否则在private中不能访问)

设置向量的形式

......

具体如下:

		void reset(double n1, double n2, Mode form = RECT);
		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();
		//operator
		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);

 

实现:

	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";
			x = y = mag = ang = 0;
			mode = RECT;
		}
	}

	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	//operator
	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(x*n, y*n);
	}
	//friends
	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) = (" << 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;
	}

至此已经完成了Vector类的定义。(借助了cmath库进行了坐标两种形式转换)

完整代码:

//vector.h
#pragma once
#include
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode{RECT,POL};
	private:
		double x;
		double y;
		double mag;
		double ang;
		Mode mode;
		void set_mag();
		void set_ang();
		void set_x();
		void set_y();
	public:
		Vector();
		Vector(double n1, double n2, Mode form= RECT);
		~Vector();
		void reset(double n1, double n2, Mode form = RECT);
		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();
		//operator
		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);
	};
}
#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);

	//private methods
	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);
	}
	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_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";
			x = y = mag = ang = 0;
			mode = RECT;
		}
	}
	Vector::~Vector()
	{
	}
	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";
			x = y = mag = ang = 0;
			mode = RECT;
		}
	}

	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	//operator
	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(x*n, y*n);
	}
	//friends
	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) = (" << 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;
	}
}

 

 

 

你可能感兴趣的:(C++,C,类实例:向量Vector)