编写一个复数类
发布网友
发布时间:2022-05-24 11:43
我来回答
共3个回答
热心网友
时间:2023-10-12 07:43
支持虚数与虚数之间,虚数与各种类型数之间直接加减乘除,让算虚数就像算普通类型int数据一样方便
show用于显示虚数,直接看
Plural就相当于int类型的虚数类型
double RealPart; //实部
double ImaginaryPart;//虚部
具体用法参见main
已通过编译
toString()=void show(Plural& b)函数
程序开始:
#include "windows.h"
#include "stdio.h"
#include "iostream.h"
//虚数算法
struct Plural{
double RealPart; //实部
double ImaginaryPart;//虚部
//////////问题:超过两次的运算统统无效////////////////
////////////////虚数与虚数计算///////////////////////
Plural operator+(Plural &s) //重载“+”,函数直接写在结构体内!
{
Plural c;
c.RealPart=RealPart+s.RealPart;
c.ImaginaryPart=ImaginaryPart+s.ImaginaryPart;
return c;
}
Plural operator-(Plural &s) //重载“-”,函数直接写在结构体内!
{
Plural c;
c.RealPart=RealPart-s.RealPart;
c.ImaginaryPart=ImaginaryPart-s.ImaginaryPart;
return c;
}
Plural operator*(Plural &s) //重载“*”,函数直接写在结构体内!
{
Plural c;
c.RealPart=RealPart*s.RealPart-ImaginaryPart*s.ImaginaryPart;
c.ImaginaryPart=RealPart*s.ImaginaryPart+ImaginaryPart*s.RealPart;
return c;
}
Plural operator/(Plural &s) //重载“*”,函数直接写在结构体内! (a+bj)/(c+dj)=(a+bj)*(c-dj)/(c+dj)*(c-dj);
{
Plural c;
UINT Dividend;
Dividend=s.RealPart*s.RealPart+s.ImaginaryPart*s.ImaginaryPart;
c=s;
c.ImaginaryPart=0-c.ImaginaryPart;
c=c*(*this); //(a+bj)*(c-dj)
c.RealPart=c.RealPart/Dividend;
c.ImaginaryPart=c.ImaginaryPart/Dividend;
return c;
}
//////////////////////////////////////////////
//////////虚数与double浮点型数据///////////////////
friend Plural operator+(Plural &s,const double& date);//Plural+double
friend Plural operator+(const double& date,Plural &s);//double+Plural
friend Plural operator-(Plural &s,const double& date);//double-Plural
friend Plural operator-(const double& date,Plural &s);//Plural-double
friend Plural operator*(Plural &s,const double& date);// Plural*double
friend Plural operator*(const double& date,Plural &s);// double*Plural
friend Plural operator/(Plural &s,const double& date);// Plural/double
friend Plural operator/(const double& date,Plural &s);// double/Plural
//////////虚数与float浮点型数据///////////////////
friend Plural operator+(Plural &s,const float& date);//Plural+float
friend Plural operator+(const float& date,Plural &s);//float+Plural
friend Plural operator-(Plural &s,const float& date);//float-Plural
friend Plural operator-(const float& date,Plural &s);//Plural-float
friend Plural operator*(Plural &s,const float& date);// Plural*float
friend Plural operator*(const float& date,Plural &s);// float*Plural
friend Plural operator/(Plural &s,const float& date);// Plural/float
friend Plural operator/(const float& date,Plural &s);// float/Plural
//////////////////////////////////////////////////////
//////////虚数与int浮点型数据///////////////////
friend Plural operator+(Plural &s,const int& date);//Plural+int
friend Plural operator+(const int& date,Plural &s);//int+Plural
friend Plural operator-(Plural &s,const int& date);//int-Plural
friend Plural operator-(const int& date,Plural &s);//Plural-int
friend Plural operator*(Plural &s,const int& date);// Plural*int
friend Plural operator*(const int& date,Plural &s);// int*Plural
friend Plural operator/(Plural &s,const int& date);// Plural/int
friend Plural operator/(const int& date,Plural &s);// int/Plural
};
Plural operator+(Plural& s,const double& date)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator+(const double& date,Plural& s)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator-(Plural& s,const double& date)
{
Plural c=s;
c.RealPart-=date;
return c;
}
Plural operator-(const double& date,Plural& s)
{
Plural c=s;
c.RealPart=date-c.RealPart;
return c;
}
Plural operator*(Plural& s,const double& date)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator*(const double& date,Plural& s)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator/(Plural& s,const double& date)
{
Plural c=s;
c.RealPart/=date;
c.ImaginaryPart/=date;
return c;
}
Plural operator/(const double& date,Plural& s)// a/(b+cj)=a(b-cj)/b^2+c^2
{
double Dividend;
Plural c=s;
Dividend=c.RealPart*c.RealPart+c.ImaginaryPart*c.ImaginaryPart;
c.ImaginaryPart=0-c.ImaginaryPart;
c=c*date;
c.ImaginaryPart/=Dividend;
c.RealPart/=Dividend;
return c;
}
/////////////////float类型////////////////////////
Plural operator+(Plural& s,const float& date)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator+(const float& date,Plural& s)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator-(Plural& s,const float& date)
{
Plural c=s;
c.RealPart-=date;
return c;
}
Plural operator-(const float& date,Plural& s)
{
Plural c=s;
c.RealPart=date-c.RealPart;
return c;
}
Plural operator*(Plural& s,const float& date)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator*(const float& date,Plural& s)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator/(Plural& s,const float& date)
{
Plural c=s;
c.RealPart/=date;
c.ImaginaryPart/=date;
return c;
}
Plural operator/(const float& date,Plural& s)// a/(b+cj)=a(b-cj)/b^2+c^2
{
double Dividend;
Plural c=s;
Dividend=c.RealPart*c.RealPart+c.ImaginaryPart*c.ImaginaryPart;
c.ImaginaryPart=0-c.ImaginaryPart;
c=c*date;
c.ImaginaryPart/=Dividend;
c.RealPart/=Dividend;
return c;
}
///////////////////////////////////////////////////////////////////////////
/////////////////int类型////////////////////////
Plural operator+(Plural& s,const int& date)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator+(const int& date,Plural& s)
{
Plural c=s;
c.RealPart+=date;
return c;
}
Plural operator-(Plural& s,const int& date)
{
Plural c=s;
c.RealPart-=date;
return c;
}
Plural operator-(const int& date,Plural& s)
{
Plural c=s;
c.RealPart=date-c.RealPart;
return c;
}
Plural operator*(Plural& s,const int& date)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator*(const int& date,Plural& s)
{
Plural c=s;
c.RealPart*=date;
c.ImaginaryPart*=date;
return c;
}
Plural operator/(Plural& s,const int& date)
{
Plural c=s;
c.RealPart/=date;
c.ImaginaryPart/=date;
return c;
}
Plural operator/(const int& date,Plural& s)// a/(b+cj)=a(b-cj)/b^2+c^2
{
double Dividend;
Plural c=s;
Dividend=c.RealPart*c.RealPart+c.ImaginaryPart*c.ImaginaryPart;
c.ImaginaryPart=0-c.ImaginaryPart;
c=c*date;
c.ImaginaryPart/=Dividend;
c.RealPart/=Dividend;
return c;
}
///////////////////////////////////////////////////////////////////////////
double AbsoluteValue(Plural& s)
{
return s.RealPart*s.RealPart+s.ImaginaryPart*s.ImaginaryPart;
}
void show(Plural& b)
{
if(b.ImaginaryPart>=0)
cout<<b.RealPart<<"+"<<b.ImaginaryPart<<"j"<<endl;
else
cout<<b.RealPart<<b.ImaginaryPart<<"j"<<endl;
}
void main()
{
float fl=1.002;
Plural a,b,c;//虚数a,b
a.RealPart=1;//虚数a实部a=1+2j
b.RealPart=1;
a.ImaginaryPart=2;//虚数a虚部
b.ImaginaryPart=2;//b=1+2j
cout<<"xushu1:";
show(a);//显示虚数a
cout<<"xushu2:";
show(b);
cout<<"xushu1-xushu2=";
c=a-b;//虚数减法
show(c);
cout<<"xushu1+xushu2=";
c=a+b;//
show(c);
cout<<"xushu1*xushu2=";
c=a*b;//
show(c);
cout<<"xushu1/xushu2=";
c=a/b;//
show(c);
cout<<"xushu2+1.002=";
b=b+1.002;//虚数加法
show(b);
cout<<"xushu2/f1+2.88=";
b=b/fl+2.88;
show(b);//除法加与实数相加
cout<<" fl=1.002";
cout<<endl;
system("pause");
}
结果如下(包括加减乘除):
xushu1:1+2j
xushu2:1+2j
xushu1-xushu2=0+0j
xushu1+xushu2=2+4j
xushu1*xushu2=-3+4j
xushu1/xushu2=1+0j
xushu2+1.002=2.002+2j
xushu2/f1+2.88=4.878+1.99601j
fl=1.002
请按任意键继续. . .
热心网友
时间:2023-10-12 07:43
实现类如下
实验结果请自己试
#include <iostream>
#include <iomanip>
class Complex
{
public:
Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //构造函数,初始化列表和默认参数
Complex(std::istream &is){is >> *this;}; //输入构造函数,调用自身的>>操作符
void SetReal(double _real); //更改实部的值
void SetImag(double _imag); //更改虚部的值
void SetVal(double _real,double _imag); //更改整个复数
inline double GetReal() const; //获取实部,常成员函数
inline double GetImag() const; //获取虚部,常成员函数
Complex& operator+=(const Complex &val); //成员操作符+=
Complex& operator*=(const Complex &val); //成员操作符-=
friend bool operator==(const Complex &lhs,const Complex &rhs); //友元函数,需访问私有数据
friend std::istream& operator>>(std::istream &,Complex &); //友元输入操作符,需私有数据
friend std::ostream& operator<<(std::ostream &,const Complex &); //友元输出操作符,需私有数据
private:
double real;
double imag;
};
Complex operator+(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数+操作
Complex operator*(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数*操作
//========================分割线,此线上面为定义代码,此线下面是实现代码===============================
inline bool operator==(const Complex &lhs,const Complex &rhs)
{
return (lhs.real == rhs.real) && (lhs.imag == rhs.imag);
}
inline bool operator!=(const Complex &lhs,const Complex &rhs)
{
return !(lhs==rhs);
}
inline Complex& Complex::operator+=(const Complex &val)
{
real += val.real;
imag += val.imag;
return *this;
}
inline Complex operator+(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret += rhs;
return ret;
}
inline Complex& Complex::operator*=(const Complex &val)
{
double tReal = real;
double tImag = imag;
real = tReal*val.real - tImag*val.imag;
imag = tReal*val.imag + tImag*val.real;
return *this;
}
inline Complex operator*(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret *= rhs;
return ret;
}
inline std::istream& operator>>(std::istream &is,Complex &com)
{
std::cout << "请输入实数部分:" ;
is >> com.real;
if(is)
{
std::cout << "请输入虚数部分:" ;
is >> com.imag;
if(is)
{
return is;
}
else
{
com = Complex(0.0);
}
}
else
{
com = Complex(0.0);
}
return is;
}
inline std::ostream& operator<<(std::ostream &os, const Complex &com)
{
os << "复数为:" << com.real << std::showpos << com.imag << "i" << std::endl;
return os;
}
inline double Complex::GetReal() const
{
return real;
}
inline double Complex::GetImag() const
{
return imag;
}
void Complex::SetReal(double _real)
{
real = _real;
}
void Complex::SetImag(double _imag)
{
imag = _imag;
}
void Complex::SetVal(double _real,double _imag)
{
real = _real;
imag = _imag;
}
热心网友
时间:2023-10-12 07:44
zuixin的C99标准已经支持复数运算了,这样的类已经显得没什么用了吧。