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

来源:互联网 发布:r语言导入csv数据 编辑:程序博客网 时间:2024/06/06 02:55

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

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,否则0system("pause");return 0;}