执行+ =, - =,* =,/ =运算符(复数控制台计算器)

时间:2018-04-29 14:02:55

标签: c++ operators

我想请求帮助我的代码的一小部分,运算符的实现+ =, - =,* =,/ =。我真的没有想法如何实现这种情况,方法(ComplexNumber operator *= (ComplexNumber &operand) {})中的变量例如递增,新值存储在同一个变量中。我希望保留用于其他二元和一元运算符的语法风格。

// Complex Number project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "ComplexNumber.h"

using namespace std;

template <class T> class ComplexNumber {
    T re, im;

public:
    ComplexNumber(T re, T im) {
        this->re = re;
        this->im = im;
    }

    ComplexNumber operator + (ComplexNumber &operand) {
        ComplexNumber sum(this->re + operand.re, this->im + operand.im);
        return sum;
    }

    ComplexNumber operator - (ComplexNumber &operand) {
        ComplexNumber diff(this->re - operand.re, this->im - operand.im);
        return diff;
    }

    ComplexNumber operator * (ComplexNumber &operand) {
        ComplexNumber product((this->re * operand.re) - (this->im * operand.im), (this->re * operand.im) + (this->im * operand.re));
        return product;
    }

    ComplexNumber operator / (ComplexNumber &operand) {
        ComplexNumber quot(((this->re * operand.re) + (this->im * operand.im) / (operand.re * operand.re) + (operand.im * operand.im)), (this->re * operand.im) + (this->im * operand.re));
        return quot;
    }

    ComplexNumber operator += (ComplexNumber &operand) {
        ComplexNumber add(this->re - operand.re, this->im - operand.im);
        return add;
    }

    ComplexNumber operator -= (ComplexNumber &operand) {
        ComplexNumber sub(this->re - operand.re, this->im - operand.im);
        return sub;
    }

    ComplexNumber operator *= (ComplexNumber &operand) {
        return multipl;
    }

    ComplexNumber operator /= (ComplexNumber &operand) {
        return div;
    }

    ComplexNumber operator - () {
        return ComplexNumber(-re, -im);
    }

    ComplexNumber operator ~ () {
        return ComplexNumber(re, -im);
    }

    void print(void) {
        cout << noshowpos << re << showpos << im << "i" << noshowpos;
    }
};

int main(int argc, _TCHAR* argv[])
{
    ComplexNumber<double> a(2.1, 3.6);
    ComplexNumber<double> b(10, 20);
    ComplexNumber<double> c(0, 0);

    c = ~a;

    cout << "Complex number: ";
    a.print();
    cout << endl << "Complex conjugate number: ";
    c.print();
    cout << endl << endl;

    c = -a;

    cout << "Complex number: ";
    a.print();
    cout << endl << "Negative number: ";
    c.print();
    cout << endl << endl;

    c = a + b;

    cout << "Sum: ";
    a.print();
    cout << " + ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c = a - b;

    cout << "Difference: ";
    a.print();
    cout << " - ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c = a * b;

    cout << "Product: ";
    a.print();
    cout << " * ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c = a / b;

    cout << "Quotient: ";
    a.print();
    cout << " / ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c += a; // c = c + a

    cout << "Addition: ";
    a.print();
    cout << " / ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c -= a; // c = c - a

    cout << "Subtraction: ";
    a.print();
    cout << " / ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c *= a; // c = c * a

    cout << "Multiplication: ";
    a.print();
    cout << " / ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    c /= a; // c = c / a

    cout << "Division: ";
    a.print();
    cout << " / ";
    b.print();
    cout << " = ";
    c.print();
    cout << endl << endl;

    //cout << "Complex number: " << noshowpos << a.re << showpos << a.im << "i" << noshowpos;

    //a.print();
    //b.print();

    system("pause");
    return 0;
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

由于您已经拥有二进制算术运算符,因此可以使用它们。

赋值运算符应返回对*this的引用,操作数应为const或不是引用:

ComplexNumber& operator += (const ComplexNumber &operand) {
    *this = *this + operand;
    return *this;
}

从分配运算符开始并使用它们来实现二元运算符更常见,通常是非成员(如果你的类有转换构造函数,这会更有用):

ComplexNumber& operator += (const ComplexNumber &operand) {
    re += operand.re;
    im += operand.im;
    return *this;
}

// ...

template <class T>
ComplexNumber<T> operator + (ComplexNumber<T> lhs, const ComplexNumber<T>& rhs) {
    return lhs += rhs;
}