前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 自定义复数类

C++ 自定义复数类

作者头像
用户6021899
发布2021-05-20 10:34:41
1.3K0
发布2021-05-20 10:34:41
举报
文章被收录于专栏:Python编程 pyqt matplotlib

C++练习。

功能:自定义复数类型,实现复数的加、减、乘、除、求共轭复数、乘方、开方等运算。

涉及到的基础知识点有:

  1. 运算符重载(+,-,*,/, <<, ^, ==, != 等运算符的重载)
  2. 友元函数(友元函数可访问类的私有属性)
  3. 函数返回指向数组的指针。此例中数组的元素是类的对象。
  4. 左值引用与右值引用
  5. 主动抛出异常(使用关键字throw)
代码语言:javascript
复制
#include <iostream>
#include <cmath>
using namespace std;


class Division_by_zero:exception{};

class NegativeValue:exception{};


class Complex
{
//类的友元函数,让该函数可以访问类的私有属性
friend ostream & operator<<(ostream& out, Complex& c);//左值引用
friend ostream & operator<<(ostream& out, Complex&& c);//右值引用
friend Complex operator*(double k, Complex& c);//左侧数乘 左值引用
friend Complex operator*(double k, Complex&& c);//左侧数乘 右值引用
private:
    double re;
    double im;
    
public:
    Complex()
    {
        re = 0;//实部
        im = 0;//虚部
    }
    
    Complex(double r, double i)
    {
        re = r;//实部
        im = i;//虚部
    }
    
    double get_re()
{
        return re;
    }
    
    double get_im()
{
        return im;
    }
    
    double mag() const//计算幅值
{
        double temp = re*re;
        temp += im*im;
        return sqrt(temp);
    }
    
    double ang() const //计算相位角
{
        return atan2(im, re);
    }
    
    Complex conjugate() const
{
        Complex temp ={0, 0};
        temp.re = this->re;
        temp.im = -this->im;
        return temp;
    }
    
    //重载加号运算符
    Complex operator+(Complex& other) const
    {
        Complex temp ={0, 0};
        temp.re = this->re + other.re;
        temp.im = this->im + other.im;
        return temp;
    }
    Complex operator+(Complex&& other) const //右值引用
    {
        Complex temp ={0, 0};
        temp.re = this->re + other.re;
        temp.im = this->im + other.im;
        return temp;
    }
    
    //重载减号运算符
    Complex operator-(Complex& other) const
    {
        Complex temp ={0, 0};
        temp.re = this->re - other.re;
        temp.im = this->im - other.im;
        return temp;
    }
    Complex operator-(Complex&& other) const //右值引用
    {
        Complex temp ={0, 0};
        temp.re = this->re - other.re;
        temp.im = this->im - other.im;
        return temp;
    }
    
    //重载乘号运算符
    Complex operator*(Complex& other) const
    {
        Complex temp ={0, 0};
        temp.re = this->re * other.re - this->im * other.im;
        temp.im = this->re * other.im + this->im * other.re;
        return temp;
    }
    Complex operator*(Complex&& other) const //右值引用
    {
        Complex temp ={0, 0};
        temp.re = this->re * other.re - this->im * other.im;
        temp.im = this->re * other.im + this->im * other.re;
        return temp;
    }
    
    Complex operator*(double k) const //右侧数乘
    {
        Complex temp ={0, 0};
        temp.re = k * this->re;
        temp.im = k * this->im;
        return temp;
    }
    
    //重载除号运算符
    Complex operator/(Complex& other) const
    {
        double denominator;
        denominator = other.re * other.re + other.im * other.im;
        if(denominator==0)
        {
            throw Division_by_zero();
        }
        Complex temp ={0, 0};
        temp.re = (this->re * other.re + this->im * other.im)/denominator;
        temp.im = (this->im * other.re - this->re * other.im)/denominator;
        return temp;
    }
    
    Complex operator/(Complex&& other) const //右值引用
    {
        double denominator;
        denominator = other.re * other.re + other.im * other.im;
        if(denominator==0)
        {
            throw Division_by_zero();
        }
        Complex temp ={0, 0};
        temp.re = (this->re * other.re + this->im * other.im)/denominator;
        temp.im = (this->im * other.re - this->re * other.im)/denominator;
        return temp;
    }
    
