小言_互联网的博客

复数的加、减、乘、除、求模求共轭复数的运算

264人阅读  评论(0)

复数的运算法则:
加法运算:
复数的加法按照以下规定的法则进行:设z1=a+bi,z2=c+di是任意两个复数,则它们的和是(a+bi)+(c+di)=(a+c)+(b+d)i;
例如:a = 1+2i,b = 3+4i  即可得 a+b = 4+6i
减法法则:
复数的减法按照以下规定的法则进行:设z1=a+bi,z2=c+di是任意两个复数,则它们的差是 (a+bi)-(c+di)=(a-c)+(b-d)i;两个复数的差依然是复数,它的实部是原来两个复数实部的差,它的虚部是原来两个虚部的差。
例如:a = 1+2i,b = 3+4i  即可得 a-b = -2i+2i;
乘法法则:
规定复数的乘法按照以下的法则进行:设z1=a+bi,z2=c+di(a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i;
例如:a = 1+2i,b = 3+4i 即可得 a*b = -5+10i
共轭复数:
两个实部相等,虚部互为相反数的复数互为共轭复数。当虚部不为零时,共轭复数就是实部相等,虚部相反,如果虚部为零,其共轭复数就是自身。
例如  a = 1+2i,a 的共轭复数为:1-2i;
模:
将复数的实部与虚部的平方和的正的平方根的值称为该复数的模,记作∣z∣,
对于复数 z = a + bi ,它的模 |z| = sqrt(aa+bb);
 

//Complex.h
#pragma once
class Complex
{
public:
	Complex();
	~Complex();
	int real;
	int img;
	double mod;
	void setReal(double real);
	void setImg(double img);
	void disp();
	void conjnum(Complex &b);
	double getReal();
	double getImg();
	void set(double r, double i);
	Complex(double r, double i);
	Complex operator+( Complex &b);
	Complex operator-( Complex &b);
	Complex operator*(Complex &b);
	Complex operator/( Complex &b);	
};

//Complex.cpp
#include "stdafx.h"
#include "Complex.h"

Complex::Complex()
	: real(0)
	, img(0)
{
}
Complex::~Complex()
{
}
void Complex::setReal(double real)
{
	this->real = real;
}
void Complex::setImg(double img)
{
	this->img = img;
}
void Complex::disp()
{
	if (img>=0)
	{
		cout <<  real << "+" << img << "i" << endl;
	}
	else
	{
		cout << real << "-" << img << "i" << endl;
	}
	
}
double Complex::getReal()
{
	return 0;
}
double Complex::getImg()
{
	return 0;
}
void Complex::set(double r, double i)
{
	this->real = r;
	this->img = i;
}
void Complex::conjnum(Complex &b)
{
	real = b.real;
	img = -(b.img);
}
Complex Complex::operator+(Complex &b) //定义复数相加函数
{
	Complex c;
	c.real = real + b.real;
	c.img = img + b.img;
	return c;
}
Complex Complex::operator-(Complex &b) //定义复数相减函数

{
	Complex c;
	c.real = real - b.real;
	c.img = img - b.img;
	return c;
}
Complex Complex::operator*(Complex &b) //定义复数相乘函数

{
	Complex c;
	c.real = real*b.real - img*b.img;
	c.img = img*b.real + real*b.img;
	return c;
}
Complex Complex::operator/(Complex &b) //定义复数相除函数
{
	Complex c;
	c.real = (real*b.real + img*b.img) / (b.real*b.real + b.img*b.img);
	c.img = (img*b.real - b.real*b.img) / (b.real*b.real + b.img*b.img);
	return c;
}
#include "stdafx.h"
#include "Complex.h"
double ModuleNum(Complex &b);
int _tmain(int argc, _TCHAR* argv[])
{
	Complex num1;
	Complex num2;
	num1.set(1.0, 2.0);
	num2.set(3.0, 4.0);
	cout << "num1=";
	num1.disp();
	cout << "\nnum2=";
	num2.disp();
	cout << endl;

	Complex res;
	cout << "num1+num2:\t";
	res = num1 + num2;
	res.disp();
	cout << "\nnum1-num2:\t";
	res = num1 - num2;
	res.disp();
	cout << "\nnum1*num2:\t";
	res = num1 * num2;
	res.disp();
	cout << "\nnum1/num2:\t";
	res = num1 / num2;
	res.disp();
	cout << endl;

	cout << "num1的共轭复数为:\t";
	res.conjnum(num1);
	res.disp();
	cout << endl;

	double mod;
	cout << "num2的模:  |num2|=";
	mod = ModuleNum(num2);
	cout << mod << endl;
	return 0;
}
double ModuleNum(Complex &b)
{
	return sqrt(b.real*b.real + b.img*b.img);
}


转载:https://blog.csdn.net/td2509575042/article/details/101036973
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场