运算符重载——重载+和-对复数类对象进行加减运算

1.题目:

Problem Description

定义一个复数类,该类包含两个double型的数据成员代表复数的实部和虚部,包含构造函数(默认值为0,0),和显示函数,现重载运算符+和-,使其能对复数类对象进行加和减运算。在主函数中进行测试

Input

输入数据有多行,每行包括4个数,前两个代表参与运算的第一个复数对象的实部和虚部,后两个代表第二个复数对象的实部和虚部。

Output

输出多行,每行包括了加和减运算后的结果。
复数按a+bi或a-bi格式显示,按数学课本中的要求显示。

Sample Input

100 20 -10 18
2 2 2 4

Sample Output

90+38i 110+2i
4+6i -2i

2.想法:

       这道题目主要是要考虑输出时的集中特殊情况,有实部和虚部都为0,还有虚部为1,-1,整数,负数的情况。静下心来其实不难的。

 

3.参考代码一:

#include <iostream>
using namespace std;

class Complex
{
private:
    double real, image;
public:
    Complex(double r = 0, double i = 0);
    Complex operator+(Complex&);
    Complex operator-(Complex&);
    void show();
};

Complex::Complex(double r, double i)
{
    real = r;
    image = i;
}

Complex Complex::operator+(Complex& c)
{
    Complex x;
    x.real = real + c.real;
    x.image = image + c.image;
    return x;
}

Complex Complex::operator-(Complex& c)
{
    Complex x;
    x.real = real - c.real;
    x.image = image - c.image;
    return x;
}

void Complex::show()
{
    if (real == 0) {
        if (image == 0)
            cout << 0;
        else if (image == 1)
            cout << "i";
        else if (image == -1)
            cout << "-i";
        else if (image > 0)
            cout << image << "i";
        else if (image < 0)
            cout << image << "i";
    } else {
        if (image == 0)
            cout << real;
        else if (image == 1)
            cout << real << "+i";
        else if (image == -1)
            cout << real << "-i";
        else if (image > 0)
            cout << real << "+" << image << "i";
        else if (image < 0)
            cout << real << image << "i";
    }
}

int main()
{
    double a, b, c, d;

    while (cin >> a >> b >> c >> d) {
        Complex x(a, b), y(c, d), z, w;
        z = x + y;
        z.show();
        cout << " ";
        w = x - y;
        w.show();
        cout << endl;
    }

    return 0;
}




 


 

参考代码二:

#include <iostream>
using namespace std;

class Complex
{

private:
    double real, image;

public:
    Complex(double r = 0, double i = 0);
    Complex& operator+(Complex&);
    Complex& operator-(Complex&);
    void show();

};

Complex::Complex(double r, double i)
{
    real = r;
    image = i;
}

Complex& Complex::operator+(Complex& c)
{
    Complex x;
    x.real = real + c.real;
    x.image = image + c.image;
    return x;
}

Complex& Complex::operator-(Complex& c)
{
    Complex x;
    x.real = real - c.real;
    x.image = image - c.image;
    return x;
}

void Complex::show()
{
    if (image == 0) {
        if (real == 0)
            cout << "0";
        else
            cout << real;
    } else if (image == 1) {
        if (real == 0)
            cout << "i";
        else
            cout << real << "+i";
    } else if (image == -1) {
        if (real == 0)
            cout << "-i";
        else
            cout << real << "-i";
    } else {
        if (real == 0)
            cout << image << "i";
        else {
            if (image > 0)
                cout << real << "+" << image << "i";
            else
                cout << real << image << "i";
        }
    }
}

int main()
{
    double a, b, c, d;

    while (cin >> a >> b >> c >> d) {
        Complex x(a, b), y(c, d), z, w;

        z = x + y;
        z.show();
        cout << " ";

        w = x - y;
        w.show();
        cout << endl;
    }

    return 0;
}




 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为实现复数类的++ --运算,我们需要对复数进行运算符重载。我们可以重载前置++和--运算符和后置++和--运算符。 下面是一个简单的复数类的定义: ```cpp class Complex { private: double real_; double imag_; public: Complex(double real = 0, double imag = 0) : real_(real), imag_(imag) {} double real() const { return real_; } double imag() const { return imag_; } Complex operator++(); //前置++ Complex operator--(); //前置-- Complex operator++(int); //后置++ Complex operator--(int); //后置-- }; Complex Complex::operator++() { real_++; imag_++; return *this; } Complex Complex::operator--() { real_--; imag_--; return *this; } Complex Complex::operator++(int) { Complex temp(*this); real_++; imag_++; return temp; } Complex Complex::operator--(int) { Complex temp(*this); real_--; imag_--; return temp; } ``` 前置++和--运算符直接将实部和虚部分别加/减1即可。 后置++和--运算符需要先保存当前对象的值,然后再将实部和虚部分别加/减1,最后返回保存的对象值。 下面是一个使用复数类的示例: ```cpp int main() { Complex a(1, 2); Complex b = ++a; Complex c = b--; cout << "a=" << a.real() << "+" << a.imag() << "i" << endl; cout << "b=" << b.real() << "+" << b.imag() << "i" << endl; cout << "c=" << c.real() << "+" << c.imag() << "i" << endl; return 0; } ``` 输出结果为: ``` a=2+3i b=2+3i c=3+4i ``` 可以看到,前置++和后置--运算符都可以正常工作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值