一个Complex类的简单实现

C++ 简单复数类 C++下一个简单的复数类,重载类+,-,*,<<,>>运算符 立即下载
 对于Complex类,最初觉得很容易。写下来之后,对照着大师的评论和实现,发现了差距,也理清了思路。在此贴出,以做参照。
/************************************************************************
The standard requires that operators = () [] and -> must be members, and 
class-specific operators new, new[], delete, and delete[] must be static members. 
For all other functions:

if the function is operator>> or operator<< for stream I/O,
or if it needs type conversions on its leftmost argument,
or if it can be implemented using the class's public interface alone,
    make it a nonmember (and friend if needed in the first two cases)

    if it needs to behave virtually,
        add a virtual member function to provide the virtual behavior,
        and implement it in terms of that

else
    make it a member
***********************************************************************
*/


#include 
<iostream>
using namespace std;

template 
<typename T>
class Complex
{
    template 
<typename Ty> friend class Complex;

public:
    
// In general it's a good idea to make your constructors explicit by default 
    
// unless you deliberately decide to allow the implicit conversion
    explicit Complex(T real, T imag = T()) : real_(real), imag_(imag)
    
{
    }


    
// Prefer writing "a op= b;" instead of "a = a op b;" (where op stands for any operator).
    
// It's clearer, and it's often more efficient
    template <typename Ty>
    Complex
& operator +=(const Complex<Ty>& other)
    
{
        real_ 
+= other.real_;
        imag_ 
+= other.imag_;
        
return *this;
    }


    template 
<typename Ty>
    Complex
& operator -=(const Complex<Ty>& other)
    
{
        real_ 
-= other.real_;
        imag_ 
-= other.imag_;
        
return *this;
    }


    
// Preincrement should return a reference to non-const梚n this case, Complex&. 
    
// This lets client code operate more intuitively and avoids needless inefficiency.
    Complex& operator ++()
    
{
        
++real_;
        
return *this;
    }


    Complex
& operator --()
    
{
        
--real_;
        
return *this;
    }


    
// Postincrement should return a const value in this case, const Complex. 
    
// By not allowing changes to the returned object, we prevent questionable code like "a++++", 
    
// which doesn't do what a naïve user might think it does.

    
// For consistency, always implement postincrement in terms of preincrement. 
    
// Otherwise, your users will get surprising (and often unpleasant) results
    const Complex operator ++(int)
    
{
        Complex temp(
*this);
        
++*this;
        
return temp;
    }


    
const Complex operator --(int)
    
{
        Complex temp(
*this);
        
--*this;
        
return temp;
    }


    
// Add a helper method to avoid friend methods for operator <<
    
// and if it is a base class, add virtual to print derived class
    ostream& print(ostream& os) const
    
{
        
return os << "(" << real_ << ", " << imag_ << ")";
    }


private:
    
// follow the convention of designating member variable names with a trailing underscore
    T real_, imag_;
}
;

// Notice the relationship between operators + and +=, - and -=. 
// The former should be implemented in terms of the latter, 
// both for simplicity (the code is easier to write) and 
// for consistency (the two will do the same thing and 
// are less likely to diverge during maintenance).
template <class T1, class T2>
const Complex<T1> operator +(const Complex<T1>& lhs, const Complex<T2>& rhs)
{
    Complex
<T1> ret(lhs);
    ret 
+= rhs;
    
return ret;
}


template 
<class T1, class T2>
const Complex<T1> operator -(const Complex<T1>& lhs, const Complex<T2>& rhs)
{
    Complex
<T1> ret(lhs);
    ret 
-= rhs;
    
return ret;
}


// operator<< should not be a member function.
// operator<< should have a return type of "ostream&" and 
// should return a reference to the stream in order to permit chaining. 
// That way, users can use your operator<< naturally in code like "cout << a << b;".
template <class T>
ostream
& operator <<(ostream& os, const Complex<T>& com)
{
    
return com.print(os);
}


int main()
{
    Complex
<double> c2(3, 4);
    Complex
<double> c3(0.1, 2.5);

    cout 
<< c2 << endl 
         
<< c3 << endl 
         
<< c3 + c2 << endl
         
<< c3 - c2 << endl;

    
return getchar();
}

 

简单的复数类源代码,适合初学者哦 关于复数类的Java源代码,可以进行复数类的运算,适合Java的初学者 立即下载

相关推荐

HDRnet——双边滤波和仿射变换的摇身一变

