【C++】 31_完善的复数类

发布时间:2019-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 31_完善的复数类脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

完善的复数

  • 复数类应该具有的操作

    • 运算: +, -, *, /
    • 比较: ==, !=
    • 赋值: =
    • 求模: modulus

  • 利用操作符重载

    • 统一复数与实数的运算方式
    • 统一复数与实数的比较方式
Complex operator + (const Complex& c);
Complex oPErator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
    
bool operator == (const Complex& c);
bool operator != (const Complex& c);
    
Complex& operator = (const Complex& c);

编程实验: 复数类的实现

Complex.h

#ifndef _COMPLEX_H_
#define _COMPLEX_H_

class Complex
{
PRivate:
    double a;
    double b;

public:
    Complex(int a = 0, int b = 0);
    int getA();
    int getB();
    int getModulus();
    
    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);
    
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    
    Complex& operator = (const Complex& c);
};

#endif

Complex.cpp

#include "Complex.h"
#include <;math.h>

Complex::Complex(int a, int b)
{
    this->a = a;
    this->b = b;
}

int Complex::getA()
{
    return a;
}

int Complex::getB()
{
    return b;
}

int Complex::getModulus()
{
    return sqrt(a * a + b * b);
}

Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.a;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator / (const Complex& c)
{
    double nm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / nm;
    double nb = (b * c.a - a * c.b) / nm;
    Complex ret(na, nb);
    
    return ret;
}

bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}

bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}

// 为了实现循环赋值,将自身引用返回
Complex& Complex::operator = (const Complex& c)
{
    // 若意图自己给自己赋值,则跳过
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }
    
    return *this;
}

main.cpp

#include <stdio.h>
#include "Complex.h"

int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    Complex c3 = c2 - c1;
    Complex c4 = c2 + c1;
    Complex c5 = c2 * c1;
    Complex c6 = c2 / c1;
    
    printf("c3.a = %d, c3.b = %dn", c3.getA(), c3.getB());
    printf("c4.a = %d, c4.b = %dn", c4.getA(), c4.getB());
    printf("c5.a = %d, c5.b = %dn", c5.getA(), c5.getB());
    printf("c6.a = %d, c6.b = %dn", c6.getA(), c6.getB());

    Complex c7(1, 2);
    
    printf("c1 == c7 : %dn", c1 == c7);
    printf("c2 != c7 : %dn", c2 != c7);

    (c3 = c2) = c1;
    
    printf("c1.a = %d, c1.b = %dn", c1.getA(), c1.getB());
    printf("c2.a = %d, c2.b = %dn", c2.getA(), c2.getB());
    printf("c3.a = %d, c3.b = %dn", c3.getA(), c3.getB());
    

    return 0;
}
输出:
c3.a = 2, c3.b = 4
c4.a = 4, c4.b = 7
c5.a = -9, c5.b = 12
c6.a = 3, c6.b = 0
c1 == c7 : 1
c2 != c7 : 1
c1.a = 1, c1.b = 2
c2.a = 3, c2.b = 6
c3.a = 1, c3.b = 2

注意事项

  • C++ 规定赋值操作符(==)只能重载为成员函数
  • 操作符重载不能改变原操作符的优先级
  • 操作符不能改变操作数的个数
  • 操作符不应改变操作符的原有语义

小结

  • 复数的概念可以通过自定义类实现
  • 复数中的运算操作符可以通过操作符重载实现
  • 赋值操作符只能通过成员函数实现
  • 操作符重载的本质为函数定义

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 31_完善的复数类全部内容,希望文章能够帮你解决【C++】 31_完善的复数类所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。