C++学习日志47-----平面向量类、重载运算符


一、平面向量类


#include<iostream>
#include"Vec2D.h"
using std::cout;
using std::endl;
int main()
{
    //创建向量对象
    Vec2D v1{ 3,5 }, v2{ 4,6 };

    //向量转为字符串
    cout << "v1 = " << v1.toString() << endl;
    cout << "v2= " << v2.toString() << endl;

    //向量加法:向量+向量,向量+数
    Vec2D v3 = v1 + v2;
    Vec2D v4 = v3 + 10.0;
    cout << "v3 = " << v3.toString() << endl;
    cout << "v4 = " << v4.toString() << endl;
    //向量减法、向量点积,向量数乘
    Vec2D v5 = v2 - (v1);
    double v6 = v2 * (v1);
    Vec2D v7 = (2.1) * v3;
    cout << "v2-v1=" << v5.toString() << endl;
    cout << "v2.v1=" << v6 << endl;
    cout << "v3*2.1" << (v3 * 2.1).toString() << endl;
    cout << "2.1*v3 = " << v7.toString() << endl;

    Vec2D va1{ 10,12 }, va2{ 1,2 };
    cout << "va1+=va2 :" << (va1 += va2).toString() << endl;
    cout << "va1-=va2 :" << (va1 -= va2).toString() << endl;

    //向量求负值 
    Vec2D v8 = v2.negative();
    cout << "-v2=" << (-v2).toString() << endl;
    //向量自增/自减
    cout << "++v8 = " << (++v8).toString() << endl;
    cout << "v8++ = " << (v8++).toString() << endl;
    cout << "v8 = " << (v8).toString() << endl;
    cout << "--v2 = " << v2.decrease().toString() << endl;
    //读取或者修改向量元素
    v1[0] = 31.1;
    cout << "v1.x_ = " << v1[0] << endl;
    cout << "v1.y_ = " << v1[1] << endl;
    //v1[8] = 10; 进入异常
    //向量的长度magnitude 和角度direction
    cout << "v1.magnitude = " << v1.magnitude() << endl;
    cout << "double(v1)" << static_cast<double>(v1) << endl;

    cout << "v1.direction = " << v1.direction() << endl;
    //比较两个向量
    cout << "v1. compare v2 : " << v1.compareTo(v2) << endl;
    cout << "Please input a Vec2D: ";
    Vec2D v9{};
    std::cin >> v9; //operator>>(std::cin,v9);
    cout << v9 << endl;

    return 0;
}

在这里插入图片描述

结果如上图所示。

二、相关文件

//Vec2D.h
#pragma once
#include<iostream>
#include<string>
#include<cmath >
#include<ostream>
#include<istream>
class Vec2D
{
public:
	Vec2D();
	Vec2D(double, double);
	~Vec2D();
private:
	double x_;
	double y_;
public:
	// 将向量转化为字符串形式表示
	std::string toString();
	// 向量加法
	Vec2D add(const Vec2D& secondVec2D);
	Vec2D add(double numeral);
	Vec2D operator+(const Vec2D& secondVec2D);
	Vec2D operator+(const double numeral);
	Vec2D& operator +=(const Vec2D& secondVec2D);
	//读取或者修改向量元素
	double& at(const int index);
	double& operator[](const int& index);


	int compareTo(Vec2D secondVec2D);
	Vec2D& decrease();
	double direction();
	double dot(const Vec2D& secondVec2D);
	Vec2D& increase();
	Vec2D& operator++();
	Vec2D operator++(int dummy);

	double magnitude();
	operator double();
	//向量数乘
	Vec2D multiply(double multiplier);
	double operator *(const Vec2D& secondVec2D);
	Vec2D operator*(double multiplier);
	friend Vec2D operator*(double multiplier, Vec2D  vec2d);


	Vec2D negative();
	Vec2D operator-();
	//向量减法
	Vec2D subtract(const Vec2D& secondVec2D);
	Vec2D subtract(double numeral);
	Vec2D operator-(const Vec2D& secondVec2D);
	Vec2D operator-(double numeral);
	Vec2D operator-=(const Vec2D& secondVec2D);

 friend std::ostream& operator<<(std::ostream& os, const Vec2D &v);
 friend std::istream& operator>>(std::istream& is,Vec2D& v);
};
//Vec2D.c
#include "Vec2D.h"
#include "Vec2D.h"
Vec2D::Vec2D()
{
	x_ = 0.0;
	y_ = 0.0;
}
Vec2D::Vec2D(double x,double y)
{
	x_ = x;
	y_ = y;
}