layer层数越多,下采样得到的图越小,low-level features的好处是最后的grid会更coarser,最后的特征图的感受野也越多,非线性更好,表达力更强。所以最终grid是5维的[B,in*out,n,H,W],permute之后是[B,H,W,n,in*out],B是batchsize,n表示对guide的量化级数,in*out表示每个cell的变换系数。这部分的处理对象是低分辨率图,利用CNN进行特征提取(局部和全局),最后fuse得到grid,这里面包含了变换的系数。

zcg1942的博客 1588

008:超简单的复数类

描述 下面程序的输出是: 3+4i 5+6i 请补足Complex类的成员函数。不能加成员变量。 #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" <&

qq_44588905的博客 979

S32K344 Flash Driver实战:手把手教你实现任意字节写入与扇区解锁(含C40_Ip避坑指南)

本文深入解析S32K344微控制器的Flash驱动开发,重点解决C40_Ip驱动库的8字节对齐限制和扇区保护锁问题。通过实战案例和代码示例,详细介绍了如何实现任意字节写入、扇区解锁及性能优化策略,为Bootloader和OTA升级等应用提供可靠解决方案。

weixin_33713707的博客 435

8 超简单的复数类

#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } // 在此处补充你

quanmator 250

瑞萨CS+仿真器高级配置指南:从原理到实战优化RX系列MCU调试

在嵌入式系统开发中,仿真器是进行早期验证和深度调试的核心工具。它通过精确模拟目标微控制器的指令集、内存和外设行为,构建了一个虚拟的硬件沙盒环境。其工作原理在于对CPU核心、时钟系统和外设接口的软件模拟,使开发者能在无物理硬件时进行代码逻辑验证、内存布局规划和时序分析。这项技术的核心价值在于大幅降低开发成本、加速问题定位,并支持复杂场景的复现与测试。在瑞萨RX系列MCU开发中,CS+集成开发环境内置的仿真器功能尤为强大。本文聚焦于**指令解码缓存**和**外设时钟比率**等关键配置的深度解析,阐述如何通过优化

cmff98425的博客 1128

【面试题】MySQL索引相关的面试题(红黑树,hash,B树,B+树)

自己整理了几个面试题分享给大家 问题1:红黑树为什么那样设计? 首先聊一下二叉搜索树,主要特点是左节点比根节点小,右节点比根节点大,并且左右子树都是二叉搜索树。缺点是在极端情况下,比如插入都是有序的,就会出现退化的情况有序序列树退化成链表。 二叉搜索树 退化成链表的二叉搜索树 此时,要想让树的节点平均分布就需要平衡树了,红黑树就是平衡树的一种(平衡二叉搜索树)。然后,一棵树的查询性能取决...

Mr_黑_ 的博客 4048

简易复数类

【问题描述】    定义一个复数类,并重载运算符,以实现复数的加减乘除,相等与否,并显示其结果。【代码实现】// code.c #include  using namespace std; class Comoplex { friend ostream& operator<<(ostream& os ,const Comoplex& c); //友元 public: Comoplex(dou

Doo66的博客 641

2D激光slam四种算法建图效果对比

2d激光slam四种算法建图比较

m0_73791170的博客 8467

OpenJudge第二周测验—03

003:超简单的复数类 样例输入 无 样例输出 3+4i 5+6i 代码 #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i <

格格格子曼的博客 260

合宙Air780EP速连OneNET:AT+MQTT应用指南

本文详细讲解了合宙Air780EP使用AT指令通过MQTT接入OneNET的过程。

2401_82812461的博客 1393

深入剖析 YOLOv11 中的 C2PSA 注意力机制

YOLOv11中的C2PSA注意力机制在目标检测领域取得突破性进展。该机制通过跨通道分组交互和轻量级位置编码,在仅增加0.6%计算量的情况下提升mAP 1.5-2.3个百分点。C2PSA创新性地结合局部跨通道交互、位置编码和双分支并行调制,采用纯卷积实现自注意力,避免传统注意力机制的高计算复杂度。实验表明,C2PSA在COCO数据集上以微小计算代价显著提升检测性能,特别是对小目标和遮挡目标效果突出。该模块可直接嵌入YOLO架构,为轻量级目标检测提供新的优化方向。

shrgegrb的博客 3412

008:超简单的复数类

请补足Complex类的成员函数。

星辰大海的博客 236

2024-12月js逆向案例-sensor-data之vmp字段之akamai_2/3.0-(下)

