Complex_Number_H

本文详细介绍了复数类的设计与实现,包括复数的加、减、乘等运算符重载,以及复数的显示和字符串转换。通过具体的代码示例,展示了如何使用该复数类进行数学计算。
#ifndef Complex_Number_H
#define Complex_Number_H
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class ComplexNumber
{
private:
	double real,imag;
public:
	ComplexNumber();
	ComplexNumber(double x,double y);
	void display(string name);
	void display();
	void sets(double x,double y);
	void gets(double& x,double& y);
	void operator=(ComplexNumber other);
	void operator=(string str);
	ComplexNumber operator+(ComplexNumber& other);
	ComplexNumber operator-(ComplexNumber& other);
	ComplexNumber operator*(ComplexNumber& other);
	bool operator==(ComplexNumber& c1);
	friend ostream &operator<<(ostream& output,const ComplexNumber& C)
	{
		if(C.real!=0&&C.imag>0)
		{
			output << C.real << " + ";
			if(C.imag!=1)output << C.imag;
			output << "i" << endl;
		}
		else if(C.real==0&&C.imag!=0)
		{
			if(C.imag!=1&&C.imag!=-1)output << C.imag;
			else if(C.imag==-1)output<<"-";
			output << "i" << endl;
		}
		else if(C.real==0&&C.imag==0)
		{
			output << "0" << endl;
		}
		else if(C.real!=0&&C.imag==0)
		{
			output << C.real << endl;
		} 
		else
		{
			output << C.real << " - ";
			if(C.imag!=-1)output << -1*C.imag;
			output << "i" << endl;
		}
	}
};

ComplexNumber::ComplexNumber()
{
	real = 0;
	imag = 0;
}

ComplexNumber::ComplexNumber(double x,double y)
{
	real = x;
	imag = y;
}

void ComplexNumber::gets(double& x,double& y)
{
	x = real;
	y = imag;
}

void ComplexNumber::sets(double x,double y)
{
	real = x;
	imag = y;
}

ComplexNumber ComplexNumber::operator+(ComplexNumber& other)
{
	double otherreal,otherimag;
	other.gets(otherreal,otherimag);
	ComplexNumber ret(real + otherreal,imag + otherimag);
	return ret;
}

ComplexNumber ComplexNumber::operator-(ComplexNumber& other)
{
	double otherreal,otherimag;
	other.gets(otherreal,otherimag);
	ComplexNumber ret(real - otherreal,imag - otherimag);
	return ret;
}

ComplexNumber ComplexNumber::operator*(ComplexNumber& other)
{
	double otherreal,otherimag;
	other.gets(otherreal,otherimag);
	ComplexNumber ret(real * otherreal - imag * otherimag,real * otherimag + imag * otherreal);
	return ret;
}

void ComplexNumber::operator=(ComplexNumber other)
{
	double otherreal,otherimag;
	other.gets(otherreal,otherimag);
	real = otherreal;
	imag = otherimag;
}

void ComplexNumber::operator=(string str)
{
	string refined;
	for(int i = 0;i < str.size();i++)
	{
		if(str[i] != ' ')refined+=str[i]; 
	}

	stringstream ss(refined);
	if(refined.find("i") == -1)
	{
		ss >> real;
		imag = 0;
	}
	else if(refined.find("+") == -1 && refined.find("-") == -1)
	{
		if(!(ss >> imag))imag = 1;
		real = 0;
	}
	else if(refined.find("-") == -1)
	{
		char buf;
		ss >> real >> buf;
		if(!(ss >> imag))imag = 1;
	}
	else if(refined.find("+") == -1)
	{
		if(refined.find("-")==0)
		{
			if(!(ss >> imag))imag = -1;
			real = 0;
		}
		else
		{
			char buf;
			double temp;
			ss >> real >> buf;
			if(!(ss >> temp))imag = -1;
			else imag = -1 * temp;
		}
	}
}

bool ComplexNumber::operator==(ComplexNumber& c1)
{
	double x1,y1;
	c1.gets(x1,y1);
	if(x1 == real && y1 == imag)return true;
	return false;
}

void ComplexNumber::display()
{
	if(real!=0&&imag>0)
	{
		cout << real << " + ";
		if(imag!=1)cout << imag;
		cout << "i" << endl;
	}
	else if(real==0&&imag!=0)
	{
		if(imag!=1&&imag!=-1)cout << imag;
		else if(imag==-1)cout<<"-";
		cout << "i" << endl;
	}
	else if(real==0&&imag==0)
	{
		cout << "0" << endl;
	}
	else if(real!=0&&imag==0)
	{
		cout << real << endl;
	} 
	else
	{
		cout << real << " - ";
		if(imag!=-1)cout << -1*imag;
		cout << "i" << endl;
	}
}

void ComplexNumber::display(string name)
{
	if(real!=0&&imag>0)
	{
		cout << name << " is " << real << " + ";
		if(imag!=1)cout << imag;
		cout << "i" << endl;
	}
	else if(real==0&&imag!=0)
	{
		cout << name << " is ";

		if(imag!=1&&imag!=-1)cout << imag;
		else if(imag==-1)cout<<"-";
		cout << "i" << endl;
	}
	else if(real==0&&imag==0)
	{
		cout << name << " is " << "0" << endl;
	}
	else if(real!=0&&imag==0)
	{
		cout << name << " is " << real << endl;
	} 
	else
	{
		cout << name << " is " << real << " - ";
		if(imag!=-1)cout << -1*imag;
		cout << "i" << endl;
	}
}



#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值