编写一个复数类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写一个复数类相关的知识,希望对你有一定的参考价值。

根据下列要求实现一个复数类ComplexNumber,并完成一个演示实例调用ComplexMulti
(ComplexNumberc)方法,计算(4+5i)×(3-4i)的乘积。
【提示】复数乘法:(a+bi) ×(c+di)=(ac-bd)+(bc+ad)i
(1)ComplexMulti(ComplexNumberc)和complexMulti(double c)方法要按讲义的格式
要求写注释
(2)复数类ComplexNumber 的属性
l realPart实部,代表复数的实数部分
l iPart虚部,代表复数的虚数部分
(3)复数类ComplexNumber 的方法
l ComplexNumber()构造函数,将实部、虚部都置为0。
l ComplexNumber(double r,double i)构造函数,创建复数对象的同时完成复数
的实部、虚部的初始化;r为实部初值,I为虚部初值
l getRealPart()获得复数对象的实部
l getIPart()获得复数对象的虚部
l setRealPart(double d)把当前复数对象的实部设置为给定的形参的数字
l setIPart(double d)把当前复数对象的虚部设置为给定形参的数字
l complexAdd(ComplexNumber c)当前复数对象与形参复数对象相加,所得的
结果也是复数值,返回给此方法的调用者。
l complexAdd(double c)当前复数对象与形参实数对象相加,所得的结果仍是复
数值,返回给此方法的调用者。
l complexMinus(ComplexNumber c)当前复数对象与形参复数对象相减,所得的
结果也是复数值,返回给此方法的调用者。
l complexMinus(double c)当前复数对象与形参实数对象相减,所得的结果也是
复数值,返回给此方法的调用者。
l complexMulti(ComplexNumber c)当前复数对象与形参复数对象相乘,所得的
结果也是复数值,返回给此方法的调用者。
l complexMulti(double c)当前复数对象与形参实数对象相乘,所得的结果也是复
数值,返回给此方法的调用者。
l toString()把当前复数对象的实部、虚部组合成a+bi 的字符串形式,其中a和b
分别为实部和虚部的数据。(要注意处理很多特殊情况,如当虚部为负数时的写法
等等!)
(4)将源代码和运行结果(命令行下截一幅图)打成一个rar 的包

支持虚数与虚数之间,虚数与各种类型数之间直接加减乘除,让算虚数就像算普通类型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
请按任意键继续. . .
参考技术A 实现类如下
实验结果请自己试

#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;
本回答被提问者和网友采纳
参考技术B zuixin的C99标准已经支持复数运算了,这样的类已经显得没什么用了吧。

复数错误输出

【中文标题】复数错误输出【英文标题】:complex number wrong output 【发布时间】:2021-03-25 10:06:43 【问题描述】:

我用一个名为 Complex 的类使用以下方法编写此代码:

一个。 Complex() // 默认构造函数返回 0

b.复数(float im, float real)

c。 float getImaginary() 常量

d。 float getReal() 常量

e。复数 add(const Complex& rhs) const

f。复数减法(const Complex& rhs) const

g.复数乘法(const Complex& rhs) const

#include <iostream>
using namespace std;

class Complex 
    private:
        float real;
        float imaginary;
    public:
        Complex();
        Complex(float real=0, float im=0);
        float GetReal() const;
        float GetImaginary() const;
        Complex Add(const Complex &rhs) const;
        Complex Subtract(const Complex &rhs) const;
        Complex Multiply(const Complex &rhs) const;
        void SetReal(float r);
        void SetImaginary(float i);
;
    Complex::Complex()
    
        real=imaginary=0;
    

    Complex::Complex(float realpart, float imaginarypart) 
    
    SetReal(real);
    SetImaginary(imaginarypart);
    

    void Complex::SetReal(float r) 
    
    real = r;
    

    void Complex::SetImaginary(float i) 
    
        imaginary = i;
    

    float Complex::GetReal() const
    
        return real;
    

    float Complex::GetImaginary() const 
        return imaginary;
    

    Complex Complex::Add(const Complex &rhs) const 
    return Complex(GetReal() + rhs.GetReal(), GetImaginary() + rhs.GetImaginary());
    

    Complex operator+(const Complex &rhs1, const Complex &rhs2) 
    return rhs1.Add(rhs2);
    

    Complex Complex::Subtract(const Complex &rhs) const 
     
    return Complex(GetReal() - rhs.GetReal(), GetImaginary() - rhs.GetImaginary());
    

    Complex operator-(const Complex &rhs1, const Complex &rhs2) 
    
    return rhs1.Subtract(rhs2); 
    

    Complex Complex::Multiply(const Complex &rhs) const
    
    return Complex(GetReal() * rhs.GetReal(), GetImaginary() * rhs.GetImaginary());
    
