C++ 复数类的定义,包含初始化,求绝对值,输出,复数的加、减、乘、除、乘方、自加、自减等。

要求:

  • 复数类
  • 复数信息的初始化
  • 复数信息的输出
  • 求复数的绝对值
  • 实现复数的加、减、乘、除、乘方、自加、自减等。

代码:

#include<iostream>
#include<math.h>
using namespace std;
class Complex
{
private:
	double real;
	double image;
public:
	Complex()
	{
		real = 0;
		image = 0;
	}
	Complex(double r, double i)
	{
		real = r;
		image = i;
	}
	Complex(int r)
	{
		real = r;
		image = 0;
	}
	double abs()//求复数信息的绝对值
	{
		double d = sqrt(pow(real, 2) + pow(image, 2));
		return d;
	}
	void display()//复数信息的输出
	{
		cout << "(" << real << "," << image << ")" << endl;
	}
	Complex operator++(); //"++" 重载为成员函数
	Complex operator--(int); //"--" 重载为成员函数
	friend Complex operator+(Complex& c1, Complex& c2);//声明为友元函数,可以访问该类的私有成员
	friend Complex operator-(Complex& c1, Complex& c2);//声明为友元函数,可以访问该类的私有成员
	friend Complex operator*(Complex& c1, Complex& c2);//声明为友元函数,可以访问该类的私有成员
	friend Complex operator/(Complex& c1, Complex& c2);//声明为友元函数,可以访问该类的私有成员
	friend Complex operator^(Complex c1, int n);//声明为友元函数,可以访问该类的私有成员
};

Complex operator+(Complex& c1, Complex& c2)//复数的加法
{
	return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator-(Complex& c1, Complex& c2)//复数的减法
{
	return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator*(Complex& c1, Complex& c2)//复数的乘法
{
	double r = c1.real * c2.real - c1.image * c2.image;
	double i = c1.real * c2.image + c1.image * c2.real;
	return Complex(r, i);
}
Complex operator/(Complex& c1, Complex& c2)//复数的除法
{
	double r = (c1.real * c2.real + c1.image * c2.image) / (pow(c2.real, 2) + pow(c2.image, 2));
	double i = (c2.real * c1.image - c1.real * c2.image) / (pow(c2.real, 2) + pow(c2.image, 2));
	return Complex(r, i);
}

Complex operator^(Complex c1, int n)//复数的乘方
{    
	double abs_c1 = sqrt(c1.real * c1.real + c1.image * c1.image);
	double theta = atan(c1.image / c1.real);
	theta = theta * n;
	abs_c1 = pow(abs_c1, n);
	return Complex(abs_c1 * cos(theta), abs_c1 * sin(theta));
}

Complex Complex::operator++()//复数的自加
{
	this->real += 1;
	this->image += 1;
	return *this;
}
Complex Complex::operator--(int)//复数的自减
{
	this->real -= 1;
	this->image -= 1;
	return *this;
}

int main()
{
	Complex a(5, 8), b(2, 2), c(0, 0);
	++a;
	b--;
	a.display();//a(6,9)
	b.display();//b(1,1)
	c = a + b;
	c.display();
	c = a - b;
	c.display();
	c = a * b;
	c.display();
	c = a / b;
	c.display();
	c = a ^ 2;
	c.display();
	cout << c.abs() << endl;
	return 0;
}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++复数计算器,包含乘除,乘方,立方: ```c++ #include <iostream> #include <cmath> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 法运算符重载 Complex operator+(Complex const& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } // 法运算符重载 Complex operator-(Complex const& obj) { Complex res; res.real = real - obj.real; res.imag = imag - obj.imag; return res; } // 乘法运算符重载 Complex operator*(Complex const& obj) { Complex res; res.real = real * obj.real - imag * obj.imag; res.imag = real * obj.imag + imag * obj.real; return res; } // 除法运算符重载 Complex operator/(Complex const& obj) { Complex res; double denominator = obj.real * obj.real + obj.imag * obj.imag; res.real = (real * obj.real + imag * obj.imag) / denominator; res.imag = (imag * obj.real - real * obj.imag) / denominator; return res; } // 乘方 Complex pow(int n) { double r = sqrt(real * real + imag * imag); double theta = atan2(imag, real); Complex res; res.real = pow(r, n) * cos(n * theta); res.imag = pow(r, n) * sin(n * theta); return res; } // 立方 Complex cube() { return pow(3); } // 显示复数 void display() { if (imag >= 0) { cout << real << "+" << imag << "i" << endl; } else { cout << real << imag << "i" << endl; } } }; int main() { // 创建两个复数 Complex c1(3, 5); Complex c2(2, -4); // 法 Complex c3 = c1 + c2; cout << "c1 + c2 = "; c3.display(); // 法 Complex c4 = c1 - c2; cout << "c1 - c2 = "; c4.display(); // 乘法 Complex c5 = c1 * c2; cout << "c1 * c2 = "; c5.display(); // 除法 Complex c6 = c1 / c2; cout << "c1 / c2 = "; c6.display(); // 乘方 Complex c7 = c1.pow(2); cout << "c1^2 = "; c7.display(); // 立方 Complex c8 = c1.cube(); cout << "c1^3 = "; c8.display(); return 0; } ``` 运行结果: ``` c1 + c2 = 5+i c1 - c2 = 1+9i c1 * c2 = 26-2i c1 / c2 = -0.378788-0.454545i c1^2 = -16+30i c1^3 = -198+70i ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值