Complex.h
#include<iostream>
using namespace std;
class Complex {
public:
Complex(double real = 0.0, double image = 0.0) {
this->real = real;
this->image = image;
}
~Complex() {
}
Complex operator-();
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);
double get_real() {
return real;
}
double get_image() {
return image;
}
void print() {
cout << "Real: " << real << " Image: " << image << endl;
}
private:
double real;
double image;
};
Complex.cpp
#include "Complex.h"
Complex Complex::operator-() {
real = -real;
image = -image;
return *this;
}
Complex Complex::operator=(const Complex &c) {
real = c.real;
image = c.image;
return *this;
}
Complex Complex::operator+(const Complex &c) {
Complex complex;
complex.real = real + c.real;
complex.image = image + c.image;
return complex;
}
Complex Complex::operator-(const Complex &c) {
Complex complex;
complex.real = real - c.real;
complex.image = image - c.image;
return complex;
}
Complex Complex::operator*(const Complex &c) {
Complex complex;
complex.real = real * c.real - (image * c.image);
complex.image = real * c.image + image * c.real;
return complex;
}
Complex Complex::operator/(const Complex &c) {
Complex complex;
if(c.image == 0) {
complex.real = real/c.real;
complex.image = image/c.real;
return complex;
}
double division = c.real * c.real + c.image * c.image;
Complex d = Complex(c.real, -c.image);
complex = *this * d;
complex.real /= division;
complex.image /= division;
return complex;
}
Complex Complex::operator+=(const Complex &c) {
*this = *this + c;
return *this;
}
Complex Complex::operator-=(const Complex &c) {
*this = *this - c;
return *this;
}
Complex Complex::operator*=(const Complex &c) {
*this = *this * c;
return *this;
}
Complex Complex::operator/=(const Complex &c) {
*this = *this / c;
return *this;
}
main.cpp
# include <iostream>
# include "Complex.cpp"
using namespace std;
int main() {
Complex c1 = Complex(5,6);
Complex c2 = Complex(3,4);
Complex cp1 = c1;
cp1.print();
Complex cp2 = -c2;
cp2.print();
Complex sum = c1 + c2;
cout << "加法 实部: " << sum.get_real() << " 虚部:" << sum.get_image() << "i" << endl;
Complex minus = c1 - c2;
cout << "减法 实部:" << minus.get_real() << " 虚部:" << minus.get_image() << "i" << endl;
Complex mul = c1 * c2;
cout << "乘法 实部:" << mul.get_real() << " 虚部:" << mul.get_image() << "i" << endl;
Complex div = c1 / c2;
cout << "除法 实部:" << div.get_real() << " 虚部:" << div.get_image() << "i" << endl;
Complex sum1 = c1;
sum1 +=c2;
cout << "+= 实部: " << sum1.get_real() << " 虚部:" << sum1.get_image() << "i" << endl;
Complex sum2 = c1;
sum2 -=c2;
cout << "-= 实部: " << sum2.get_real() << " 虚部:" << sum2.get_image() << "i" << endl;
Complex sum3 = c1;
sum3 *=c2;
cout << "*= 实部: " << sum3.get_real() << " 虚部:" << sum3.get_image() << "i" << endl;
Complex sum4 = c1;
sum4 /=c2;
cout << "/= 实部: " << sum4.get_real() << " 虚部:" << sum4.get_image() << "i" << endl;
return 0;
}