一个数学中常用到的复数类的实现

一个数学上用到的复数类的实现

 

Complex类是我写的一个复数类,实现了基本的复数四则运算如+,-,*,/, 并重载了流输入,流输出运算符, 等于不等于比较运算符等。

 

复数的格式是realPart + imaginaryPart * i, 其中i是-1的平方根。 这里我先来温故而知新一下复数的运算法则:

 

加法:若复数A = a + bi,  B= c + di;  那么 A + B = (a + c) + (b + d)i;

减法:若复数A = a + bi,  B= c + di;  那么 A - B = (a - c) + (b - d)i;

 

对于乘法和除法:

 

好了,不再啰嗦了,下面来看程序代码:

Complex类头文件Complex.h:

  1. //========================================================================
  2. //       一个复数类,实现了基本的复数四则运算如+,-,*,/重载了流输入输出运算符等
  3. //                        复数的格式是realPart + imaginaryPart * i
  4. //     其中i是-1的平方根, By 一剑 Loomman, QQ:28077188, QQ裙:30515563 ☆程序天堂☆ 
  5. //========================================================================
  6. #ifndef COMPLEX_H
  7. #define COMPLEX_H
  8. #include <iostream>
  9. using namespace std;
  10. class Complex
  11. {
  12. friend ostream &operator<<(ostream &, const Complex &);
  13. friend istream &operator>>(istream &, Complex &);
  14. public:
  15.     Complex(double=0.0, double=0.0);                    //构造函数
  16.     Complex operator+(const Complex &) const;           //加
  17.     Complex operator-(const Complex &) const;           //减
  18.     Complex operator*(const Complex &) const;           //乘
  19.     Complex operator/(const Complex &) const;           //除
  20.     Complex &operator=(const Complex &);                //赋值
  21.     bool operator==(const Complex &);                   //比较 等于==
  22.     bool operator!=(const Complex &);                   //比较 不等于!=
  23.     void Print() const;                                 //输出
  24. private:
  25.     double  real;                                       //实部
  26.     double  imaginary;                                  //虚部
  27. };
  28. #endif //COMPLEX_H

Complex类实现文件Complex.cpp:

  1. //========================================================================
  2. //       一个复数类,实现了基本的复数四则运算如+,-,*,/重载了流输入输出运算符等
  3. //                        复数的格式是realPart + imaginaryPart * i
  4. //     其中i是-1的平方根, By 一剑 Loomman, QQ:28077188, QQ裙:30515563 ☆程序天堂☆ 
  5. //========================================================================
  6. #include "complex.h"
  7. Complex::Complex(double r, double i)
  8. {
  9.     real = r;
  10.     imaginary = i;
  11. }
  12. Complex Complex::operator +(const Complex & cplx) const
  13. {
  14.     Complex sum;
  15.     sum.real = real + cplx.real;
  16.     sum.imaginary = imaginary + cplx.imaginary;
  17.     return sum;
  18. }
  19. Complex Complex::operator -(const Complex & cplx) const
  20. {
  21.     Complex diff;
  22.     diff.real = real - cplx.real;
  23.     diff.imaginary = imaginary - cplx.imaginary;
  24.     return diff;
  25. }
  26. Complex Complex::operator*(const Complex & cplx) const
  27. {
  28.     Complex muti;
  29.     muti.real = real * cplx.real - imaginary * cplx.imaginary;
  30.     muti.imaginary = imaginary * cplx.real + real * cplx.imaginary;
  31.     return muti;
  32. }
  33. Complex Complex::operator/(const Complex & cplx) const
  34. {
  35.     Complex div;
  36.     div.real = (real * cplx.real + imaginary * cplx.imaginary) / (cplx.real * cplx.real + cplx.imaginary * cplx.imaginary);
  37.     div.imaginary = (imaginary * cplx.real - real * cplx.imaginary) / (cplx.real * cplx.real + cplx.imaginary * cplx.imaginary);
  38.     return div;
  39. }
  40. Complex & Complex::operator =(const Complex & right)
  41. {
  42.     real = right.real;
  43.     imaginary = right.imaginary;
  44.     return *this;
  45. }
  46. bool Complex::operator==(const Complex & right)
  47. {
  48.     return (real==right.real && imaginary==right.imaginary);
  49. }
  50. bool Complex::operator!=(const Complex & right)
  51. {
  52.     return !(real==right.real && imaginary==right.imaginary);
  53. }
  54. ostream &operator<<(ostream & output, const Complex & cplx)
  55. {
  56.     output<<'('<<cplx.real<<", "<<cplx.imaginary<<')';
  57.     return output;
  58. }
  59. istream &operator>>(istream & input, Complex & cplx)
  60. {
  61.     input.ignore();         //跳过'('
  62.     input>>cplx.real;       //输入实部
  63.     input.ignore(2);        //跳过','和空格
  64.     input>>cplx.imaginary;  //输入虚部
  65.     input.ignore();         //跳过')'
  66.     return input;
  67. }
  68. void Complex::Print() const
  69. {
  70.     cout<<'('<<real<<", "<<imaginary<<')';
  71. }

Complex类使用演示程序Main.cpp:

  1. #include <iostream>
  2. #include "complex.h"
  3. using namespace std;
  4. int main()
  5. {
  6.     Complex x, y(4.3, 8.2), z(3.3, 1.1);
  7.     cout<<"x:"<<x<<endl;
  8.     cout<<"y:"<<y<<endl;
  9.     cout<<"z:"<<z<<endl;
  10.     x = y+z;
  11.     cout<<endl<<"x=y+z:"<<endl;
  12.     cout<<x<<"="<<y<<"+"<<z<<endl;
  13.     x=y-z;
  14.     cout<<endl<<"x=y-z:"<<endl;
  15.     cout<<x<<"="<<y<<"-"<<z<<endl;
  16.     x=y*z;
  17.     cout<<endl<<"x=y*z:"<<endl;
  18.     cout<<x<<"="<<y<<"*"<<z<<endl;
  19.     x=y/z;
  20.     cout<<endl<<"x=y/z:"<<endl;
  21.     cout<<x<<"="<<y<<"/"<<z<<endl;
  22.     cout<<endl<<"cin>>x:";
  23.     cin>>x;
  24.     cout<<"     x:"<<x<<endl;
  25.     int a;
  26.     cin>>a;
  27.     return a;
  28. }

演示程序运行结果如下:

x:(0, 0)
y:(4.3, 8.2)
z:(3.3, 1.1)

 

x=y+z:
(7.6, 9.3)=(4.3, 8.2)+(3.3, 1.1)

 

x=y-z:
(1, 7.1)=(4.3, 8.2)-(3.3, 1.1)

 

x=y*z:
(5.17, 31.79)=(4.3, 8.2)*(3.3, 1.1)

 

x=y/z:
(1.91818, 1.84545)=(4.3, 8.2)/(3.3, 1.1)

 

cin>>x:(1.2345, 2.2345)
         x:(1.2345, 2.2345)

 

by Loomman, QQ:28077188, MSN: Loomman@hotmail.com QQ裙:30515563 ☆程序天堂☆ 请尊重作者原创,转载注明来自裂帛一剑博客,谢谢合作。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值