程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 重新認識c++(01),做個簡單的復數Complex類

重新認識c++(01),做個簡單的復數Complex類

編輯:C++入門知識

最近看了一下Effect c++ 想要重新理解一下c++的機制以及內存布局,還是想搞這方面的研究,對底層不明白,總是有點很不踏實。

那就寫了一個 Complex 類,這個類還是相對比較全的,基本上涵蓋了一些運算。請大神飄過,勿噴,謝謝合作。


Complex.h文件

#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include 
#include 
using namespace std ;
class Complex{
public:
    Complex();
    Complex(Complex&);
    Complex(double);
    Complex(double,double);
    Complex& operator = (Complex&);
    ~Complex();

    double getA();
    double getB();

    void setA(double);
    void setB(double);

    Complex operator + (double);
    Complex operator - (double);
    Complex operator * (double);
    Complex operator / (double);

    Complex& operator += (double);
    Complex& operator -= (double);
    Complex& operator *= (double);
    Complex& operator /= (double);

    bool operator == (double);
    bool operator == (Complex &c);

    Complex operator + (Complex&);
    Complex operator - (Complex&);
    Complex operator * (Complex&);
    Complex operator / (Complex&);

    Complex& operator += (Complex&);
    Complex& operator -= (Complex&);
    Complex& operator *= (Complex&);
    Complex& operator /= (Complex&);

private:
    double a;
    double b;
};

ostream& operator << (ostream &,Complex &);

Complex operator + (double,Complex &);
Complex operator - (double,Complex &);
Complex operator * (double,Complex &);
Complex operator / (double,Complex &);

Complex pow(Complex &,int);
#endif // __COMPLEX_H__

Complex.cpp文件

#include "Complex.h"
Complex::Complex():a(0),b(0){}
Complex::Complex(Complex& c):a(c.getA()),b(c.getB()){}
Complex::Complex(double _a):a(_a),b(0){}
Complex::Complex(double _a,double _b):a(_a),b(_b){}
Complex::~Complex(){};
Complex&  Complex::operator = (Complex&c){
    this->a = c.getA() ;
    this->b = c.getB() ;
    return *this;
}
double Complex::getA(){
    return this->a;
}
double Complex::getB(){
    return this->b;
};

void Complex::setA(double _a){
    this->a = _a ;
}
void Complex::setB(double _b){
    this->b = _b ;
}

bool Complex::operator == (double a){
    Complex c(a);
    return *this == c ;
}
bool Complex::operator == (Complex &c){
    if(this->a == c.getA() && this->b == c.getB())return true;
    return false;
}


Complex& Complex::operator += (Complex&c){
    this->a += c.getA() ;
    this->b += c.getB() ;
    return *this;
}
Complex& Complex::operator -= (Complex&c){
    this->a -= c.getA() ;
    this->b -= c.getB() ;
    return *this;
}
Complex& Complex::operator *= (Complex&c){
    this->a = ((this->a)*(c.getA()) - (this->b)*(c.getB()));
    this->b = ((this->a)*(c.getB()) + (this->b)*(c.getA()));
    return *this;
}
Complex& Complex::operator /= (Complex&c){
    if(c == 0)throw "error" ;
    else {
        *this = (*this) / c ;
    }
    return *this;
}

Complex& Complex::operator += (double a){
    Complex r(a) ;
    *this+=r;
    return *this;
}
Complex& Complex::operator -= (double a){
    Complex r(a) ;
    *this-=r;
    return *this;
}
Complex& Complex::operator *= (double a){
    Complex r(a) ;
    *this*=r;
    return *this;
}
Complex& Complex::operator /= (double a){
    Complex r(a) ;
    *this/=r;
    return *this;
}

Complex Complex::operator + (Complex&c){
    Complex r ;
    r.setA(this->a + c.getA()) ;
    r.setB(this->b + c.getB()) ;
    return r ;
}
Complex Complex::operator - (Complex&c){
    Complex r ;
    r.setA(this->a - c.getA()) ;
    r.setB(this->b - c.getB()) ;
    return r ;
}
Complex Complex::operator * (Complex&c){
    Complex r ;
    r.setA(((this->a)*(c.getA()) - (this->b)*(c.getB()))) ;
    r.setB(((this->a)*(c.getB()) + (this->b)*(c.getA()))) ;
    return r ;
}
Complex Complex::operator / (Complex&c){
    Complex r = c;
    if(c == 0 )throw "error" ;
    double t = c.getA()*c.getA() + c.getB()*c.getB() ;
    r.setB(-1*r.getB()) ;
    r = ((*this) * r) * (1.0/t) ;
    return r;
}

Complex Complex::operator + (double a){
    Complex r(a) ;
    return (*this)+r;
}
Complex Complex::operator - (double a){
    Complex r(a) ;
    return (*this)-r;
}
Complex Complex::operator * (double a){
    Complex r(a) ;
    return (*this)*r;
}
Complex Complex::operator / (double a){
    Complex r(a) ;
    return (*this)/r;
}

ostream& operator << (ostream &out,Complex &c){
    double a = c.getA() ;
    double b = c.getB() ;
    if(a == 0 )
    {
        if(b==0)out<<0;
        else out<

Main.cpp文件

#include 
#include "Complex.h"
using namespace std;

int main()
{
    Complex a(1,2);
    Complex b(2,3);
    cout<
測試結果:




可能寫的倉促,沒有想到的功能。逐漸完善。


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved