写一个复数类Complex,(复数形如3.2+5.6i,2.9-1.3i,其中i*i=-1)。要求支持+-*/,++、--,到bool类型和string类型的转换,支持>>、<<运算符。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Complex
{
	float a;
	float b;
public:
	Complex():a(),b(){}
	Complex(const float& a, const float& b):a(a),b(b){}
	Complex operator+(const Complex& com2)
	{
		return Complex((a+com2.a),(b+com2.b));
	}
	Complex operator-(const Complex& com2)
	{
		return Complex((a-com2.a),(b-com2.b));
	}
	Complex operator*(const Complex& com2)
	{
		return Complex((a*com2.a),(b*com2.b));
	}
	Complex operator/(const Complex& com2)
	{
		return Complex((a/com2.a),(b/com2.b));
	}

	/*成员函数前置--:*/
	Complex& operator--()
	{
		--a;
		--b;
		return *this;
	}

	/*友元函数前置--:*/
	//friend Complex& operator--(Complex& com)
	//{
	//	--com.a;
	//	--com.b;
	//	return com;
	//}

	/*成员函数后置--:*/
	/*Complex operator--(int)
	{
	Complex temp(*this);
	a--;
	b--;
	return temp;
	}*/

	/*友元函数后置--:*/
	friend Complex operator--(Complex& com,int)//(Complex& com,int)中不加& 就不能改变自己的值
	{
		Complex temp(com);
		com.a--;
		com.b--;
		return temp;
	}

	Complex& operator=(const Complex& b)//必须是成员函数,与=相关的都必须是成员函数,还有[ ]和()
	{
		this->a=b.a;
		this->b=b.b;
		return *this;
	}
	friend ostream& operator<<(ostream&o,const Complex& com)//必须是友元函数
	{
		return o << com.a << " + " << com.b << "i";
	}
	friend istream& operator>>(istream& in,Complex& com)//必须是友元函数
	{
		return in >> com.a >> com.b ;//return in >> com.a >> " + " >> com.b >>"i"; 错误 没有这些符号的定义
	}
	//void show()
	//{
	//	char * str;
	//	int dec, sign, ndigits = 3; 
	//	str = _fcvt_s(a, ndigits, &dec, &sign);
	//	printf("Original number; %f\n" , a) ; 
	//	printf ("Converted string; %s\n",str);    
	//	printf ("Decimal place: %d\n" , dec) ; 
	//	printf ("Sign: %d\n" , sign) ;           
	//	//str = fcvt(b, ndigits, &dec, &sign);

	//}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Complex a(1,5);
	Complex b(5,6);
	a=a+b; 
	cout << a << endl;
	cout << --a << endl;
	cout << a-- << endl;
	cout << a << endl;
	Complex c;

	cout << "a:" << a << endl;
	cout << "c:" << c << endl;
	c=a--;
	cout << "c:" << c << endl;
	c=a;
	cout << "c:" << c << endl;
	cin >> c ;
	cout << "c:" << c << endl;
	c.show();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数是由一个实部和一个虚部组成的数字形式,其中虚部用数学符号 i 表示。实部和虚部都可以是实数或整数。 为了实现对复数的四则运算,需要创建一个复数类 complex。这个类应该包含以下成员函数: - 构造函数:初始化实部和虚部。 - 加法运算符重载:实现两个复数的加法操作。 - 减法运算符重载:实现两个复数的减法操作。 - 乘法运算符重载:实现两个复数的乘法操作。 - 除法运算符重载:实现两个复数的除法操作。 在复数类中,RealPart 表示实部,ImaginaryPart 表示虚部。对于实部和虚部,可以使用基本数据类型 double 来存储。 在加法运算符重载中,两个复数的实部相加,虚部相加。在减法运算符重载中,两个复数的实部相减,虚部相减。在乘法运算符重载中,实部相乘后减去虚部相乘的结果,虚部相乘后加上实部相乘的结果。在除法运算符重载中,将两个复数化简成 a+bi 和 c+di 的形式,然后进行乘法运算并约分。 示例代码如下: ``` class Complex { public: Complex(double real, double imaginary) : RealPart(real), ImaginaryPart(imaginary) {} Complex operator+(const Complex& other) const { double real = RealPart + other.RealPart; double imaginary = ImaginaryPart + other.ImaginaryPart; return Complex(real, imaginary); } Complex operator-(const Complex& other) const { double real = RealPart - other.RealPart; double imaginary = ImaginaryPart - other.ImaginaryPart; return Complex(real, imaginary); } Complex operator*(const Complex& other) const { double real = RealPart * other.RealPart - ImaginaryPart * other.ImaginaryPart; double imaginary = RealPart * other.ImaginaryPart + ImaginaryPart * other.RealPart; return Complex(real, imaginary); } Complex operator/(const Complex& other) const { double denominator = other.RealPart * other.RealPart + other.ImaginaryPart * other.ImaginaryPart; double real = (RealPart * other.RealPart + ImaginaryPart * other.ImaginaryPart) / denominator; double imaginary = (ImaginaryPart * other.RealPart - RealPart * other.ImaginaryPart) / denominator; return Complex(real, imaginary); } private: double RealPart; double ImaginaryPart; }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值