Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >重载,一个矢量类

重载,一个矢量类

作者头像
用户8247415
发布于 2021-04-13 08:15:07
发布于 2021-04-13 08:15:07
42000
代码可运行
举报
文章被收录于专栏:网页前端网页前端
运行总次数:0
代码可运行

Vector

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include 
#include 
namespace VECTOR
{
    class Vector
    {
    public:
        enum Mode { RECT, POL };
        // RECT for rectangular, POL for Polar modes
    private:
        double x;      // horizontal value
        double y;      // vertical value
        double mag;    // length of vector
        double ang;    // direction of vector in degrees
        Mode mode;     // RECT or POL
    // private methods for setting values
        void set_mag();
        void set_ang();
        void set_x();
        void set_y();
    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; }      // report x value
        double yval() const { return y; }      // report y value
        double magval() const { return mag; } // report magnitude
        double angval() const { return ang; } // report angle
        void polar_mode();                      // set mode to POL
        void rect_mode();                       // set mode to RECT
   // operator overloading
        Vector operator+(const Vector& b) const;
        Vector operator-(const Vector& b) const;
        Vector operator-() const;
        Vector operator*(double n) const;
        // friends
        friend Vector operator*(double n, const Vector& a);
        friend std::ostream&
            operator<<(std::ostream& os, const Vector& v);
    };
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
    // compute degrees in one radian
    const double Rad_to_deg = 45.0 / atan(1.0);
    // should be about 57.2957795130823

    // private methods
    // calculates magnitude from x and y
    void Vector::set_mag()
    {
        mag = sqrt(x * x + y * y);
    }

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

    // set x from polar coordinate
    void Vector::set_x()
    {
        x = mag * cos(ang);
    }

    // set y from polar coordinate
    void Vector::set_y()
    {

        y = mag * sin(ang);
    }
// public methods
Vector::Vector() // default constructor
{
    x = y = mag = ang = 0.0;
    mode = RECT;
}

// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
Vector::Vector(double n1, double n2, Mode form)
{
    mode = form;
    if (form == RECT)
     {
        x = n1;
        y = n2;
        set_mag();
        set_ang();
    }
    else if (form == POL)
    {
        mag = n1;
        ang = n2 / Rad_to_deg;
        set_x();
        set_y();
    }
    else
    {
        cout << "Incorrect 3rd argument to Vector() -- ";
        cout << "vector set to 0\n";
        x = y = mag = ang = 0.0;
        mode = RECT;
    }
}

// reset vector from rectangular coordinates if form is
// RECT (the default) or else from polar coordinates if
// form is POL
void Vector:: reset(double n1, double n2, Mode form)
{
    mode = form;
    if (form == RECT)
     {
        x = n1;
        y = n2;
        set_mag();
        set_ang();
    }
    else if (form == POL)
    {
        mag = n1;
        ang = n2 / Rad_to_deg;
        set_x();
        set_y();
    }
    else
    {
        cout << "Incorrect 3rd argument to Vector() -- ";
        cout << "vector set to 0\n";
        x = y = mag = ang = 0.0;
        mode = RECT;
    }
}

Vector::~Vector() // destructor
{
}

void Vector::polar_mode() // set to polar mode
{
    mode = POL;
}

void Vector::rect_mode() // set to rectangular mode
{
    mode = RECT;
}

// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
    return Vector(x + b.x, y + b.y);
}

// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
    return Vector(x - b.x, y - b.y);
}