akamai2/3的vmp字段逆向分析

十一姐的博客 3150

创建一个complex包,包中定义一个复数类Complex,实现复数的加减乘法(包括其中有一个是实数的情况)以及相应的set /get方法,然后编写另一个应用程序进行验证包

1.public class ComplexTester{ 3. public static void main(String[] args) 4. { 5. Complex z1=new Complex(1,2); 6. Complex z2=new Complex(3,4); 7. System.out.println(z1+"+"+z2+"="+z1

keep_early的博客 9539

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

java复数类实部_用Java定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部定义一个复数类 complex ,它的内部具有两个实例...

优质解答import java.util.Scanner;public class Complex {private int realPart;private int imagPart;public int getRealPart() {return realPart;}public void setRealPart(int realPart) {this.realPart = realPart...

weixin_39878688的博客 5273

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

给出复数,实现复数的四则运算

给出复数,实现复数的四则运算。

baidu_27876317的博客 1259

java定义类complex,复数加减

class Fushu{ public static void main(String[] args){ Complex complex1=new Complex(5,6); Complex complex2=new Complex(6,2); System.out.println(complex1+"+("+complex2+")="+Complex.addit

foreverlove111的博客 6516

创建一个复数类Complex,对复数进行数学运算

参考,误抄袭 问题描述: 创建一个复数类Complex,对复数进行数学运算,复数具有如下格式:realPart+imaginaryPart*i,其中,i为-1的平方根,具体要求如下: (1)利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明时对象的初始化;一个为带默认值得无参构造方法。 (2)提供两复数加、减、乘的运算方法。 (3)按格式(a,b)打印复数,其中a为实部,b为虚部。 【输入形式】 输入两个复数的实部和虚部,构造两个复数对象 【输出形式】 输出两个复数的相加、相减、相乘的结果

先养只猫的博客 9457

编写一个完整的java程序——Complex

要求是: Complex类有两个属性: realPart:int型,实数部分; imagePart:int型,虚数部分; 构造方法1:将复数的实部和虚部都设为0; 构造方法2:形参r为实部的初值,形参i为虚部的初值 complexAdd()将当前复数对象与形参复数对象相加,结果返回; toString()把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a和b分别为实部和虚部

guo10299的博客 5134

复数类complex,加减乘除,,,,,java

package p1; public class Complex { private double real;//实部 private double image;//虚部 Complex(){ } Complex(double real, double image){ this.real=real; this.image=image; } void add(Complex a, Complex b) {//加 Complex c=new Complex(); c.real=a.real+b.

ONE_one_plus的博客 344

【java学习记录】3.采用面向对象的方法设计一个能对复数(Complex)进行加、减、乘法的简单运算的程序

采用面向对象的方法设计一个能对复数(Complex)进行加、减、乘法简单运算的程序

六盆的博客 3481

python定义复数类complex使代码能够工作_定义一个复数类Complex,使得下面的代码能够工作...

题目:定义一个复数类Complex,使得下面的代码能够工作Complex c1(3,5); //用复数3+5i初始化c1Complex c2=4.5; //用实数4.5初始化c2c1.add(c2); //将c1与c2相加,结果保存在c1中c1.show(); //将c1输出(这时的结果应该是7.5+5i)解答:#includeusing namespace std;class Complex{p...

weixin_39884877的博客 1856

Double的Complex()的方法

class Complex{ //instance variables double real,imag; public static void main(String args[]){ Complex a=new Complex(1.0,1.1); Complex b=new Complex(1.0,1.1); bo...

feiyuyuniao的专栏 1450

简单的运算符重载

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作 者:刘亚 *完成日期:2016年 5月 30日 *版 本 号:1.0 * *问题描述:简单的运算符重载 *输入描述: 两个复数, *程序输出: 两个复数相加减的结果 */ #include using namespace std;

qq_33758529的博客 286

类

源代码: #include<iostream> using namespace std; class Complex { public: Complex(float r1,float i1); Complex(float r1); void add(Complex c); voi...

dlt70311的博客 95

处理虚数类 运算符的重载 友元函数

/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: object.cpp * 作者:王飞 * 完成日期:2013 年4月 19日 * 版本号: v1.0 * 输入描述:无 * 问题描述:处理虚数类 * 程序输出:如下 */ #include usin

fly 1367
上一篇: 一个统计线程占用CPU时间的类
下一篇: 二叉树系列之一:二叉树
HappyTree
博客等级 码龄26年 20粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址