一、复数的概念:我们把形如z=a+bi(a,b均为实数)的数称为复数,其中a称为实部,b称为虚部,i称为虚数单位。当z的虚部等于零时,常称z为实数;当z的虚部不等于零时,实部等于零时,常称z为纯虚数。
二、复数计算公式
三、使用C++计算复数
#include<iostream>
using namespace std;
//复数类
class Complex
{
public:
//赋初值
void assgin(float real, float image)
{
this->m_Real = real;
this->m_Image = image;
}
//两个复数相加
void add(Complex& c,Complex c2)
{
c.m_Real = this->m_Real + c2.m_Real;
c.m_Image = this->m_Image + c2.m_Image;
}
//两个复数相减
void minus(Complex& c, Complex c2)
{
c.m_Real = this->m_Real - c2.m_Real;
c.m_Image = this->m_Image - c2.m_Image;
}
//两个复数相乘
void multiply(Complex& c, Complex c2)
{
c.m_Real = this->m_Real * c2.m_Real - this->m_Image * c2.m_Image;
c.m_Image = c2.m_Real * this->m_Image + this->m_Real * c2.m_Image;
}
//两个复数相除
void divide(Complex& c, Complex c2)
{
c.m_Real = (this->m_Real * c2.m_Real + this->m_Image * c2.m_Image) /
(c2.m_Real * c2.m_Real + c2.m_Image * c2.m_Image);
c.m_Image = (c2.m_Real * this->m_Image - this->m_Real * c2.m_Image) /
(c2.m_Real * c2.m_Real + c2.m_Image * c2.m_Image);
}
//判断实部、虚部是否为零,如果不为零输出复数C
void getComplex()
{
if (this->m_Real == 0)
{
cout << "复数C的实部为零时,称C为纯虚数" << endl;
}
else if (this->m_Image == 0)
{
cout << "复数C的虚部为零,称C为实数" << endl;
}
else
{
cout << "C= " << this->m_Real << " + " << this->m_Image << "i" << endl;
}
}
float m_Real; //实部
float m_Image; //虚部
};
int main()
{
//1.创建复数C1,并赋初值
Complex c1;
c1.assgin(8.0, 6.0); //c1赋初值
//2.创建复数C2,并赋初值
Complex c2;
c2.assgin(4.0, 3.0); //c2赋初值
//3.创建c3记录c1 + c2 后实部、虚部的值
Complex c3;
c1.add(c3, c2);
//c3.getComplex(); 测试c1 + c2 的值是否正确
//4.创建c4记录c1 * c2 后实部、虚部的值
Complex c4;
c1.multiply(c4, c2);
//c4.getComplex(); 测试 c1 * c2 的值是否正确
//5.创建c记录c4 * c3 后实部、虚部的值
Complex c;
c4.divide(c, c3);
c.getComplex(); //输出复数C结果
system("pause");
return 0;
}
四、结果为