// reverse sign of Vector
    Vector Vector::operator-() const
    {
        return Vector(-x, -y);
    }

    // multiply vector by n
    Vector Vector::operator*(double n) const
    {
        return Vector(n * x, n * y);
    }

    // friend methods
    // multiply n by Vector a
    Vector operator*(double n, const Vector & a)
    {
        return a * n;
    }

    // display rectangular coordinates if mode is RECT,
    // else display polar coordinates if mode is POL
    std::ostream & operator<<(std::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.mag << ", "
                << v.ang * Rad_to_deg << ")";
        }
        else
            os << "Vector object mode is invalid";
        return os;
    }

} 
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
    // compute degrees in one radian
    const double Rad_to_deg = 45.0 / atan(1.0);
    // should be about 57.2957795130823

    // private methods
    // calculates magnitude from x and y
    void Vector::set_mag()
    {
        mag = sqrt(x * x + y * y);
    }

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

    // set x from polar coordinate
    void Vector::set_x()
    {
        x = mag * cos(ang);
    }

    // set y from polar coordinate
    void Vector::set_y()
    {

        y = mag * sin(ang);
    }
    // public methods
    Vector::Vector() // default constructor
    {
        x = y = mag = ang = 0.0;
        mode = RECT;
    }

    // construct vector from rectangular coordinates if form is r
    // (the default) or else from polar coordinates if form is p
    Vector::Vector(double n1, double n2, Mode form)
    {
        mode = form;
        if (form == RECT)
        {
            x = n1;
            y = n2;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            mag = n1;
            ang = n2 / Rad_to_deg;
            set_x();
            set_y();
        }
        else
        {
            cout << "Incorrect 3rd argument to Vector() -- ";
            cout << "vector set to 0\n";
            x = y = mag = ang = 0.0;
            mode = RECT;
        }
    }

    // reset vector from rectangular coordinates if form is
    // RECT (the default) or else from polar coordinates if
    // form is POL
    void Vector::reset(double n1, double n2, Mode form)
    {
        mode = form;
        if (form == RECT)
        {
            x = n1;
            y = n2;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            mag = n1;
            ang = n2 / Rad_to_deg;
            set_x();
            set_y();
        }
        else
        {
            cout << "Incorrect 3rd argument to Vector() -- ";
            cout << "vector set to 0\n";
            x = y = mag = ang = 0.0;
            mode = RECT;
        }
    }

    Vector::~Vector() // destructor
    {
    }

    void Vector::polar_mode() // set to polar mode
    {
        mode = POL;
    }

    void Vector::rect_mode() // set to rectangular mode
    {
        mode = RECT;
    }

    // operator overloading
    // add two Vectors
    Vector Vector::operator+(const Vector& b) const
    {
        return Vector(x + b.x, y + b.y);
    }

    // subtract Vector b from a
    Vector Vector::operator-(const Vector& b) const
    {
        return Vector(x - b.x, y - b.y);
    }

    // reverse sign of Vector
    Vector Vector::operator-() const
    {
        return Vector(-x, -y);
    }

    // multiply vector by n
    Vector Vector::operator*(double n) const
    {
        return Vector(n * x, n * y);
    }

    // friend methods
    // multiply n by Vector a
    Vector operator*(double n, const Vector& a)
    {
        return a * n;
    }

    // display rectangular coordinates if mode is RECT,
    // else display polar coordinates if mode is POL
    std::ostream& operator<<(std::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.mag << ", "
                << v.ang * Rad_to_deg << ")";
        }
        else
            os << "Vector object mode is invalid";
        return os;
    }
}