    //幂运算
    Complex operator^(int n) const
    {
        if(n<0)
        {
            throw NegativeValue();
        }
        Complex temp ={0, 0};
        double r = this->mag();
        double theta = this->ang();
        temp.re = cos(n * theta);
        temp.im = sin(n * theta);
        temp = temp * pow(r, n);
        return temp;
    }
    
    //开n次方
    Complex* root(int n) const
{
        if(n<=0)//如果n的类型是 unsigned, 则这句不执行!
        {
            throw NegativeValue();
        }
        const double pi = 3.141592653589793238;
        Complex* p = new Complex[n]; //p指向堆上新建的数组
        Complex temp ={0, 0};
        double r = this->mag();
        double theta = this->ang();
        double theta_new = 0.0;
        double r_new = pow(r, 1.0/n);
        for(int k=0; k<n; k++)
        {
            theta_new = (2.0*k*pi + theta)/n;
            temp.re = r_new * cos(theta_new);
            temp.im = r_new * sin(theta_new);
            p[k] = temp;
        }
        return p;//返回数组指针
    }
    
    //重载等号运算符
    bool operator==(Complex& other) const
    {
        return this->re == other.re && this->im == other.im;
    }
     bool operator==(Complex&& other) const //右值引用
    {
        return (this->re == other.re && this->im == other.im);
    }

    //重载不等于等号运算符
    bool operator!=(Complex& other) const
    {
        return this->re != other.re || this->im != other.im;
    }
     bool operator!=(Complex&& other) const //右值引用
    {
        return this->re != other.re || this->im != other.im;
    }
};


//左移运算符重载,用于自定义打印。只能利用全局函数重载左移运算符
ostream & operator<<(ostream& out, Complex& c)
{
    if(c.im >= 0) out<< c.re <<"+" << c.im <<"j";
    else out<< c.re  << c.im <<"j";
    return out;
}
// 同上,但右值引用
ostream & operator<<(ostream& out, Complex&& c)
{
    if(c.im >= 0) out<< c.re <<"+" << c.im <<"j";
    else out<< c.re  << c.im <<"j";
    return out;
}

//左侧数乘
Complex operator*(double k, Complex& c)
    {
        Complex temp ={0, 0};
        temp.re = k * c.re;
        temp.im = k * c.im;
        return temp;
    }
Complex operator*(double k, Complex&& c)
    {
        Complex temp ={0, 0};
        temp.re = k * c.re;
        temp.im = k * c.im;
        return temp;
    }

int main()
{
    //测试
    Complex c1 ={-1,3};
    Complex c2 ={3,4};
    Complex c3 = c2;
    cout.precision(10);
    cout<<"c1= "<<c1<<endl;
    cout<<"c1.re = "<<c1.get_re()<<endl;
    cout<<"c1.im = "<<c1.get_im()<<endl;
    cout<<"conjugate complex of c1: "<<c1.conjugate()<<endl;
    cout<<4 *c1<<endl;
    cout<<"c2= "<<c2<<endl;
    cout<<"c3= "<<c3<<endl;
    cout<<"c2.mag= "<<c2.mag()<<endl;
    cout<<"c1+c2= "<<c1+c2<<endl;
    cout<<"c1-c2= "<<c1-c2<<endl;
    cout<<Complex(-1,-3)<<endl;//Complex(-1,-3)是个常量,属于右值
    cout<<Complex(3,5) * Complex(3,5)<<endl;
    cout<<Complex(3,4) / Complex(5,6)<<endl;
    //cout<<Complex(3,4) / Complex(0,0)<<endl;//0做分母
    cout<<(Complex(3,4)^(9))<<endl;
    unsigned n = 9;
    Complex* p = Complex(3,4).root(n);
    cout<<Complex(3,4)<<"的"<<n<<"个"<<n<<"次方根为:"<<endl;
    for(unsigned i=0; i<n; i++)
    {
        cout<<"第"<<(i+1)<<"个"<<n<<"次方根为:"<<p[i]<<endl;
    }
    delete[] p;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档