一个数学上用到的复数类的实现
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:
- //========================================================================
- // 一个复数类,实现了基本的复数四则运算如+,-,*,/重载了流输入输出运算符等
- // 复数的格式是realPart + imaginaryPart * i
- // 其中i是-1的平方根, By 一剑 Loomman, QQ:28077188, QQ裙:30515563 ☆程序天堂☆
- //========================================================================
- #ifndef COMPLEX_H
- #define COMPLEX_H
- #include <iostream>
- using namespace std;
- class Complex
- {
- friend ostream &operator<<(ostream &, const Complex &);
- friend istream &operator>>(istream &, Complex &);
- public:
- Complex(double=0.0, double=0.0); //构造函数
- Complex operator+(const Complex &) const; //加
- Complex operator-(const Complex &) const; //减
- Complex operator*(const Complex &) const; //乘
- Complex operator/(const Complex &) const; //除
- Complex &operator=(const Complex &); //赋值
- bool operator==(const Complex &); //比较 等于==
- bool operator!=(const Complex &); //比较 不等于!=
- void Print() const; //输出
- private:
- double real; //实部
- double imaginary; //虚部
- };
- #endif //COMPLEX_H
Complex类实现文件Complex.cpp:
- //========================================================================
- // 一个复数类,实现了基本的复数四则运算如+,-,*,/重载了流输入输出运算符等
- // 复数的格式是realPart + imaginaryPart * i
- // 其中i是-1的平方根, By 一剑 Loomman, QQ:28077188, QQ裙:30515563 ☆程序天堂☆
- //========================================================================
- #include "complex.h"
- Complex::Complex(double r, double i)
- {
- real = r;
- imaginary = i;
- }
- Complex Complex::operator +(const Complex & cplx) const
- {
- Complex sum;
- sum.real = real + cplx.real;
- sum.imaginary = imaginary + cplx.imaginary;
- return sum;
- }
- Complex Complex::operator -(const Complex & cplx) const
- {
- Complex diff;
- diff.real = real - cplx.real;
- diff.imaginary = imaginary - cplx.imaginary;
- return diff;
- }
- Complex Complex::operator*(const Complex & cplx) const
- {
- Complex muti;
- muti.real = real * cplx.real - imaginary * cplx.imaginary;
- muti.imaginary = imaginary * cplx.real + real * cplx.imaginary;
- return muti;
- }
- Complex Complex::operator/(const Complex & cplx) const
- {
- Complex div;
- div.real = (real * cplx.real + imaginary * cplx.imaginary) / (cplx.real * cplx.real + cplx.imaginary * cplx.imaginary);
- div.imaginary = (imaginary * cplx.real - real * cplx.imaginary) / (cplx.real * cplx.real + cplx.imaginary * cplx.imaginary);
- return div;
- }
- Complex & Complex::operator =(const Complex & right)
- {
- real = right.real;
- imaginary = right.imaginary;
- return *this;
- }
- bool Complex::operator==(const Complex & right)
- {
- return (real==right.real && imaginary==right.imaginary);
- }
- bool Complex::operator!=(const Complex & right)
- {
- return !(real==right.real && imaginary==right.imaginary);
- }
- ostream &operator<<(ostream & output, const Complex & cplx)
- {
- output<<'('<<cplx.real<<", "<<cplx.imaginary<<')';
- return output;
- }
- istream &operator>>(istream & input, Complex & cplx)
- {
- input.ignore(); //跳过'('
- input>>cplx.real; //输入实部
- input.ignore(2); //跳过','和空格
- input>>cplx.imaginary; //输入虚部
- input.ignore(); //跳过')'
- return input;
- }
- void Complex::Print() const
- {
- cout<<'('<<real<<", "<<imaginary<<')';
- }
Complex类使用演示程序Main.cpp:
- #include <iostream>
- #include "complex.h"
- using namespace std;
- int main()
- {
- Complex x, y(4.3, 8.2), z(3.3, 1.1);
- cout<<"x:"<<x<<endl;
- cout<<"y:"<<y<<endl;
- cout<<"z:"<<z<<endl;
- x = y+z;
- cout<<endl<<"x=y+z:"<<endl;
- cout<<x<<"="<<y<<"+"<<z<<endl;
- x=y-z;
- cout<<endl<<"x=y-z:"<<endl;
- cout<<x<<"="<<y<<"-"<<z<<endl;
- x=y*z;
- cout<<endl<<"x=y*z:"<<endl;
- cout<<x<<"="<<y<<"*"<<z<<endl;
- x=y/z;
- cout<<endl<<"x=y/z:"<<endl;
- cout<<x<<"="<<y<<"/"<<z<<endl;
- cout<<endl<<"cin>>x:";
- cin>>x;
- cout<<" x:"<<x<<endl;
- int a;
- cin>>a;
- return a;
- }
演示程序运行结果如下:
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 ☆程序天堂☆ 请尊重作者原创,转载注明来自裂帛一剑博客,谢谢合作。