复数运算符重载(C++)

这是用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;
}

在这里插入图片描述

都看到这了,不妨给博主点个小赞吧,谢谢啦~

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值