一、平面向量类
#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;
}