问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

编写一个复数类

发布网友 发布时间: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标准已经支持复数运算了,这样的类已经显得没什么用了吧。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
iOS 16.1.1值得更新升级吗 ios16.1.1正式版建议更新吗 iOS 16.1.1修复了哪些问题 ios16.1.1值得更新吗?ios 16.1.1新增内容及更新建议 iPhone 12升级iOS 16.1.1之后好用吗 iOS 16.1.1更新内容介绍 iOS 16.1正式版新增了哪些新功能?iOS 16.1正式版值得升级吗? ios 16.1正式版解决了哪些问题和BUG ...森先生是他“最好的老师” 你喜欢这样的老师吗? ... 你喜欢怀特森这样老师吗?说说你的看法.250字 C++ no matching function for call to......... JAVA面向对象属性简便写法,如图哦P221.JAVA里面我要给很多很多变量一个一个的赋值,有没有什么办法能使 关于C++中类模板的问题 谁能帮忙翻译一下下面这段文字,翻得好可以再加分 求大师翻译下这段话。 关于许嵩的英文作文 批处理:set创建变量之后怎么删除 求解这段的翻译 苹果和ibm笔记本电脑哪个好 输尿管疼怎么回事 在笔记本电脑中,IBM这个品牌怎么样? 输尿管结石激光碎石手术后两星期后仍有血块和轻微疼痛感 企业电子邮箱是什么?_? 肾结石手术痛吗 公司开设邮箱选哪个? 输尿管结石术后为何还会疼痛? 老年人肱骨外上髁骨折怎么办? 右肱骨外上髁骨折,对位对线可,余无殊 右肱骨外髁骨折,肱骨滑车骺软骨挫伤,右肘关节周围软组织肿胀。严重吗? 速求骨科损伤并发症的原因! 批处理执行指定目录下的随机程序 太阳象征了什么 太阳象征什么作文 艾青太阳象征着什么 本诗中的太阳代表了什么表达了作者什么 太阳的话中太阳象征着什么 太阳象征着什么~月亮又象征着什么 【抖音】最强电音合集220首的歌,求百度网盘下载资源 请大家帮忙弄个通达信的阳后双阴阳选股公式源码~ 好的还加分!! 通达信经典实用选股公式(125个常用公式源码) 通达信导出公式如何直接看见源码 求通达信公式代码 我房间的门上被我贴满了贴纸,撕掉后还有残余,怎么才能把它们弄干净呢。 治疗慢粒白血病需要花多少钱? 我妈妈被查出来了慢性粒细胞白血病,现在每个月的药费是三千多块,我们家是农村户口,家里买了新农合保险 治疗慢性粒细胞白血病需要多少钱 白血病慢粒快粒怎么分?白血病慢粒治疗的话要用多少钱?请专业点的人士回答....谢谢. 治疗慢性粒细胞白血病大概需要多少钱?费用是不是很高? 慢性白血病的治疗费用要用多少 慢粒早期白血病 现在配型全相合(是我哥哥的骨髓)在武汉协和医院移植的话费用大概要多少