Complex.h 文件
#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include<iostream>
using namespace std;
class Complex{
public:
Complex(double real,double image)
:_real(real)
,_image(image)
{
}
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);
Complex& operator = (const Complex& c);
bool operator != (const Complex& c);
bool operator == (const Complex& c);
//输出指定复数
void PrintComplex(){
cout<<_real<<"+"<<"("<<_image<<"i"<<")\n"<<endl;
}
private:
double _real;
double _image;
};
#endif //__COMPLEX_H__
Complex.cpp实现文件:
#include<iostream>
#include"Complex.h"
using namespace std;
bool Complex::operator==(const Complex& c){
return ((_real == c._real) && (_image == c._image));
}
bool Complex::operator!=(const Complex& c){
return !(*this == c);
}
Complex& Complex::operator=(const Complex& c){
_real = c._real;
_image = c._image;
return *this;
}
//(a+bi)+(c+di)=(a+c)+(b+d)i
Complex Complex::operator+(const Complex& c){
Complex temp(*this);
temp._real = _real + c._real;
temp._image = _image + c._image;
return temp;
}
//(a+bi)-(c+di)=(a-c)+(b-d)i
Complex Complex::operator-(const Complex& c){
Complex temp(*this);
temp._real = _real - c._real;
temp._image = _image - c._image;
/*if(temp._image)*/
return temp;
}
//(a+bi)(c+di)=(ac-bd)+(bc+ad)i
Complex Complex::operator*(const Complex& c){
Complex temp(*this);
temp._real = (_real * c._real) - (_image * c._image);
temp._image = (_image * c._real) + (_real * c._image);
return temp;
}
//(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
Complex Complex::operator/(const Complex& c){
Complex temp(*this);
temp._real = ((_real * c._real) + (_image * c._image))/((c._real * c._real) + (c._image * c._image));
temp._image = ((_image * c._real) - (_real * c._image)) / ((c._real * c._real) + (c._image * c._image));
return temp;
}
Complex& Complex::operator+=(const Complex& c){
_real = _real + c._real;
_image = _image + c._image;
return *this;
}
Complex& Complex::operator-=(const Complex& c){
_real = _real - c._real;
_image = _image - c._image;
return *this;
}
Complex& Complex::operator*=(const Complex& c){
Complex temp(*this);
temp._real = (_real * c._real) - (_image * c._image);
temp._image = (_image * c._real) + (_real * c._image);
_real = temp._real;
_image = temp._image;
return *this;
}
Complex& Complex::operator/=(const Complex& c){
Complex temp(*this);
temp._real = ((_real * c._real) + (_image * c._image))/((c._real * c._real) + (c._image * c._image));
temp._image = ((_image * c._real) - (_real * c._image)) / ((c._real * c._real) + (c._image * c._image));
_real = temp._real;
_image = temp._image;
return *this;
}
test.cpp 测试文件:
#include<stdio.h>
#include<Windows.h>
#include"Complex.h"
void test(){
/*cout<<"复数c1"<<endl;
Complex c1(3,4);
c1.PrintComplex();
cout<<"复数c2"<<endl;
Complex c2(2,2);
c2.PrintComplex();
cout<<"测试复数的 '/' "<<endl;
(c1 / c2).PrintComplex();
cout<<"测试复数的 '/=' "<<endl;
(c1 /= c2).PrintComplex();
cout<<"复数c1"<<endl;
c1.PrintComplex();
cout<<"复数c3"<<endl;
Complex c3(2,4);
c3.PrintComplex();
cout<<"复数c4"<<endl;
Complex c4(3,2);
c4.PrintComplex();
cout<<"测试复数的 '*' "<<endl;
(c3 * c4).PrintComplex();
cout<<"测试复数的 '*=' "<<endl;
(c3 *= c4).PrintComplex();
cout<<"复数c3"<<endl;
c3.PrintComplex();*/
/*cout<<"复数c1"<<endl;
Complex c1(3,4);
c1.PrintComplex();
cout<<"复数c2"<<endl;
Complex c2(2,2);
c2.PrintComplex();
cout<<"测试复数的 '+' "<<endl;
(c1 + c2).PrintComplex();
cout<<"测试复数的 '+=' "<<endl;
(c1 += c2).PrintComplex();
cout<<"复数c1"<<endl;
c1.PrintComplex();
cout<<"复数c3"<<endl;
Complex c3(2,4);
c3.PrintComplex();
cout<<"复数c4"<<endl;
Complex c4(3,2);
c4.PrintComplex();
cout<<"测试复数的 '-' "<<endl;
(c3 - c4).PrintComplex();
cout<<"测试复数的 '-=' "<<endl;
(c3 -= c4).PrintComplex();
cout<<"复数c3"<<endl;
c3.PrintComplex();*/
cout<<"复数c1"<<endl;
Complex c1(3,4);
c1.PrintComplex();
cout<<"复数c2"<<endl;
Complex c2(2,2);
c2.PrintComplex();
int i = 3,j = 3;
i = (c1 == c2);
j = (c1 != c2);
cout<<"测试两个复数是否相等 "<<"测试两个复数是否不相等\n"<<endl;
cout<<" "<<i<<" "<<j<<endl;
cout<<"复数c3"<<endl;
Complex c3(2,4);
c3.PrintComplex();
cout<<"复数c4"<<endl;
Complex c4(3,2);
c4.PrintComplex();
cout<<"测试复数c4给c3赋值: "<<endl;
(c3 = c4).PrintComplex();
cout<<"复数c3"<<endl;
c3.PrintComplex();
}
int main(){
test();
system("pause");
return 0;
}
程序运行结果如下所示:
乘除:
加减:
相等赋值: