CPP学习笔记(自用)

2023/9/24

自己写了写书中的vector类

vector.h

#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <iostream>
using namespace std;
namespace VECTOR
{

  class Vector
  {
  public:
    enum Mode
    {
      RECT, // 直角坐标系
      POL   // 极坐标系
    };

  private:
    double x; // 坐标
    double y;
    double length; // 长度和角度
    double ang;
    Mode mode;

    void set_x();
    void set_y();
    void set_length();
    void set_ang();

  public:
    Vector();
    Vector(double n1, double n2, Mode form = RECT); // 构造函数,默认使用直角坐标系
    void reset(double n1, double n2, Mode form = RECT);
    ~Vector();

    double xVal() const { return x; }
    double yVal() const { return y; }
    double lengthVal() const { return length; }
    double angVal() const { return ang; }
    void polMode();
    void rectMode();

    // 运算符重载
    Vector operator+(const Vector &b) const;
    Vector operator-(const Vector &b) const;
    Vector operator-() const;
    Vector operator*(double n) const;

    // 友元
    friend Vector operator*(double n, const Vector &b);
    friend ostream &operator<<(ostream &os, const Vector &v);
  };

}

#endif

vector.cpp

#include <cmath>
#include "vector.h"
using namespace std;
namespace VECTOR
{
  const double RadToDeg = 45.0 / atan(1.0);

  void Vector::set_length()
  {
    length = sqrt(x * x + y * y);
  }

  void Vector::set_ang()
  {
    if (x == 0 && y == 0)
      ang == 0;
    else
      ang = atan2(y, x);
  }

  void Vector::set_x()
  {
    x = length * cos(ang);
  }
  void Vector::set_y()
  {
    y = length * sin(ang);
  }
  // 构造函数
  Vector::Vector()
  {
    x = y = length = ang = 0.0;
    mode = RECT;
  }
  Vector::Vector(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1, y = n2;
      set_length();
      set_ang();
    }
    else if (form = POL)
    {
      length = n1, ang = n2 / RadToDeg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect,vector has set to 0." << endl;
      form = RECT;
      x = y = length = ang = 0;
    }
  }
  void Vector::reset(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1, y = n2;
      set_length();
      set_ang();
    }
    else if (form = POL)
    {
      length = n1, ang = n2 / RadToDeg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect,vector has set to 0." << endl;
      form = RECT;
      x = y = length = ang = 0;
    }
  }

  // 析构函数
  Vector::~Vector()
  {
    cout << "Vector has been destoryed." << endl;
  }

  void Vector::polMode()
  {
    mode = POL;
  }
  void Vector::rectMode()
  {
    mode = RECT;
  }

  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(x * n, y * n);
  }
  Vector operator*(double n, const Vector &b)
  {
    return b * n;
  }
  ostream &operator<<(ostream &os, const Vector &v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << " , " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.length << ", " << v.ang * RadToDeg << ")";
    }
    else
      os << "Vector object mode is invalid";
    return os;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值