这是用C++实现的对与复数相关的运算符重载
首先在头文件(Complex.h)中创建复数类,加入成员属性,声明成员方法:
#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
friend ostream& operator<<(ostream& cout, const Complex& complex);
private:
double real, image;
public:
Complex(double real = 0.0, double image = 0.0);
Complex operator+(const Complex& complex);
Complex operator-(const Complex& complex);
Complex operator*(const Complex& complex);
Complex operator/(const Complex& complex);
Complex operator+(double n);
Complex operator-(double n);
Complex operator*(double n);
Complex operator/(double n);
friend Complex operator+(double n, const Complex& c);
friend Complex operator-(double n, const Complex& c);
friend Complex operator*(double n, const Complex& c);
friend Complex operator/(double n, const Complex& c);
};
然后在源文件(Complex.cpp)中实现成员方法:
#include "Complex.h"
Complex::Complex(double real, double image){
this->real = real;
this->image = image;
}
Complex Complex::operator+(const Complex& complex)
{
Complex temp(this->real + complex.real, this->image + complex.image);
return temp;
}
Complex Complex::operator-(const Complex& complex){
Complex temp(this->real - complex.real, this->image - complex.image);
return temp;
}
Complex Complex::operator*(const Complex& complex){
double a = this->real, b = this->image, c = complex.real, d = complex.image;
Complex temp(a * c - b * d, b * c + a * d);
return temp;
}
Complex Complex::operator/(const Complex& complex){
double a = this->real, b = this->image, c = complex.real, d = complex.image;
Complex temp;
if (c == 0 && d == 0)
cout << "除数不能为零!";
else {
temp.real = (a * c + b * d) / (c * c + d * d);
temp.image = (b * c + a * d) / (c * c, d * d);
}
return temp;
}
Complex Complex::operator+(double n){
Complex temp(this->real + n, this->image);
return temp;
}
Complex Complex::operator-(double n){
Complex temp(real - n, image);
return temp;
}
Complex Complex::operator*(double n){
Complex temp(real * n, image * n);
return temp;
}
Complex Complex::operator/(double n){
Complex temp;
if (n == 0)
cout << "除数不能为零!";
else {
temp.real = real / n;
temp.image = image / n;
}
return temp;
}
Complex operator+(double n, const Complex& c){
Complex temp(c.real + n, c.image);
return temp;
}
Complex operator-(double n, const Complex& c){
Complex temp(c.real - n, c.image);
return temp;
}
Complex operator*(double n, const Complex& c){
Complex temp(c.real * n, c.image * n);
return temp;
}
Complex operator/(double n, const Complex& c){
Complex temp;
if (c.real == 0 && c.image == 0)
cout << "除数不能为零!";
else {
temp.real = (c.real * n) / (c.real * c.real + c.image * c.image);
temp.image = (c.image * n) / (c.real * c.real + c.image * c.image);
}
return temp;
}
值得注意的是,在创建double型对复数运算符重载时,由于传参不同,因此需要创建为全局函数,且在头文件中声明的时候也应该声明成类的友元函数。
另附上求模函数和<<操作符重载(也需要先在类中声明):
double Complex::mod(){
return sqrt(this->real * this->real + this->image * this->image);
}
ostream& operator<<(ostream& cout, const Complex& complex){
if (complex.image != 0) {
cout << complex.real << " + " << complex.image << "i";
}
else {
cout << complex.real;
}
return cout;
}
测试代码
#include"Complex.h"
int main() {
Complex c1(3, 4);
Complex c2(6, 6);
cout << "c1 = " << c1 << '\t' << "c2 = " << c2 << endl;
cout << "c1 + c2 = " << c1 + c2 << endl;
cout << "c1 - c2 = " << c1 - c2 << endl;
cout << "c1 * c2 = " << c1 * c2 << endl;
cout << "c1 / c2 = " << c1 / c2 << endl;
cout << "c1的模为:" << c1.mod() << endl;
cout << "c1 + 10 = " << c1 + 10 << endl;
cout << "c1 - 10 = " << c1 - 10 << endl;
cout << "c1 * 10 = " << c1 * 10 << endl;
cout << "c1 / 10 = " << c1 / 10 << endl;
cout << "10 + c2 = " << 10 + c2 << endl;
cout << "10 - c2 = " << 10 - c2 << endl;
cout << "10 * c2 = " << 10 * c2 << endl;
cout << "10 / c2 = " << 10 / c2 << endl;
return 0;
}
都看到这了,不妨给博主点个小赞吧,谢谢啦~