使用 C++ 实现向量,并重载运算符 对其进行基本运算;
将以下四个文件放入同一文件夹中,对 main.cpp 执行编译即可;
本实现支持向量的两种表示:RECT(直角坐标表示)、POL(极坐标制),分别对应参数(x, y)、(mag,ang),其中 mag 为模长、ang为弧度(而非角度);
PS:本实现基于《C++ Primer Plus》提供的方法并略有改动,评论区欢迎各位大佬提问、指正
Vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
using std::ostream;
class Vector
{
public:
enum Mode{RECT, POL}; // RECT for rectangular, POL for polar modes
private:
double x;
double y;
double mag; // length
double ang; // direction in radian
Mode mode; // RECT or POL
void set_x_y();
void set_mag_ang();
public:
Vector(); // RECT mode is used by default
Vector(double n1, double n2, Mode form = RECT); // RECT mode is used by default
void reset(double n1, double n2, Mode form = RECT); // RECT mode is used by default
void setmode(Mode form = RECT); // RECT mode is used by default
// OPOL: operator overloading
Vector operator+(const Vector &b) const;
Vector operator-(const Vector &b) const;
Vector operator-() const;
Vector operator*(double n) const;
Vector operator*(const Vector &b) const;
friend Vector operator*(double n, const Vector &b);
friend ostream &operator<<(ostream &os, const Vector &v);
};
#endif
Vector.cpp
#include <iostream>
#include <cmath>
#include "Vector.h"
const double Rad_to_deg = 45.0 / atan(1.0);
void Vector::set_x_y()
{
x = mag * cos(ang);
y = mag * sin(ang);
}
void Vector::set_mag_ang()
{
mag = sqrt(x * x + y * y);
if (x == 0.0 && y == 0.0)
ang = 0.0;
else
ang = atan2(y, x);
}
Vector::Vector()
{
x = y = mag = ang = 0.0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form)
{
if(form == POL)
{
mode = POL;
mag = n1;
ang = n2 / Rad_to_deg;
set_x_y();
}
else
{
mode = RECT;
x = n1;
y = n2;
set_mag_ang();
}
}
void Vector::reset(double n1, double n2, Mode form)
{
if(form == POL)
{
mode = POL;
mag = n1;
ang = n2 / Rad_to_deg;
set_x_y();
}
else
{
mode = RECT;
x = n1;
y = n2;
set_mag_ang();
}
}
void Vector::setmode(Mode form)
{
if(form == POL)
mode = POL;
else
mode = RECT;
}
OPOL.cpp
// operator overloading
#include <iostream>
#include "Vector.h"
using std::ostream;
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(n * x, n * y);
}
Vector Vector::operator*(const Vector &b) const
{
return Vector(x * b.x, y * b.y);
}
Vector operator*(double n, const Vector &b)
{
return b * n; // call the method in the class
}
ostream &operator<<(ostream &os, const Vector &v)
{
os << "(x, y) = (" << v.x << ", " << v.y << ")\n"
<< "mag = " << v.mag << " ang = " << v.ang << "\n";
return os;
}
main.cpp
#include <iostream>
#include "Vector.h"
#include "Vector.cpp"
#include "OPOL.cpp"
using namespace std;
int main()
{
Vector V1 = {};
Vector V2 = {10.0, 37.0, Vector::POL};
cout << "V1:" << endl;
cout << V1;
cout << "V2:" << endl;
cout << V2;
cout << "V1.reset(3.0, 4.0):" << endl;
V1.reset(3.0, 4.0);
cout << V1;
cout << "V1 * 2:" << endl;
cout << V1 * 2;
cout << "V1 + V2:" << endl;
cout << V1 + V2;
cout << "V1 - V2:" << endl;
cout << V1 - V2;
cout << "V1 * V2:" << endl;
cout << V1 * V2;
while(true)
cin.get();
return 0;
}