c++实现三维向量的操作

实现一个三维矢量库,要求

1 计算矢量的模长,夹角,内积和外积

2比较矢量的大小,重载==、!=、>=、<=、<、>

3实现矢量与数值的加减乘除

4实现矢量之间的加减

5包含拷贝构造函数和=重载

6重载<<和>>函数

 

说明:const对象只能调用const成员函数。

类头文件:

 

#ifndef MY_HEADER_
#define MY_HEADER_
#include<iostream>
typedef struct myvec{
	int x;
	int y;
	int z;
}vec;

class Myvec
{
public:
	Myvec();
	~Myvec();
	Myvec::Myvec(const Myvec& p);
	Myvec(int x, int y, int z);
	friend std::ostream& operator<<(std::ostream& out,Myvec& V);
	friend std::istream& operator>>(std::istream& in, Myvec& V);
	bool operator==(const Myvec& V);
	Myvec operator=(const Myvec& V);
	double cal_length() const;
	double cal_inner(const Myvec& V) const;
	double cal_angle(const Myvec& V) const;
	double cal_outer(const Myvec& V);
	Myvec Add(const Myvec& V);
	Myvec Del(const Myvec& V);
	Myvec By(int a);
	Myvec Div(int a);
private:
	vec v;
};


#endif

类源文件:

 

 

#include "myvec.h"
#include<iostream>
using namespace std;


Myvec::Myvec()
{
	
}

Myvec::Myvec(int x,int y,int z)
{
	
	this->v.x = x;
	this->v.y = y;
	this->v.z = z;
}

Myvec::Myvec(const Myvec& p)
{
	this->v.x = p.v.x;
	this->v.y = p.v.y;
	this->v.z = p.v.z;
}

Myvec::~Myvec()
{
}

double Myvec::cal_length() const
{
	double total = pow(this->v.x, 2) + pow(this->v.y, 2) + pow(this->v.z, 2);
	return sqrtf(total);
}

std::ostream& operator<<(std::ostream& out, Myvec& V)
{
	out << "(" << V.v.x << "," << V.v.y << "," << V.v.z << ")" << endl;
	return out;
}

std::istream& operator>>(std::istream& in, Myvec& V)
{
	cout << "输入格式:x y z:" << endl;
	in >> V.v.x >> V.v.y >> V.v.z;
	return in;
}

bool Myvec::operator==(const Myvec& V)
{
	return (this->v.x == V.v.x && this->v.y == V.v.y && this->v.z == V.v.z);
}

Myvec Myvec::operator=(const Myvec& V)
{
	this->v.x = V.v.x;
	this->v.y = V.v.y;
	this->v.z = V.v.z;
	return *this;
}

Myvec Myvec::Add(const Myvec& V)
{
	Myvec a;
	a.v.x = this->v.x + V.v.x;
	a.v.y = this->v.y + V.v.y;
	a.v.z = this->v.z + V.v.z;
	return a;
}
Myvec Myvec::Del(const Myvec& V)
{
	Myvec a;
	a.v.x = this->v.x - V.v.x;
	a.v.y = this->v.y - V.v.y;
	a.v.z = this->v.z - V.v.z;
	return a;
}
Myvec Myvec::By(int m)
{
	Myvec a;
	a.v.x = this->v.x * m;
	a.v.y = this->v.y * m;
	a.v.z = this->v.z * m;
	return a;
}
Myvec Myvec::Div(int m)
{
	if (!m)
	{
		cout << "除数不能为0!" << endl;
		return *this;
	}
	else
	{
		Myvec a;
		a.v.x = this->v.x/ m;
		a.v.y = this->v.y/ m;
		a.v.z = this->v.z/ m;
		return a;
	}
}

double Myvec::cal_inner(const Myvec& V) const
{
	return this->v.x*V.v.x + this->v.y*V.v.y + this->v.z*V.v.z;
}



double Myvec::cal_angle(const Myvec& V) const
{
	double cos = this->cal_inner(V) / (this->cal_length()* V.cal_length());
	return acosf(cos);
}

double Myvec::cal_outer( const Myvec& V)//const对象不可以调用非const成员函数,因此函数中调用的成员函数应设置为const
{
	return this->cal_length() * V.cal_length() * sinf(this->cal_angle(V));
}


主函数:

 

 

// excise3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "myvec.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	Myvec p1(4, 5, 6);
	Myvec p2(1, 2, 3);

	cout<<p1.Add(p2);//重载输出符
	cout << p1.By(3);
	cout << p1.Del(p2);
	cout << p1.Div(2);

	cout << p1.cal_inner(p2) << endl;//内积即点积
	cout << p1.cal_outer(p2) << endl;//外积即叉积
	cout << p1.cal_angle(p2) << endl;//求角度

	Myvec p3(p1);//拷贝构造函数
	cout << p3;

	p3 = p2;//重载赋值符
	cout << p3;

	Myvec p4;
	cin >> p4;//重载输出符
	cout << p4;


	cout << (p4 == p3)<<endl;//重载等于号,其他判断号类似,等于则输出1,否则0

	system("pause");
	return 0;
}

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值