C++ 实现向量

该博客介绍了如何使用C++编写一个向量类,支持直角坐标(RECT)和极坐标(POL)两种表示,并实现了向量的基本运算,包括加、减、负号和乘法运算。通过重载运算符,使得向量操作更加便捷。示例代码展示了如何创建和使用这些向量对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gaoqizhong7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值