news 2026/5/28 1:04:29

6.再谈重载:一个矢量类

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
6.再谈重载:一个矢量类

6.再谈重载:一个矢量类

位移矢量指的是从何处开始到何处结束,而不是经过的路线。

VECTOR.h

#pragma once #ifndef VECTOR_H__ #define VECTOR_H__ #include <iostream> using namespace std; namespace Vector { class VECTOR1 { public: enum Mode { RECT, POL }; 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: VECTOR1(); VECTOR1(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); 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() { mode = RECT; }; // set mode to POL void rect_mode() { mode = POL; }; // set mode to RECT ​ VECTOR1 operator+(const VECTOR1& b) const; VECTOR1 operator-(const VECTOR1& b) const; VECTOR1 operator-() const; VECTOR1 operator*(double n) const; ​ friend VECTOR1 operator*(double n, const VECTOR1& a); friend ostream& operator<<(ostream& os, const VECTOR1& v); }; }; ​ #endif ​

,

VECTOR.cpp

#include "VECTOR.h" #include <cmath> namespace Vector { ​ //double num = 180.0 / 3.14159265358979323846; const double num = 45.0 / atan(1.0); void VECTOR1::set_mag() { mag = sqrt(x * x + y * y); } ​ void VECTOR1::set_ang() { if (x == 0.0 && y == 0.0) { ang = 0.0; } else { ang = atan2(y, x); } } ​ void VECTOR1::set_x() { x = mag * cos(ang); } ​ void VECTOR1::set_y() { y = mag * sin(ang); } ​ VECTOR1::VECTOR1() { x = y = mag = ang = 0.0; mode = RECT; } ​ VECTOR1::VECTOR1(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 / num; set_x(); set_y(); cout << "ang=" << ang << "mag=" << mag << endl; } else { cout << "Incorrect 3rd argument to VECTOR() -- "; cout << "vector set to 0\n"; x = y = mag = ang = 0.0; mode = RECT; } } ​ void VECTOR1::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 / num; 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; } } ​ VECTOR1 VECTOR1::operator+(const VECTOR1& b) const { VECTOR1 temp; temp.x = x + b.x; temp.y = y + b.y; temp.set_mag();//类适合在一个对象中表示实体的不同方面(直角坐标和极坐标) temp.set_ang(); return temp; }//按照这种重载方式,不仅要更新直角坐标,还要更新极坐标, //按照下面用构造函数的方式就方便的多 VECTOR1 VECTOR1::operator-(const VECTOR1& b) const { return VECTOR1(x - b.x, y - b.y); } VECTOR1 VECTOR1::operator-() const { return VECTOR1(-x, -y); } VECTOR1 VECTOR1::operator*(double n) const { return VECTOR1(n * x, n * y); } ​ VECTOR1 operator*(double n, const VECTOR1& a) { return a * n; } ​ ostream& operator<<(ostream& os, const VECTOR1& v) { if (v.mode == VECTOR1::RECT) { os << "(x,y) = (" << v.x << "," << v.y << ")"; } else if (v.mode == VECTOR1::POL) { os << "(m,a) = (" << v.mag << "," << v.ang<< ")"; //cout << "d" << v.mag << v.ang * num; } else { os << "Vector object mode is invalid"; } return os; } }

main.cpp

// VECTOR.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // ​ #include <iostream> #include "VECTOR.h" using namespace Vector; int main() { std::cout << "Hello World!\n"; VECTOR1 a = VECTOR1(3.0, 4.0, VECTOR1::POL); VECTOR1 B = VECTOR1(6.0, 4.0, VECTOR1::RECT); VECTOR1 d = VECTOR1(8.0, 6.0, VECTOR1::POL); /*VECTOR1 a = VECTOR1(3.0, 4.0, RECT);*/ cout << "a: " << a << endl; cout << "B: " << B << endl; VECTOR1 c; c = a + B; cout << "c=a+B: " << c << endl; c = a - B; cout << "c=a-B: " << c << endl; c = -d; cout << "c=-d: " << c << endl; return 0; } ​ // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 ​ // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 ​

类适合在一个对象中表示实体的不同方面

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/26 12:48:37

Java SpringBoot+Vue3+MyBatis 影院购票系统系统源码|前后端分离+MySQL数据库

摘要 随着互联网技术的快速发展&#xff0c;在线购票系统逐渐成为现代影院管理的重要组成部分。传统的线下购票方式存在效率低、排队时间长、资源分配不均等问题&#xff0c;而在线购票系统能够有效解决这些痛点&#xff0c;为用户提供便捷的购票体验。影院购票系统通过整合影院…

作者头像 李华
网站建设 2026/5/23 18:32:01

Elasticsearch 分片满了?「cluster.max_shards_per_node」报错

目录一、问题原因分析二、解决方案&#xff08;按优先级排序&#xff09;1. 临时调整集群分片上限&#xff08;快速缓解&#xff09;2. 检查并优化现有分片&#xff08;长期解决方案&#xff09;3. 检查服务器文件描述符限制&#xff08;底层排查&#xff09;三、验证解决方案总…

作者头像 李华
网站建设 2026/5/26 23:37:45

HelloGitHub低代码革命:5款零基础也能玩转的开源神器

HelloGitHub低代码革命&#xff1a;5款零基础也能玩转的开源神器 【免费下载链接】HelloGitHub 项目地址: https://gitcode.com/GitHub_Trending/he/HelloGitHub HelloGitHub低代码开发平台正在彻底改变编程学习方式&#xff0c;让零基础的普通用户也能轻松构建自己的数…

作者头像 李华
网站建设 2026/5/26 20:55:34

Langchain-Chatchat回滚机制设计:出现问题快速恢复的预案

Langchain-Chatchat 回滚机制设计&#xff1a;快速恢复的工程实践 在智能知识系统日益普及的今天&#xff0c;一个看似微小的配置错误&#xff0c;可能让整个问答服务陷入瘫痪——用户提问无响应、检索结果错乱、甚至模型加载失败。这类问题在本地部署的 LLM 应用中尤为常见&am…

作者头像 李华
网站建设 2026/5/27 22:50:30

Langchain-Chatchat OAuth2集成:统一身份认证平台对接

Langchain-Chatchat OAuth2集成&#xff1a;统一身份认证平台对接 在企业构建智能问答系统的实践中&#xff0c;一个常见的矛盾逐渐浮现&#xff1a;我们渴望大模型带来的智能化能力&#xff0c;又担心数据泄露的风险。尤其是当系统需要处理内部政策、技术文档或客户资料时&…

作者头像 李华
网站建设 2026/5/25 4:34:59

终极指南:用动漫主题彻底改变你的VS Code编程体验

终极指南&#xff1a;用动漫主题彻底改变你的VS Code编程体验 【免费下载链接】doki-theme-vscode Cute anime character themes for VS-Code. 项目地址: https://gitcode.com/gh_mirrors/do/doki-theme-vscode 想要为枯燥的编程环境注入活力吗&#xff1f;VS Code动漫主…

作者头像 李华