Complex類,運算符重載

題目描述

復(fù)數(shù)形式是a+bi,其中a和b是實數(shù),i是。a和b分別被稱為復(fù)數(shù)的實部和虛部。可以使用下列公式實現(xiàn)復(fù)數(shù)的加、減、乘、除:

a + bi + c + di = (a + c) + (b + d) i

a + bi - (c + di) = (a - c) + (b - d) i

(a + bi) * (c + di) = (ac - bd) + (bc + ad) i

(a + bi) / (c + di) = (ac + bd) / (c2 + d2) + (bc - ad) i /
(c2 + d2)

使用下面公式也可以獲得復(fù)數(shù)的絕對值:

|a+bi|=√(a2+b2)

設(shè)計一個名為Complex的復(fù)數(shù)類,它可以用函數(shù)add、subtract、multiply、divide和abs實現(xiàn)復(fù)數(shù)的加、減、乘、除和取絕對值。toString函數(shù)實現(xiàn)以字符串形式表示的復(fù)數(shù)a+bi。如果b是0,只返回a。

該類有三個構(gòu)造函數(shù)Complex (a, b)、Complex (a)和Complex ( )。Complex ( )生成一個表示原點的復(fù)數(shù)對象,Complex (a)生成一個b值為0的復(fù)數(shù)對象。函數(shù)getRealPart ( )和getImaginaryPart ( )分別返回復(fù)數(shù)的實部和虛部。

重載運算符+,-,,/,+=,-=,=,/=,[ ],一元+和-,前綴++和--,后綴++和--,<<,>>。

以非成員函數(shù)形式重載+,-,*,/。重載[ ],使得[0]返回a,[1]返回b。
編寫一個測試程序:當(dāng)用戶輸入兩個復(fù)數(shù)后,程序顯示它們的加、減、乘、除操作的結(jié)果。樣例輸出如下:

Enter the first complex number:  3.5  5.5  ~Enter
Enter the second complex number: -3.5  1  ~Enter
(3.5 + 5.5i) + (-3.5 + 1.0i) = 0.0 + 6.5i
(3.5 + 5.5i) - (-3.5 + 1.0i) = 7.0 + 4.5i
(3.5 + 5.5i) * (-3.5 + 1.0i) = -17.75 + -15.75i
(3.5 + 5.5i) / (-3.5 + 1.0i) = -0.5094 + -1.7i
|3.5 + 5.5i| = 6.519202405202649

C++代碼

Complex.h

//
// Created by blue on 16-12-9.
//

#ifndef COMPLEXCLASS_COMPLEX_H
#define COMPLEXCLASS_COMPLEX_H

#include "iostream"
#include "sstream"
#include <string>

class Complex {
private:
    double realPart;
    double virtualPart;

public:
    Complex add(const Complex &c);
    Complex subtract(const Complex &c);
    Complex multiply(const Complex &c);
    Complex divide(const Complex &c);
    double abs();
    std::string toString();

public:
    Complex operator+(const Complex &c);
    Complex operator-(const Complex &c);
    Complex operator*(const Complex &c);
    Complex operator/(const Complex &c);
    Complex operator+=(const Complex &c);
    Complex operator-=(const Complex &c);
    Complex operator*=(const Complex &c);
    Complex operator/=(const Complex &c);
    double operator[](const int &index);
    Complex operator+();
    Complex operator-();
    Complex operator++(); //前置版本
    Complex operator--(); //前置版本
    Complex operator++(int t); //后置版本
    Complex operator--(int t); //后置版本
    friend std::ostream& operator<<(std::ostream &os,const Complex &c); //輸出運算附
    friend std::istream& operator>>(std::istream &is,Complex c); //輸入運算符

public:
    Complex();
    Complex(double realPart);

    Complex(double realPart, double virtualPart);

    double getRealPart() const;

    void setRealPart(double realPart);

    double getVirtualPart() const;

    void setVirtualPart(double virtualPart);

};


#endif //COMPLEXCLASS_COMPLEX_H

Complex.cpp

//
// Created by blue on 16-12-9.
//

#include "iostream"
#include "sstream"
#include "Complex.h"
#include <math.h>

/*方法*/
Complex Complex:: add(const Complex &c){
    Complex temp;
    temp.setRealPart(Complex::getRealPart()+c.getRealPart());
    temp.setVirtualPart(Complex::getVirtualPart()+c.getVirtualPart());
    return temp;
}
Complex Complex::subtract(const Complex &c){
    Complex temp;
    temp.setRealPart(Complex::getRealPart()-c.getRealPart());
    temp.setVirtualPart(Complex::getVirtualPart()-c.getVirtualPart());
    return temp;
}
Complex Complex::multiply(const Complex &c){
    Complex temp;
    temp.setRealPart(Complex::getRealPart()*c.getRealPart()-Complex::getVirtualPart()*c.getVirtualPart());
    temp.setVirtualPart(Complex::getVirtualPart()*c.getRealPart()+Complex::getRealPart()*c.getVirtualPart());
    return temp;
}
Complex Complex::divide(const Complex &c){
    Complex temp;
    temp.setRealPart((Complex::getRealPart()*c.getRealPart()+Complex::getVirtualPart()*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    temp.setVirtualPart((Complex::getVirtualPart()*c.getRealPart()-Complex::getRealPart()*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    return temp;
}
double Complex::abs(){
    return sqrt(pow(Complex::getRealPart(),2)+pow(Complex::getVirtualPart(),2));
}
std::string Complex::toString(){
    std::ostringstream ostring;

    if (Complex::getVirtualPart() == 0){
        ostring << Complex::getRealPart();
    }else if (Complex::getVirtualPart() < 0){
        ostring << Complex::getRealPart() << "-" << fabs(Complex::getVirtualPart()) << "i";
    }else{
        ostring << Complex::getRealPart() << "+" << Complex::getVirtualPart() << "i";
    }

    return ostring.str();
}

/*運算符重載*/
Complex Complex::operator+(const Complex &c){
    Complex tmp;
    tmp.setRealPart(Complex::getRealPart()+c.getRealPart());
    tmp.setVirtualPart(Complex::getVirtualPart()+c.getVirtualPart());
    return tmp;
}
Complex Complex::operator-(const Complex &c){
    Complex tmp;
    tmp.setRealPart(Complex::getRealPart()-c.getRealPart());
    tmp.setVirtualPart(Complex::getVirtualPart()-c.getVirtualPart());
    return tmp;
}
Complex Complex::operator*(const Complex &c){
    Complex tmp(Complex::getRealPart(),Complex::getVirtualPart());
    tmp.setRealPart(Complex::getRealPart()*c.getRealPart()-Complex::getVirtualPart()*c.getVirtualPart());
    tmp.setVirtualPart(Complex::getVirtualPart()*c.getRealPart()+Complex::getRealPart()*c.getVirtualPart());
    return tmp;
}
Complex Complex::operator/(const Complex &c){
    Complex temp;
    temp.setRealPart((Complex::getRealPart()*c.getRealPart()+Complex::getVirtualPart()*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    temp.setVirtualPart((Complex::getVirtualPart()*c.getRealPart()-Complex::getRealPart()*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    return temp;
}
Complex Complex::operator+=(const Complex &c){
    double a = Complex::getRealPart();
    double b = Complex::getVirtualPart();
    Complex::setRealPart((a*c.getRealPart()+b*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    Complex::setVirtualPart((b*c.getRealPart()-a*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    return *this;
}
Complex Complex::operator-=(const Complex &c){
    double a = Complex::getRealPart();
    double b = Complex::getVirtualPart();
    Complex::setRealPart(a-c.getRealPart());
    Complex::setVirtualPart(b-c.getVirtualPart());
    return *this;
}
Complex Complex::operator*=(const Complex &c){
    double a = Complex::getRealPart();
    double b = Complex::getVirtualPart();
    Complex::setRealPart(a*c.getRealPart()-b*c.getVirtualPart());
    Complex::setVirtualPart(b*c.getRealPart()+a*c.getVirtualPart());
    return *this;
}
Complex Complex::operator/=(const Complex &c){
    double a = Complex::getRealPart();
    double b = Complex::getVirtualPart();
    Complex::setRealPart((a*c.getRealPart()+b*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    Complex::setVirtualPart((a*c.getRealPart()-b*c.getVirtualPart())/(pow(c.getRealPart(),2)+pow(c.getVirtualPart(),2)));
    return *this;
}
double Complex::operator[](const int &index){
    if (index == 0){
        return Complex::getRealPart();
    }else if(index == 1){
        return Complex::getVirtualPart();
    }else{
        std::cout << "Invalid Index." << std::endl;
    }
}
Complex Complex::operator+(){
    Complex::setRealPart(fabs(Complex::getRealPart()));
    Complex::setVirtualPart(fabs(Complex::getVirtualPart()));
    return *this;
}
Complex Complex::operator-(){
    Complex::setRealPart(-Complex::getRealPart());
    Complex::setVirtualPart(-Complex::getVirtualPart());
    return *this;
}
Complex Complex::operator++(){ //前置版本
    Complex::setRealPart(Complex::getRealPart()+1);
    Complex::setVirtualPart(Complex::getRealPart()+1);
    return *this;
}
Complex Complex::operator--(){ //前置版本
    Complex::setRealPart(Complex::getRealPart()-1);
    Complex::setVirtualPart(Complex::getRealPart()-1);
    return *this;
}
Complex Complex::operator++(int t){ //后置版本
    Complex tmp(Complex::getRealPart(),Complex::getVirtualPart());
    this->setRealPart(this->getRealPart()+1);
    this->setVirtualPart(this->getVirtualPart()+1);
    return tmp;
}
Complex Complex::operator--(int t){ //后置版本
    Complex tmp(Complex::getRealPart(),Complex::getVirtualPart());
    this->setRealPart(this->getRealPart()-1);
    this->setVirtualPart(this->getVirtualPart()-1);
    return tmp;
}

/*輸入輸出重載*/
std::ostream& operator<<(std::ostream &os,const Complex &c){ //輸出運算符
    if (c.getVirtualPart() > 0){
        os << c.getRealPart() << "+" << c.getVirtualPart() << "i";
    }else if(c.getVirtualPart() == 0){
        os << c.getRealPart();
    }else{
        os << c.getRealPart() << "-" << fabs(c.getVirtualPart()) << "i";
    }
    return os;
}
std::istream& operator>>(std::istream &is,Complex c){ //輸入運算符
    double t1,t2;
    is >> t1 >> t2;
    c.setRealPart(t1);
    c.setVirtualPart(t2);
    return is;
}

/*構(gòu)造函數(shù),getter,setter*/
Complex::Complex() {
    Complex::realPart = 0;
    Complex::virtualPart = 0;
}

Complex::Complex(double realPart) : realPart(realPart) {
    Complex::realPart = realPart;
    Complex::virtualPart = 0;
}

Complex::Complex(double realPart, double virtualPart) : realPart(realPart), virtualPart(virtualPart) {
    Complex::realPart = realPart;
    Complex::virtualPart = virtualPart;
}


double Complex::getRealPart() const {
    return realPart;
}

void Complex::setRealPart(double realPart) {
    Complex::realPart = realPart;
}

double Complex::getVirtualPart() const {
    return virtualPart;
}

void Complex::setVirtualPart(double virtualPart) {
    Complex::virtualPart = virtualPart;
}

main.cpp

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

using namespace std;

int main(){

    double a0,a1;
    double b0,b1;
    cout << "Enter the first complex number: " << endl;
    cin >> a0 >> a1;
    cout << "Enter the second complex number:" << endl;
    cin >> b0 >> b1;

    Complex a(a0,a1);
    Complex b(b0,b1);


    cout << "(" << a.toString() << ")" << " + " << "(" << b.toString() << ")" << " = " << (a+b).toString() << endl;
    cout << "(" << a.toString() << ")" << " - " << "(" << b.toString() << ")" << " = " << (a-b).toString() << endl;
    cout << "(" << a.toString() << ")" << " * " << "(" << b.toString() << ")" << " = " << (a*b).toString() << endl;
    cout << "(" << a.toString() << ")" << " / " << "(" << b.toString() << ")" << " = " << (a/b).toString() << endl;
    cout << "|" << a.toString() << "|" << " = " << a.abs() << endl;

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • GeekBand C++ Week1 Notes A.OOP-面向?qū)ο缶幊?1基礎(chǔ):C語言 -變量variable...
    古來征戰(zhàn)幾人回閱讀 598評論 0 0
  • 借我亡命天涯的勇敢 借我說得出口的旦旦誓言 借我孤絕如初見 借我不懼碾壓的鮮活 借我生猛與莽撞不問明天 借我一束光...
    虞微閱讀 286評論 0 0

友情鏈接更多精彩內(nèi)容