说明:CComplex 实现复数的加减乘除运算 代码环境:VC6.0 CComplex 类.h文件: // CComplex.h: interface for the CComplex class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_) #define AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_ #include<iostream> #include<cmath> using namespace std; #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CComplex { public: void show(); double real; double image; public: CComplex(){real=0;image=0;} ~CComplex(){} CComplex(double a,double b){real=a;image=b;} double Getreal(){return real;} double Getimage(){return image;} double abs(){return sqrt(real*real+image*image);} CComplex operator +(CComplex &other); CComplex operator +(const double &other); CComplex operator +(const int &other); CComplex operator -(CComplex &other); CComplex operator -(const double &other); CComplex operator -(const int &other); CComplex operator *(CComplex &other); CComplex operator *(const double &other); CComplex operator *(const int &other); CComplex operator /(CComplex &other); CComplex operator /(const double &other); CComplex operator /(const int &other); void operator +=(CComplex &other); void operator +=(const double &other); void operator +=(const int &other); void operator -=(CComplex &other); void operator -=(const double &other); void operator -=(const int &other); void operator *=(CComplex &other); void operator *=(const double &other); void operator *=(const int &other); void operator /=(CComplex &other); void operator /=(const double &other); void operator /=(const int &other); CComplex operator =(CComplex &other); CComplex operator =(const double &other); CComplex operator =(const int &other); bool operator ==(CComplex &other); bool operator ==(const double &other); bool operator ==(const int &other); friend ostream& operator<<(ostream &os,CComplex &other); friend istream& operator>>(istream &is,CComplex &other); }; #endif // !defined(AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_) CComplex 类.cpp文件: // CComplex.cpp: implementation of the CComplex class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Complex.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// void CComplex::show() { if(real>0&&image<0) printf("%g%gj",real,image); else if(real>0&&image>0) printf("%g+%gj",real,image); else if(real<0&&image>0) printf("%g+%gj",real,image); else if(real<0&&image<0) printf("%g%gj",real,image); else if(real==0&&image!=0) printf("%gj",image); else if(real!=0&&image==0) printf("%g",real); else printf("0"); } CComplex CComplex::operator+(CComplex &other) { CComplex temp; temp.real=real+other.real; temp.image=image+other.image; return temp; } CComplex CComplex::operator +(const double &other) { CComplex temp; temp.real=real+other; temp.image=image; return temp; } CComplex CComplex::operator +(const int &other) { CComplex temp; temp.real=real+(double)other; temp.image=image; return temp; } CComplex CComplex::operator-(CComplex &other) { CComplex temp; temp.real=real-other.real; temp.image=image-other.image; return temp; } CComplex CComplex::operator -(const double &other) { CComplex temp; temp.real=real-(double)other; temp.image=image; return temp; } CComplex CComplex::operator -(const int &other) { CComplex temp; temp.real=real-(double)other; temp.image=image; return temp; } CComplex CComplex::operator*(CComplex &other) { CComplex temp; temp.real=(real*other.real-image*other.image); temp.image=(image*other.real+real*other.image); return temp; } CComplex CComplex::operator *(const double &other) { CComplex temp; temp.real=real*other; temp.image=image*other; return temp; } CComplex CComplex::operator *(const int &other) { CComplex temp; temp.real=real*(double)other; temp.image=image*(double)other; return temp; } CComplex CComplex::operator/(CComplex &other) { CComplex temp; temp.real=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image); temp.image=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image); return temp; } CComplex CComplex::operator /(const double &other) { CComplex temp; temp.real=real/other; temp.image=image/other; return temp; } CComplex CComplex::operator /(const int &other) { CComplex temp; temp.real=real/(double)other; temp.image=image/(double)other; return temp; } void CComplex::operator+=(CComplex &other) { this->real+=other.real; this->image+=other.image; } void CComplex::operator +=(const double &other) { this->real+=other; } void CComplex::operator +=(const int&other) { this->real+=(double)other; } void CComplex::operator-=(CComplex &other) { this->real-=other.real; this->image-=other.image; } void CComplex::operator -=(const double &other) { this->real-=other; } void CComplex::operator -=(const int &other) { this->real-=(double)other; } void CComplex::operator*=(CComplex &other) { this->real=(real*other.real-image*other.image); this->image=(image*other.real+real*other.image);; } void CComplex::operator *=(const double &other) { this->real=real*other; this->image=image*other; } void CComplex::operator *=(const int &other) { this->real=real*(double)other; this->image=image*(double)other; } void CComplex::operator/=(CComplex &other) { this->real=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image); this->image=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image); } void CComplex::operator /=(const double &other) { this->real=real/other; this->image=image/other; } void CComplex::operator /=(const int &other) { this->real=real/(double)other; this->image=image/(double)other; } CComplex CComplex::operator= (CComplex &other) { this->real=other.real; this->image=other.image; return *this; } CComplex CComplex::operator =(const double &other) { this->real=other; this->image=image; return *this; } CComplex CComplex::operator =(const int &other) { this->real=(double)other; this->image=image; return *this; } bool CComplex::operator ==(CComplex &other) { if(this->real=other.real&&this->image==other.image) return true; else return false; } bool CComplex::operator ==(const double &other) { if(this->real==other&&this->image==0) return true; else return false; } bool CComplex::operator ==(const int &other) { if(this->real==(double)other&&this->image==0) return true; else return false; } ostream& operator<<(ostream &os,CComplex &other) { other.show(); return cout; } istream& operator>>(istream &is,CComplex &other) { is>>other.real; is>>other.image; return cin; } 使用示例: // ComplexClass.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Complex.h" int main(int argc, char* argv[]) { CComplex test1(10,6); CComplex test2(5,3); cout<<test1*test2<<endl; cout<<test1+test2<<endl; cout<<test1/test2<<endl; cout<<test1-test2<<endl; printf("Hello World!/n"); return 0; } 参考文献:http://www.cppblog.com/abilitytao/articles/86109.html