Vec2D::~Vec2D()
{  

}

// 将向量转化为字符串形式表示
std::string Vec2D::toString()
{
	// TODO: 在此处添加实现代码.
	return std::string("("+std::to_string(x_)+","+std::to_string(y_)+")");
}


// 向量加法
Vec2D Vec2D::add(const Vec2D& secondVec2D)
{
	// TODO: 在此处添加实现代码.
	return Vec2D(x_+secondVec2D.x_,y_+secondVec2D.y_);
}

//向量和数值加法
Vec2D Vec2D::add(double numeral)
{
	return Vec2D(this->x_ + numeral, this-> y_ + numeral);

}

Vec2D Vec2D::operator+(const Vec2D& secondVec2D)
{
	return this->add(secondVec2D);
}
Vec2D Vec2D::operator+(const double numeral)
{
	return this->add(numeral);


}

Vec2D& Vec2D::operator +=(const Vec2D& secondVec2D)
{
	x_ += secondVec2D.x_;
	y_ += secondVec2D.y_;
	return (*this);
}


double& Vec2D::at(const int index)
{

	// TODO: 在此处添加实现代码.
	if (0 == index) return x_;
	else if (1 == index) return y_;
	else throw std::out_of_range("at() only accept 1 or 2 as paramater");
	
}
double& Vec2D::operator[](const int& index)
{
	return  at(index);
}

int Vec2D::compareTo(Vec2D secondVec2D)
{
	// TODO: 在此处添加实现代码.
	double m1 = this->magnitude();
	double m2 = secondVec2D.magnitude();
	if (abs(m1 - m2) < 1e-10) return 0;
	else return(m1 > m2 ? 1 : -1);
	
}


Vec2D& Vec2D::decrease()
{
	// TODO: 在此处添加实现代码.
	x_--; y_--;
	return *this;
}


double Vec2D::direction()
{
	// TODO: 在此处添加实现代码.
	
	return atan(y_/x_);
}


double Vec2D::dot(const Vec2D& secondVec2D)
{
	// TODO: 在此处添加实现代码.
	return (x_*secondVec2D.x_+y_*secondVec2D.y_);
}


Vec2D& Vec2D::increase()
{
	// TODO: 在此处添加实现代码.
	x_ ++ ; y_++;
	return (*this);
}

Vec2D& Vec2D::operator++()
{
	// TODO: 在此处添加实现代码.
	x_++; y_++;
	return (*this);

}
Vec2D Vec2D:: operator++(int dummy)
{
	Vec2D temp{ *this };
	x_++; y_++;
	return temp;
}

Vec2D:: operator double()
{
	return this->magnitude();


}


//求向量的范数(长度)
double Vec2D::magnitude()
{
	// TODO: 在此处添加实现代码.
	return sqrt(x_*x_+y_*y_);
}

//向量数乘
Vec2D Vec2D::multiply(double multiplier)
{
	// TODO: 在此处添加实现代码.
	
	return Vec2D(x_*multiplier,y_*multiplier);
}
double Vec2D::operator*(const Vec2D& secondVec2D)
{
	return this->dot(secondVec2D);
}
Vec2D Vec2D::operator*(double multiplier)
{
	return this->multiply(multiplier);
}


Vec2D operator*(double multiplier, Vec2D  vec2d)
{
	return vec2d.multiply(multiplier);

}



Vec2D Vec2D::negative()
{
	return Vec2D(-x_,-y_);
}

Vec2D Vec2D::operator-()
{
	return Vec2D(-this->x_, -this->y_);

}
//向量减法
Vec2D Vec2D::subtract(const Vec2D& secondVec2D)
{

	return Vec2D(x_-secondVec2D.x_,y_-secondVec2D.y_);
}
//向量减去数值
Vec2D Vec2D::subtract(double numeral)
{

	return Vec2D(x_ - numeral, y_ - numeral);
}
Vec2D Vec2D::operator-(const Vec2D& secondVec2D)
{
	return this->subtract(secondVec2D);
}
Vec2D Vec2D::operator-(double numeral)
{
	return this->subtract(numeral);
}


Vec2D  Vec2D::operator-=(const Vec2D& secondVec2D)
{
	x_ -= secondVec2D.x_;
	y_ -= secondVec2D.y_;
	return (*this);
}

std::ostream& operator<<(std::ostream& os,const Vec2D & v)
{
	os << "(" << v.x_ << " ," << v.y_ << ")";
	return os;
}
std::istream& operator>>(std::istream& is, Vec2D& v)
{
	is >> v.x_ >> v.y_;
	return is;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@白圭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值