没有主函数,程序不完整。 如果是RECT则输出直角坐标 否则是POL极坐标 里面重载了乘法,除法,加减法 运用了友元 是一个重载的总结,值得好好分析 注重返回值 下一章是关于Vector的应用

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++ Primer Plus习题及答案-第十一章
成员函数是类定义的一部分,通过特定的对象来调用。成员函数可以隐式访问调用对象的成员,而无需使用成员运算符。友元函数不是类的组成部分,因此被称为直接函数调用。友元函数不能隐式访问类成员,而需要将成员运算符用作参数传递的对象。
艰默
2022/12/04
7130
C++ Primer Plus习题及答案-第十一章
EasyC++44,单独编译
头文件当中只能放一些生命和常量的定义,不能有具体的实现。所以我们要把具体的实现单独放入一个cpp文件当中。因为我们的头文件叫做coordin.h,那么我们与之对应的cpp文件自然就叫做coordin.cpp。
TechFlow-承志
2022/08/26
3460
BZOJ1278: 向量vector(计算几何 随机化乱搞)
首先我们可以按极角排序。然后对\(y\)轴上方/下方的加起来分别求模长取个最大值。。
attack
2019/03/04
3310
Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:standard input output:standard output We will not waste your time, it is a straightforward problem. Given multiple polygons, calculate the area of their interse
Angel_Kitty
2018/04/09
8030
Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题
运算符重载(看完这篇彻底掌握重载问题)
运算符重载:用同一个运算符完成不同的运算功能。 C++运算符重载的相关规定如下:
海盗船长
2020/08/27
9.8K1
Flutter之旅:从源码赏析Dart面向对象
前言 相信大家都是有过面向对象经验的人,那面向对象是什么感觉呢? 大概也就是一开始心跳加速,小鹿乱撞,之后平淡无奇,最后被她折磨到怀疑人生。 今天给你介绍个对象,她的名字叫Dart,还等什么,赶
张风捷特烈
2020/04/30
1K0
Flutter之旅:从源码赏析Dart面向对象
UVA - 11178 Morley's Theorem
按照刘汝佳老师说的,这道题本身没有什么算法可言, 主要是考察选手对于几何算法的应用, 我们已经知道了点A,B,C 如果要求点D的话 我们可以先求出向量C-B的坐标,然后求出向量C-B与A-B的夹角。 再把夹角/3,这样我们就找到了∠CBD的度数。 再把向量C-B逆时针旋转∠CBD度 就求出了点D的坐标, E,F同理 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #defi
attack
2018/04/12
6520
UVA - 11178 Morley's Theorem
c++ 头文件
可以将程序分为二部分: 头文件:包含结构声明和使用这些结构的函数的原型 源代码文件: 包含与结构有关的函数的代码 不要将函数的定义或变量的声明放在头文件里, 一般头文件可以包含以下内容 >函数原型 >使用#define或const定义的符号常量 >结构声明 >类声明 >模板声明 >内联函数 在包含头文件时我们使用 #include "head.h"而不是#include <head.h> 如果使用<>  c++编译器将在存储标准头文件的主机系统的文件系统中查找; 如果使用""   编译器会首先查找当前的工作
lpxxn
2018/01/31
1.7K0
向量的加减(输出重载)
设向量X=(x1,x2,…,xn)和Y=(y1,y2…,yn),它们之间的加、减分别定义为:
叶茂林
2023/07/30
2010
C++ 自定义复数类
C++练习。 功能:自定义复数类型,实现复数的加、减、乘、除、求共轭复数、乘方、开方等运算。 涉及到的基础知识点有: 运算符重载(+,-,*,/, <<, ^, ==, != 等运算符的重载) 友元函数(友元函数可访问类的私有属性) 函数返回指向数组的指针。此例中数组的元素是类的对象。 左值引用与右值引用 主动抛出异常(使用关键字throw) #include <iostream> #include <cmath> using namespace std; class Division_by_zer
用户6021899
2021/05/20
1.4K0
3_机械臂位姿变换计算过程代码
有了这部分代码,可以进一步说明与验证该接口。cur_pose是机械臂基于基坐标系的位置和姿态,毫米和弧度为单位,即p_from参数。对于target_pose参数,是对p_from进行的位置和姿态的变换,例子中target_pose表示位置不变,绕ry旋转1弧度。输出结果:
用户5908113
2024/06/17
1840
3_机械臂位姿变换计算过程代码
一个多边形内部有3枚钉子_多边形的内部和外部
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 24 Accepted Submission(s) : 7 Problem Description Statement of the Problem Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points. You’re expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.
全栈程序员站长
2022/09/22
5910
【第10题】题解及代码分享:ABC329-F - Colored Ball
小码匠
2023/11/21
3140
【第10题】题解及代码分享:ABC329-F - Colored Ball
Codeforces Beta Round #1 A,B,C
A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's a
Angel_Kitty
2018/04/08
7640
Codeforces Beta Round #1 A,B,C
UVA 1396_UVC和UVA
大家好,又见面了,我是你们的朋友全栈君。 书上的题目,开始跟着新的大神了= =
全栈程序员站长
2022/09/22
3570
基于误差状态卡尔曼滤波惯性导航理论
一般有两个坐标系:大地基准坐标系w系(或者G系)与机器人本体坐标系b系(或者I系),两坐标系之间的旋转矩阵表示为:
3D视觉工坊
2023/04/30
6650
基于误差状态卡尔曼滤波惯性导航理论
【智能指针】—— 我与C++的不解之缘(三十三)
在异常学习中,我们的解决方法就是在test函数中捕获Divide函数抛出的异常,进行资源的释放再将异常重新抛出。
星辰与你
2025/04/11
860
【智能指针】—— 我与C++的不解之缘(三十三)
Halcon实例转OpenCV:计算回形针方向
Halcon中有一个计算回形针方向的实例clip.hdev,可以在例程中找到。原图如下:
Color Space
2020/08/21
1.3K0
Halcon实例转OpenCV:计算回形针方向
计算几何之圆与圆的交点
计算圆与圆的交点,需要用到余弦定理 步骤如下: 求出两个圆的圆心距d 求出向量c2.c-c1.c与c1.c到某交点的向量夹角a 求出向量c2.c-c1.c与x轴的夹角t 那么,两个交点就分别是以c1.c为起点,大小为c1.r,角度为t+a、t-a的两个向量 题目:CGL_7_E AC代码: #include <iostream> #include <cstdio> #include <math.h> using namespace std; #define COUNTER_CLOCKWISE -1 /
灯珑LoGin
2022/10/31
7140
计算几何之圆与圆的交点
(转载非原创)C++运算符重载介绍
C ++ 中预定义的运算符的操作对象只能是基本数据类型。但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作。这时就必须在C ++ 中重新定义这些运算符,赋予已有运算符新的功能,使它能够用于特定类型执行特定的操作。运算符重载的实质是函数重载,它提供了C ++ 的可扩展性,也是C ++ 最吸引人的特性之一。
xlj
2021/07/17
8550
相关推荐
C++ Primer Plus习题及答案-第十一章
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验