2021-08-16_C++实例复数的实现

系列文章目录

目前学习进度,C++基础语法学完。
下面分析侯捷的复数程序案例,原程序没有代码注释,在重写过程中我将尽量过期添加注释。



前言

提示:本案例涉及函数基础、构造函数、参数传递、引用、运算符重载,


提示:以下是本篇文章正文内容,下面案例可供参考

一、头文件

创建C++工程

#ifndef __MYCOMPLEX__  //检查是否已定义
#define __MYCOMPLEX__

class complex;         //类的声明,三个全局函数的声明
complex&
  __doapl (complex* ths, const complex& r);
complex&
  __doami (complex* ths, const complex& r);
complex&
  __doaml (complex* ths, const complex& r);
//三个函数为实现加减乘的操作
//注意返回值的类型为complex& 目的是为了能进行连运算,如:r1+r2+r3+r4
//若返回值类型为void,这只能将本次传入的数据进行处理,如:r1+r2


class complex    //定义复数
{
public:
  complex (double r = 0, double i = 0): re (r), im (i) { }
  complex& operator += (const complex&);
  complex& operator -= (const complex&); //成员函数,实现复数的运算
  complex& operator *= (const complex&); //涉及操作符重载
  complex& operator /= (const complex&);
  double real () const { return re; }  //对外接口函数,取实部
  double imag () const { return im; }  //对外接口函数,取虚部
private:       
  double re, im;   //有关数据,建议全部放在私有

  friend complex& __doapl (complex *, const complex&);
  friend complex& __doami (complex *, const complex&);
  friend complex& __doaml (complex *, const complex&);
  //friend 关键字定义为友元,所定义的函数可以使用类中private的对象
};


 //inline内联函数关键字,即为在函数调用处重写此函数,高效
//此函数定义复数的加法,第二个参数不会改变所以加上const,第一个参数不能加
//因为使用+=操作,数据保存在第一个参数里。加const会引起错误
inline complex&
__doapl (complex* ths, const complex& r)
{
  ths->re += r.re;
  ths->im += r.im;
  return *ths;   //ths是一个指针,*ths为取值操作,返回值类型为complex&
}
 
inline complex&
complex::operator += (const complex& r) //运算符重载,有一个隐藏this参数
{
  return __doapl (this, r);
}

inline complex&
__doami (complex* ths, const complex& r)
{
  ths->re -= r.re;
  ths->im -= r.im;
  return *ths;
}
 
inline complex&
complex::operator -= (const complex& r)  //-=的运算符重载
{
  return __doami (this, r);
}
 
inline complex&
__doaml (complex* ths, const complex& r)
{
  double f = ths->re * r.re - ths->im * r.im;
  ths->im = ths->re * r.im + ths->im * r.re;
  ths->re = f;
  return *ths;
}

inline complex&
complex::operator *= (const complex& r) //*=的运算符重载
{
  return __doaml (this, r);
}
 
inline double
imag (const complex& x)     //取实部函数
{
  return x.imag ();
}

inline double
real (const complex& x)    //取虚部函数
{
  return x.real ();
}


//+法运算符重载,返回的是值,不是引用,使用临时对象,返回引用错误
//typename();创建临时对象。
inline complex
operator + (const complex& x, const complex& y)
{
  return complex (real (x) + real (y), imag (x) + imag (y));
  //创建临时对象保存的值
}

inline complex
operator + (const complex& x, double y)
{
  return complex (real (x) + y, imag (x));
}

inline complex
operator + (double x, const complex& y)
{
  return complex (x + real (y), imag (y));
}

inline complex
operator - (const complex& x, const complex& y)
{
  return complex (real (x) - real (y), imag (x) - imag (y));
}

inline complex
operator - (const complex& x, double y)
{
  return complex (real (x) - y, imag (x));
}

inline complex
operator - (double x, const complex& y)
{
  return complex (x - real (y), - imag (y));
}

inline complex
operator * (const complex& x, const complex& y)
{
  return complex (real (x) * real (y) - imag (x) * imag (y),
			   real (x) * imag (y) + imag (x) * real (y));
}

inline complex
operator * (const complex& x, double y)
{
  return complex (real (x) * y, imag (x) * y);
}

inline complex
operator * (double x, const complex& y)
{
  return complex (x * real (y), x * imag (y));
}

complex
operator / (const complex& x, double y)
{
  return complex (real (x) / y, imag (x) / y);
}

inline complex
operator + (const complex& x)    //重载运算符为,正数
{
  return x;
}

inline complex
operator - (const complex& x)    //重载运算符为,负数
{
  return complex (-real (x), -imag (x));
}

inline bool                      //判断是否相等,返回值为布尔类型
operator == (const complex& x, const complex& y)
{
  return real (x) == real (y) && imag (x) == imag (y);
}

inline bool
operator == (const complex& x, double y)
{
  return real (x) == y && imag (x) == 0;
}

inline bool
operator == (double x, const complex& y)
{
  return x == real (y) && imag (y) == 0;
}

inline bool
operator != (const complex& x, const complex& y)
{
  return real (x) != real (y) || imag (x) != imag (y);
}

inline bool
operator != (const complex& x, double y)
{
  return real (x) != y || imag (x) != 0;
}

inline bool
operator != (double x, const complex& y)
{
  return x != real (y) || imag (y) != 0;
}

#include <cmath>

inline complex
polar (double r, double t)    //不知道,
{
  return complex (r * cos (t), r * sin (t));
}

inline complex
conj (const complex& x)    //定义共轭复数
{
  return complex (real (x), -imag (x));
}

inline double
norm (const complex& x)   //求模运算
{
  return real (x) * real (x) + imag (x) * imag (x);
}

#endif   //__MYCOMPLEX__

二、主程序

1.引入库

#include <iostream>
#include "complex.h"
using namespace std;

2. mian()函数

代码如下(示例):

int main()
{
  complex c1(2, 1);
  complex c2(4, 0);

  cout << c1 << endl;
  cout << c2 << endl;
  
  cout << c1+c2 << endl;
  cout << c1-c2 << endl;
  cout << c1*c2 << endl;
  cout << c1 / 2 << endl;
  
  cout << conj(c1) << endl;
  cout << norm(c1) << endl;
  cout << polar(10,4) << endl;
  
  cout << (c1 += c2) << endl;
  
  cout << (c1 == c2) << endl;
  cout << (c1 != c2) << endl;
  cout << +c2 << endl;
  cout << -c2 << endl;
  
  cout << (c2 - 2) << endl;
  cout << (5 + c2) << endl;
  
  return 0;
}


总结

一个简单的C++实现复数的案例。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值