运用操作符重载实现数学中的复数类COMPLEX,能实现数学运算,赋值运算,前后序的自增自减运算,关系运算

发布时间:2024-05-22 00:08 发布:上海旅游网

问题描述:

求完整程序。。thxxxx

问题解答:

部分头文件:
#ifndef _COMPLEX_H_
#define _COMPLEX_H_

class Complex
{
public:
Complex():real(0.0),ima(0.0){}
Complex(const Complex& com):real(com.real),ima(com.ima){}
Complex(const double& r):real(r),ima(0){}
Complex(const double& r,const double& i):real(r),ima(i){}
Complex& operator=(const Complex&) ;
Complex operator++();
Complex operator++(int);

friend ostream& operator<<(ostream&,const Complex&);

Complex& operator/=(const Complex&);
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
friend Complex operator/(const Complex&,const Complex&);
friend Complex operator-(const Complex&,const Complex&);
friend Complex operator*(const Complex&,const Complex&);
friend Complex operator+(const Complex&,const Complex&);
friend bool operator==(const Complex&,const Complex&);
friend bool operator!=(const Complex&,const Complex&);

private:
double real;
double ima;
};

Complex operator/(const Complex&,const Complex&);
Complex operator-(const Complex&,const Complex&);
Complex operator*(const Complex&,const Complex&);
Complex operator+(const Complex&,const Complex&);
bool operator==(const Complex&,const Complex&);
bool operator!=(const Complex&,const Complex&);

#endif

部分源文件:
#include<iostream>
using namespace std;

#include "complex.h"

Complex& Complex::operator=(const Complex& com)
{
this->real=com.real;
this->ima=com.ima;
return *this;
}

Complex operator+(const Complex& lcom,const Complex& rcom)
{
Complex temp(lcom);
return temp+=rcom;
}

Complex& Complex::operator+=(const Complex& rcom)
{
this->real+=rcom.real;
this->ima+=rcom.ima;
return *this;
}

Complex Complex::operator++(int)
{
Complex temp(*this);
++this->real;
++this->ima;
return temp;
}

Complex Complex::operator++()
{
++this->real;
++this->ima;
return *this;
}

ostream& operator<<(ostream& os,const Complex& com)
{
os<<com.real<<"+"<<com.ima<<"i"<<flush;
return os;
}

热点新闻