最近看了一下Effect c++ 想要重新理解一下c++的机制以及内存布局,还是想搞这方面的研究,对底层不明白,总是有点很不踏实。
那就写了一个 Complex 类,这个类还是相对比较全的,基本上涵盖了一些运算。请大神飘过,勿噴,谢谢合作。
Complex.h文件
#ifndef __COMPLEX_H__ #define __COMPLEX_H__ #include <iostream> #include <string> 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<<b<<"i"; } else if(b==0)out<<a; else if(b>0)out<<a<<"+"<<b<<"i"; else out<<a<<b<<"i"; return out; } Complex operator + (double a,Complex &c){ Complex r(a) ; return r+c; } Complex operator - (double a,Complex &c){ Complex r(a) ; return r-c; } Complex operator * (double a,Complex &c){ Complex r(a) ; return r*c; } Complex operator / (double a,Complex &c){ Complex r(a) ; return r/c; }
Main.cpp文件
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex a(1,2); Complex b(2,3); cout<<a+b<<endl; cout<<a-b<<endl; cout<<a*b<<endl; cout<<a/b<<endl; cout<<a+b-a*b/a<<endl; return 0; }
测试结果:
可能写的仓促,没有想到的功能。逐渐完善。