Complex类,运算符重载

题目描述

复数形式是a+bi,其中a和b是实数,i是。a和b分别被称为复数的实部和虚部。可以使用下列公式实现复数的加、减、乘、除:

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)

使用下面公式也可以获得复数的绝对值:

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

设计一个名为Complex的复数类,它可以用函数add、subtract、multiply、divide和abs实现复数的加、减、乘、除和取绝对值。toString函数实现以字符串形式表示的复数a+bi。如果b是0,只返回a。

该类有三个构造函数Complex (a, b)、Complex (a)和Complex ( )。Complex ( )生成一个表示原点的复数对象,Complex (a)生成一个b值为0的复数对象。函数getRealPart ( )和getImaginaryPart ( )分别返回复数的实部和虚部。

重载运算符+,-,,/,+=,-=,=,/=,[ ],一元+和-,前缀++和--,后缀++和--,<<,>>。

以非成员函数形式重载+,-,*,/。重载[ ],使得[0]返回a,[1]返回b。
编写一个测试程序:当用户输入两个复数后,程序显示它们的加、减、乘、除操作的结果。样例输出如下:

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;
}

/*构造函数,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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容

  • GeekBand C++ Week1 Notes A.OOP-面向对象编程 1基础:C语言 -变量variable...
    古来征战几人回阅读 489评论 0 0
  • 借我亡命天涯的勇敢 借我说得出口的旦旦誓言 借我孤绝如初见 借我不惧碾压的鲜活 借我生猛与莽撞不问明天 借我一束光...
    虞微阅读 202评论 0 0