c++复习一:复数运算的简单实现。

复数运算的简单实现。

程序很简单了。基本忘光了复数,重新了解了基本概念。如何在平面表示一个复数,复数的长度|x|=开根 a^2+b^2.和四则运算。

程序基本点:

封装和抽象:

1)封装成员数据,私有。 

2)分治思想,或树形数据结构,来解决运算。子结果和运算数 抽象为同一个类型。

main.cpp

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


using namespace std;

int main()
{
    compleNumber a(2,4);
    compleNumber b(3,5);
    compleNumber c=a/b+a;
    cout<<c<<endl;

    return 0;
}

 

complexNumber.h
#ifndef COMPLEXNUMBER_H_INCLUDED
#define COMPLEXNUMBER_H_INCLUDED

#include <iostream>

using namespace std;

class compleNumber
{
public:
    compleNumber(float,float);
    float getRealPart()const;
    float getComplexPart()const;
private:
    compleNumber();
    float a;
    float b;
};

compleNumber operator+(const compleNumber&,const compleNumber&);
compleNumber operator-(const compleNumber&,const compleNumber&);
compleNumber operator*(const compleNumber&,const compleNumber&);
compleNumber operator/(const compleNumber&,const compleNumber&);

ostream& operator<<(ostream& os,const compleNumber& complexNumb);

#endif // COMPLEXNUMBER_H_INCLUDED

complexNumber.cpp
//class compleNumber
//{
//public:
//    compleNumber(float,float);
//private:
//    compleNumber();
//    float a;
//    float b;
//};
//
//compleNumber operator+(const compleNumber&,const compleNumber&);
//compleNumber operator-(const compleNumber&,const compleNumber&);
//compleNumber operator*(const compleNumber&,const compleNumber&);
//compleNumber operator/(const compleNumber&,const compleNumber&);

#include "complexNumber.h"

compleNumber::compleNumber(float _a,float _b):a(_a),b(_b){}

ostream& operator<<(ostream& os,const compleNumber& complexNumb)
{
    return os<<complexNumb.getRealPart()<<"+"<<complexNumb.getComplexPart()<<"i";
}

float compleNumber::getRealPart()const
{
    return a;
}
float compleNumber::getComplexPart()const
{
    return b;
}

compleNumber operator+(const compleNumber& left,const compleNumber& right)
{
    compleNumber ret(left.getRealPart()+right.getRealPart(),left.getComplexPart()+right.getComplexPart());
    return ret;
}

compleNumber operator-(const compleNumber& left,const compleNumber& right)
{
    compleNumber ret(left.getRealPart()-right.getRealPart(),left.getComplexPart()-right.getComplexPart());
    return ret;
}

compleNumber operator*(const compleNumber& left,const compleNumber& right)
{
    compleNumber ret(left.getRealPart()*right.getRealPart()-left.getComplexPart()*right.getComplexPart(),left.getRealPart()*right.getComplexPart()+left.getComplexPart()*right.getRealPart());
    return ret;
}

compleNumber operator/(const compleNumber& left,const compleNumber& right)
{
    double PYfenmu=right.getComplexPart()*right.getComplexPart()+right.getRealPart()*right.getRealPart();
    double realpart=(left.getRealPart()*right.getRealPart()+left.getComplexPart()*right.getComplexPart())/PYfenmu;
    double complexPart=(left.getComplexPart()*right.getRealPart()-left.getRealPart()*right.getComplexPart())/PYfenmu;
    compleNumber ret(realpart,complexPart);
    return ret;
}

 

posted @ 2016-11-11 14:12  琴鸟  阅读(4189)  评论(0编辑  收藏  举报