Complex numbers

Question:

Write a class for complex numbers. A complex number has the form a + b i a+bi a+bi, where a a a and b b b are real numbers and i i i is the square root of − 1 -1 1. We refer to a a a as the real part and b b b as the imaginary part of the number. The class should have two data members to represent the real and imaginary numbers; the constructor takes two arguments to set these numbers. Discuss and implement other appropriate operators for this class.

My answer:

complex.h

#pragma once
class complex{
public:
    complex(){real = imagine = 0;};
    complex(double a, double b){
        real = a;
        imagine = b;
    };
    // double get_real();
    // double get_imagine();
    void print_info();
    friend complex operator +(const complex &a, const complex &b);
    friend complex operator -(const complex &a, const complex &b);
    friend complex operator *(const complex &a, const complex &b);
    friend complex operator /(const complex &a, const complex &b);
private:
    double real;
    double imagine;
};

complex.cpp

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

using namespace std;

void complex::print_info(){
    if(real == 0 && imagine != 0){
        cout << imagine << "i" << endl;
        return;
    } else if(real == 0 && imagine == 0){
        cout << "0" << endl;
        return;
    }
    // 到这里real不为0
    cout << real;
    if(imagine > 0 && imagine != 1){
        cout << "+" << imagine << "i" << endl;
    } else if(imagine == 1){
        cout << "+i" << endl;
    } else if(imagine < 0 && imagine != -1){
        cout << imagine << "i" << endl;
    } else if(imagine == -1){
        cout << "-i" << endl;
    } else {// imagine == 0
        cout << endl;
    }
    return;
}

complex operator +(const complex &a, const complex &b){
    double n = a.real + b.real;
    double m = a.imagine + b.imagine;
    return complex(n, m);
}

complex operator -(const complex &a, const complex &b){
    double n = a.real - b.real;
    double m = a.imagine - b.imagine;
    return complex(n, m);
}

complex operator *(const complex &a, const complex &b){
    double n = a.real*b.real - a.imagine*b.imagine;
    double m = a.real*b.imagine + a.imagine*b.real;
    return complex(n, m);
}

complex operator /(const complex &a, const complex &b){
    assert(!(b.real == 0 && b.real == 0));
    double n = (a.real*b.real+a.imagine*b.imagine)/(pow(b.real, 2)+pow(b.imagine, 2));
    double m = (a.imagine*b.real-a.real*b.imagine)/(pow(b.real, 2)+pow(b.imagine, 2));
    return complex(n, m);
}

main.cpp

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

using namespace std;

int main(){
    complex a = complex(7, 1);// 7+i
    complex b = complex(3, 4);// 3+4i
    complex c;
    cout << "a = ";
    a.print_info();
    cout << "b = ";
    b.print_info();
    cout << "c = a + b = ";
    c = a + b;
    c.print_info();
    cout << "c = a - b = ";
    c = a - b;
    c.print_info();
    cout << "c = a * b = ";
    c = a * b;
    c.print_info();
    cout << "c = a / b = ";
    c = a / b;
    c.print_info();
    return 0;
}

结果:

a = 7+i
b = 3+4i
c = a + b = 10+5i
c = a - b = 4-3i
c = a * b = 17+31i
c = a / b = 1-i

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p125

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值