侯捷-C++面向对象高级开发(上)-complex类实现

2023-11-15

complex类实现

comlex.h

#ifndef __COMPLEX_H__
#define __COMPLEX_H__

#include <cmath>

class complex;
complex &
__doapl(complex *ths, const complex &r);
complex &
__doami(complex *ths, const complex &r);
complex &
__doaml(complex *ths, const complex &r);

class complex
{
public:
    complex(double r = 0, double i = 0) : re(r), im(i) {}
    complex &operator+=(const complex &);
    complex &operator-=(const complex &);
    complex &operator*=(const complex &);
    complex &operator/=(const complex &);
    double real() const { return re; }
    double imag() const { return im; }

private:
    double re, im;

    friend complex &__doapl(complex *, const complex &);
    friend complex &__doami(complex *, const complex &);
    friend complex &__doaml(complex *, const complex &);
};

inline complex &
__doapl(complex *ths, const complex &r)
{
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

inline complex &
__doami(complex *ths, const complex &r)
{
    ths->re -= r.re;
    ths->im -= r.im;
    return *ths;
}

inline complex &
__doaml(complex *ths, const complex &r)
{
    double f = ths->re * r.re - ths->im * r.im;
    ths->im = ths->re * r.im + ths->im * r.re;
    ths->re = f;
    return *ths;
}

inline complex &
complex::operator+=(const complex &r)
{
    return __doapl(this, r);
}

inline complex &
complex::operator-=(const complex &r)
{
    return __doami(this, r);
}

inline complex &
complex::operator*=(const complex &r)
{
    return __doaml(this, r);
}

inline double
real(const complex &r)
{
    return r.real();
}

inline double
imag(const complex &i)
{
    return i.imag();
}

inline complex
operator+(const complex &left, const complex &right)
{
    return complex(real(left) + real(right), imag(left) + imag(right));
}

inline complex
operator+(const complex &left, double right)
{
    return complex(real(left) + right, imag(left));
}

inline complex
operator+(double left, const complex &right)
{
    return complex(left + real(right), imag(right));
}

inline complex
operator-(const complex &left, const complex &right)
{
    return complex(real(left) - real(right), imag(left) - imag(right));
}

inline complex
operator-(const complex &left, double right)
{
    return complex(real(left) - right, imag(left));
}

inline complex
operator-(double left, const complex &right)
{
    return complex(left - real(right), imag(right));
}

inline complex
operator*(const complex &left, const complex &right)
{
    return complex(real(left) * real(right) - imag(left) * imag(right),
                   real(left) * imag(right) + imag(left) * real(right));
}

inline complex
operator*(const complex &left, double right)
{
    return complex(real(left) * right, imag(left) * right);
}

inline complex
operator*(double left, const complex &right)
{
    return complex(left * real(right), left * imag(right));
}

inline complex
operator/(const complex &left, double y)
{
    return complex(real(left) / y, imag(left) / y);
}

inline complex
operator+(const complex &x) // ??
{
    return x;
}

inline complex
operator-(const complex &x)
{
    return complex(-real(x), -imag(x));
}

inline bool
operator==(const complex &left, const complex &right)
{
    return real(left) == real(right) && imag(left) == imag(right);
}

inline bool
operator==(const complex &left, double right)
{
    return real(left) == right && imag(left) == 0;
}

inline bool
operator==(double left, const complex &right)
{
    return left == real(right) && 0 == imag(right);
}

inline bool
operator!=(const complex &left, const complex &right)
{
    return real(left) != real(right) || imag(left) != imag(right);
}

inline bool
operator!=(const complex &left, double right)
{
    return real(left) != right || imag(left) != 0;
}

inline bool
operator!=(double left, const complex &right)
{
    return left != real(right) || 0 == imag(right);
}

inline complex
polar(double r, double t)
{
    return complex(r * cos(t), r * sin(t));
}

inline complex
conj(const complex &x)
{
    return complex(real(x), -imag(x));
}

inline double
norm(const complex &x)
{
    return real(x) * real(x) + imag(x) * imag(x);
}

#endif // !__COMPLEX_H__

comlex_test.cpp 测试

#include "complex.h"
#include <iostream>

using namespace std;

ostream &
operator<<(ostream &os, const complex &x)
{
    return os << '(' << real(x) << ',' << imag(x) << ')';
}

int main()
{
    complex c1(2, 1);
    complex c2(4, 0);

    cout << c1 << endl; // (2,1)
    cout << c2 << endl; // (4,0)

    cout << c1 + c2 << endl; // (6,1)
    cout << c1 - c2 << endl; // (-2,1)
    cout << c1 * c2 << endl; // (8,4)
    cout << c1 / 2 << endl;  // (1,0.5)

    cout << conj(c1) << endl;     // (2,-1)
    cout << norm(c1) << endl;     // 5
    cout << polar(10, 4) << endl; // (-6.53644,-7.56802)

    cout << (c1 += c2) << endl; // (6, 1)

    cout << (c1 == c2) << endl; // 0
    cout << (c1 != c2) << endl; // 1
    cout << +c2 << endl;        // (4, 0)
    cout << -c2 << endl;        // (-4, 0)

    cout << (c2 - 2) << endl; // (2, 0)
    cout << (5 + c2) << endl; // (9,0)

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

侯捷-C++面向对象高级开发(上)-complex类实现 的相关文章

  • VS 程序在调试模式下崩溃,但在发布模式下不崩溃?

    我正在 VS 2012 中运行以下程序来尝试 Thrust 函数查找 include cuda runtime h include device launch parameters h include
  • 信号处理程序有单独的堆栈吗?

    信号处理程序是否有单独的堆栈 就像每个线程都有单独的堆栈一样 这是在 Linux C 环境中 来自 Linux 手册页signal 7 http kernel org doc man pages online pages man7 sign
  • 动态生成的控件 ID 返回为 NULL

    我可以在 Page PreInit 函数中创建动态控件 如何检索控件及其 ID 我的 C 代码用于创建动态控件之一 var btn new WebForms Button btn Text btn ID Addmore btn Click
  • fprintf() 线程安全吗?

    我正在为野人就餐问题的某些变量编写一个 C 解决方案 现在 我创建线程 每个线程都将 FILE 获取到同一个调试文件 在线程内我正在使用 fprintf 进行一些打印 打印的语句不受任何类型的互斥锁等保护 我没有在调试文件中观察到任何交错行
  • 如何获取 QTableView 的标题列表?

    我有一个QTableView我的对话框中的对象 我需要访问该表的水平标题并将它们放入QStringList object 尽管进行了大量搜索 但我在 Qt 文档中找不到如何获取此标头列表 编辑 我发现的最接近的地方是this https w
  • 如何在 QTabWidget Qt 中展开选项卡

    我有一个QTabWidget像这个 但我想展开选项卡以 填充 整个小部件宽度 如下所示 我怎样才能做到这一点 我在用Qt 5 3 2 and Qt 创建者 3 2 1 Update 我尝试使用setExpanding功能 ui gt myT
  • 单例模式和 std::unique_ptr

    std unique ptr唯一地控制它指向的对象 因此不使用引用计数 单例确保利用引用计数只能创建一个对象 那么会std unique ptr与单例执行相同 单例确保只有一个实例属于一种类型 A unique ptr确保只有一个智能指针到
  • std::forward_as_tuple 将参数传递给 2 个构造函数

    我想传递多个参数以便在函数内构造两个对象 以同样的方式std pair
  • C# 构建一个 webservice 方法,它接受 POST 方法,如 HttpWebRequest 方法

    我需要一个接受 POST 方法的 Web 服务 访问我的服务器正在使用 POST 方法 它向我发送了一个 xml 我应该用一些 xml 进行响应 另一方面 当我访问他时 我已经使用 HttpWebRequest 类进行了管理 并且工作正常
  • C++ php 和静态库

    我创建了一个library a 其中包含 cpp 和 h 文件 其中包含很多类 嵌套类和方法 我想在 php 示例中包含这个静态库并尝试使用它 我想提一下 我是 php 新手 我已经在 test cpp 文件中测试了我的 libray a
  • 将二进制数据从 C# 上传到 PHP

    我想将文件从 Windows C 应用程序上传到运行 PHP 的 Web 服务器 我知道 WebClient UploadFile 方法 但我希望能够分块上传文件 以便我可以监控进度并能够暂停 恢复 因此 我正在读取文件的一部分并使用 We
  • 如何通过 JsonConvert.DeserializeObject 在动态 JSON 中使用 null 条件运算符

    我正在使用 Newtonsoft 反序列化已知的 JSON 对象并从中检索一些值 如果存在 关键在于对象结构可能会不断变化 因此我使用动态来遍历结构并检索值 由于对象结构不断变化 我使用 null 条件运算符来遍历 JSON 代码看起来像这
  • 将标量添加到特征矩阵(向量)

    我刚刚开始使用 Eigen 库 无法理解如何向所有矩阵成员添加标量值 假设我有一个矩阵 Eigen Matrix3Xf mtx Eigen Matrix3Xf Ones 3 4 mtx mtx 1 main cxx 104 13 error
  • 如何更改 Ubuntu 14.04 上的 php-cli 版本?

    我是 Linux 新手 在篡改时破坏了一些 php 设置 如果我执行一个包含以下内容的 php 脚本 phpinfo 它显示 php 版本为 5 6 但通过命令行 如果我运行php v它返回 7 0 版本 我想让两个版本匹配 我怎样才能修复
  • cout 和字符串连接

    我刚刚复习了我的 C 我尝试这样做 include
  • 您是否将信息添加到每个 .hpp/.cpp 文件的顶部? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 创建新的 C 头文件 源文件时 您会在顶部添加哪些信息 例如 您是否添加日期 您的姓名 文件描述等 您是否使用结构化格式来存储此信息 e g F
  • 在简单注入器中解析具有自定义参数的类

    我正在使用以下命令创建 WPF MVVM 应用程序简易注射器作为 DI 容器 现在 当我尝试从简单注入器解析视图时遇到一些问题 因为我需要在构造时将参数传递到构造函数中 而不是在将视图注册到容器时 因此这不是适用的 简单注入器将值传递到构造
  • 将 char[][] 转换为 char** 会导致段错误吗?

    好吧 我的 C 有点生疏了 但我想我应该用 C 来做我的下一个 小 项目 这样我就可以对其进行抛光 并且我已经有不到 20 行的段错误了 这是我的完整代码 define ROWS 4 define COLS 4 char main map
  • C++0x中disable_if在哪里?

    Boost 两者都有enable if and disable if 但 C 0x 似乎缺少后者 为什么它被排除在外 C 0x 中是否有元编程工具允许我构建disable if按照enable if 哦 我刚刚注意到std enable i
  • xsi:type 属性搞乱了 C# XML 反序列化

    我使用 XSD exe 根据 XML 架构 xsd 文件 自动生成 C 对象 我正在反序列化 OpenCover 输出 但其中一个部分类未正确生成 这是导致异常的行

随机推荐

  • linux tmux的经验总结

    背景 主要操作实现 安装 概念了解 快捷键 tmux重启后恢复终端layout界面的方法 如果有多个用户比如adminqilei等 新建windows或者pane分屏保留目录路径 复制模式 支持鼠标模式 窗口列表居中否则session和wi
  • 9大最佳知识库软件/文档管理工具

    企业的任何工作流程都离不开文档管理 面对复杂的业务流程 频繁的文档编辑任务和跨区域的文件共享需求 优秀的文档管理体系能够帮助企业实现安全的文档存储 高效的文档搜索 便捷的文档协作和有效的文档权限 版本 行为管控 由于各个产品切入文档管理市场
  • windows下安装cygwin+swoole教程

    swoole下载 http git oschina net swoole swoole cygwin下载 https www cygwin com setup x86 64 exe cygwin镜像地址 http mirrors sohu
  • 如何拯救空间不足的C盘?

    目录 操作步骤 确定软件后期安装的位置 修改注册表 验证 心得 操作步骤 确定软件后期安装的位置 建议选择硬盘内存比较多的一个盘 我选择的是D盘 然后复制D programs 修改注册表 打开注册表编辑器 双击HKEY LOCAL MACH
  • JS操作dom,bom

    属性是 方法里面是可以写参数的 window open 打开窗口 p1 要打开的新窗口地址 p2 窗口名称 p3 窗口特征 open newwindow html width 400px height 400px close 关闭窗口 al
  • STM32外部高速晶振不起振的故障分析

    STM32外部高速晶振不起振的故障分析 一 故障背景 网上售卖的STM32F103C8T6的核心板如图1所示 由于STM32F103C8T6最小系统核心板的采购成本高达20元 块至40元 块 为了降低采购成本 对其STM32F103C8T6
  • oracle sqlldr详解,sqlldr详解

    Oracle 的SQL LOADER可以将外部数据加载到数据库表中 下面是SQL LOADER的基本特点 1 能装入不同数据类型文件及多个数据文件的数据 2 可装入固定格式 自由定界以及可度长格式的数据 3 可以装入二进制 压缩十进制数据
  • codeblocks安装及使用教程详解

    一 官网下载 搜索codeblocks官网 下载最新codeblocks安装包 codeblocks官网 https www codeblocks org downloads 二 安装 1 双击下载好的安装文件 弹出如下界面 点击 Next
  • matlab dct实现代码,基于DCT数字水印算法的Matlab实现源代码

    M 256 原图像长度 N 32 水印图像长度 K 8 I zeros M M II zeros K K B zeros M M Idct zeros K K D zeros M M 读取原图像 I imread 33 png subplo
  • 机器学习 —— 类不平衡问题与SMOTE过采样算法

    在前段时间做本科毕业设计的时候 遇到了各个类别的样本量分布不均的问题 某些类别的样本数量极多 而有些类别的样本数量极少 也就是所谓的类不平衡 class imbalance 问题 本篇简述了以下内容 什么是类不平衡问题 为什么类不平衡是不好
  • 删除git远程库中误传的文件

    不小心把node modules文件夹上传到远端了哇 别急 有办法 git rm r cached node modules 如果是在某个文件夹下面 也可以使用路径 xxx node modules 之后再执行push git commit
  • c语言灯塔案例求塔低数,C++:有一个8层灯塔,每层所点灯数都等于该层上一层的两倍,一共有765盏灯,求塔底的灯数...

    满意答案 0214zyt 2013 05 23 采纳率 51 等级 12 已帮助 6734人 Note Your choice is C IDE include include using namespace std int main 第一
  • Java多线程的同步问题

    在多线程的编程环境中 可能会有两个或者更多的线程试图同时访问一个有限的资源 必须对这种潜在的资源冲突进行预防 解决办法 在线程使用一个资源的时候 我们为其加锁即可 访问资源的第一个线程为其加上锁以后 其它线程便不能访问那个资源 除非获得那个
  • 刷脸支付助力互联网产业时代全面到来

    近两年来 刷脸支付发展如火如荼 宁波 长沙等多个城市相继开展线下刷脸支付试点 建设银在其网点的ATM机推出刷脸取款 光大银也将人脸识别应用于账户登陆 转账 线上融资等场景 支付宝 财付通等第三方支付公司也争相推出刷脸支付设备 随着移动支付的
  • cs231n: How to Train a Neuron Network 如何训练神经网络

    CS231N第六第七课时的一些笔记 如何训练神经网络是一个比较琐碎的事情 所以整理了一下 以后训练Neuron Network的时候可以看一下 Activation Functions ReLu good ELU leaky ReLu no
  • STL中的栈——stack

    stack为STL中的适配器容器 具有栈的结构特性 对于适配器容器 默认底层容器为deque 在创建stack对象时 也可以指定其他线性容器作为其底层容器 基本操作 include
  • AIX/Unix/Linux/HP-UX 系统中文字符集

    在运行环境Unix与Linux系统中遇到中文乱码 在查看后台运行日志时很不方便 于是在网上查看解决方法 经过以下内容可以解决这个问题 希望看到此篇的人能解决此题 针对不同系统可以选用字符集如下 AIX zh CN IBM eucCN Lin
  • 单片机毕业设计不用愁!!30篇单片机毕业设计参考案例

    单片机毕业设计不用愁 30篇单片机毕业设计参考案例 30篇单片机毕业设计参考案例 这篇文章分享给大四的小伙伴 是时候该准备毕业设计了吧 别偷懒了 第二学期就准备实习了喔 所以小编我就开始为你们准备资料啦 30篇单片机毕业设计参考案例给你们啦
  • Eclipse安装查看java字节码插件Bytecode Outline

    一 下载地址 download 选择适合你自己的eclipse的版本 二 安装 根据提示把下载的jar包放进对应的目录里面 然后重启就行了 我自己的是放在这个路径下 E eclipse jee kepler SR2 win32 eclips
  • 侯捷-C++面向对象高级开发(上)-complex类实现

    complex类实现 comlex h ifndef COMPLEX H define COMPLEX H include