C++ 复数运算

本文介绍了如何在C++中创建复数类并实现复数的运算符重载,包括基本算术运算、自增自减运算以及流运算符。通过头文件Plural.h和源文件Plural.cpp的编写,详细展示了复数类的定义及使用方法,并在main.cpp中给出具体操作示例。
摘要由CSDN通过智能技术生成

完成复数类的运算符重载函数,包括:

  • 基本算术运算(+、-、*、/、=)

  • 自增自减运算(前置++、后置++、前置–、后置–)

  • 流运算符(<< 、>>)
    z=a+bi(a,b均为实数)的数称为复数,其中a称为实部,b称为虚部,i称为虚数单位。
    运算法则:

  • 加法: (a+bi)+(c+di)=(a+c)+(b+d)i

  • 减法:(a+bi)-(c+di)=(a-c)+(b-d)i

  • 乘法:(a+bi)(c+di)=ac+adi+bci+bdi2 = (ac-bd)+(bc+ad)i

  • 除法:(a+bi)/(c+di)=(ac+bd)/(c2+d2) +((bc-ad)/(c2+d2))i

由上面题目得知,该复数需要运用到重载函数。所以先写一个.h头文件,
如下Plural.h文件

#pragma once
#include <iostream>
using namespace std;
class Plural {
   
public:
	Plural(double x = 0, double y = 0) {
   
		before = x;
		after = y;
	}
//基本算术运算
	friend Plural operator + (const Plural&, const Plural&);
	friend Plural operator - (const Plural&, const Plural&);
	friend Plural operator * (const Plural&, const Plural&);
	friend Plural operator / (const Plural&, const Plural&);
	Plural& operator = (const Plural&);
//自增自减运算
	Plural& operator ++();//前置++
	Plural& operator ++(int);//后置++
	Plural& operator --();//前置--
	Plural& operator --(int);//后置--

//流运算符(<< 、>>)
	friend ostream&</
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值