Complex operator*(const Complex &rhs1, const Complex &rhs2) 
    
    return rhs1.Multiply(rhs2);
    

int main() 

    Complex c();
    Complex x(-2,4);
    Complex y(5,6);
    Complex z = x + y;
    cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;

       Complex e = x - y;
    cout << e.GetReal() << '+' << e.GetImaginary() << 'i' << endl;

    Complex r = x * y;
    cout << r.GetReal() << '*' << r.GetImaginary() << 'i' << endl;
    return 0;

但我把这些输出

8.99944e-039+10i

8.99944e-039+-2i

8.99944e-039*24i

有人可以帮我解决这个错误吗?

【问题讨论】:

int main() int x; int x; 出现同样的错误。同一范围内只能声明一个同名变量。你不明白错误在说什么吗? 是的,我听不懂,也无法修复,你能重写吗? 为什么你不能修复它?错误是关于具有相同名称的不同对象,您可以给它们不同的名称。或者,如果你真的想使用相同的 z 声明一次 那是一个不同的问题。您在此处提出的问题与您的 Complex 课程无关(正如我所写的,您会遇到与 int 相同的错误)。当您的 Complex 课程被破坏时,这是一个不同的问题。当你得到意想不到的输出时,你应该在问题中包括输入、输出和预期输出。 2 个复数相乘不是 real1*real2 + i * imag1*imag2。 【参考方案1】:

您的代码中有 3 个问题:

Complex::Complex(float realpart, float imaginarypart) 

    SetReal(realpart);
    SetImaginary(imaginarypart);

导致警告:

<source>:24:28: warning: unused parameter 'realpart' [-Wunused-parameter]
   24 |     Complex::Complex(float realpart, float imaginarypart)
      |    

通过将realpart 传递给SetReal 或者使用成员初始化列表来修复它:

Complex::Complex(float realpart, float imaginarypart) : real(realpart),imaginary(imaginarypart)

接下来,这个:

Complex c();

是最令人头疼的解析。它声明了一个名为c 的函数,不带参数并重新调整Complex。它没有声明Complex 类型的变量c。写

 Complex c;
 Complex c;

调用默认构造函数。但是,您的类有两个默认构造函数:

    Complex();  
    Complex(float real=0, float im=0);

默认构造函数是可以在没有参数的情况下调用的构造函数,这两种情况都是如此。因此,您目前不能默认构造 Complex,构造函数是模棱两可的。我读到您的作业要求您提供两个构造函数:

    Complex();  
    Complex(float real, float im);  // no defaults !!

然后您将能够通过Complex c;Complex c; 默认构造一个Complex(但不能通过Complex c();,因为那是一个函数声明)。

修复这些后,我得到了几乎合理的输出:https://godbolt.org/z/x4xG8xzTf

最后,您将两个复数相乘的公式是错误的。我会留给你解决它。

【讨论】:

我无法理解复杂 c;复杂的 c;并且将两个复数相乘是错误的。我的输出好吗?你能帮我更详细的吗? @MertEmreÖnen google:最令人头疼的解析。并尝试将ii 相乘,结果应该是-1,但你的结果是i 好吧,我用使用过的cout &lt;&lt; r.GetReal() &lt;&lt; '*' &lt;&lt; "-" &lt;&lt; r.GetImaginary() &lt;&lt; endl; 修复了 -1 @largest_prime_is_463035818,我用谷歌搜索了最令人头疼的解析,我认为对我来说并不重要,我只是 C++ 的初学者。 @MertEmreÖnen 请务必了解Complex c(); 是错误的,因为它没有按照您的想法进行操作。你并没有真正修复它,你只是通过打印不同的输出来作弊,即使这样输出也是错误的。 0+1i * 0+1i-1+0i 不是 0+-1i 如果我使用Complex cc(); 是真的吗?是的,你说得对,我找不到好的多路复用解决方案。

以上是关于编写一个复数类的主要内容,如果未能解决你的问题,请参考以下文章

C++编程,定义一个复数类

java构造一个复数类

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

用JAVA定义个复数类

java里怎样表示复数啊