第九周 项目一:定义复数类的<<和>>运算符的重载

本文介绍了一个用于复数运算的C++类实现,包括加、减、乘、除以及基本的输入输出操作。通过实例展示了如何使用该类进行复数的四则运算,并演示了与实数的混合运算。
/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 作者:    李洋
* 完成日期: 2013年 4 月 26 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:无
* 程序输出:无
*/

#include <iostream>
using namespace std;

class Complex
{public:
	Complex(){real=0;imag=0;}
	Complex(double r,double i){real=r;imag=i;}
	Complex operator+(Complex &c);
	Complex operator-(Complex &c);
	Complex operator*(Complex &c);
	Complex operator/(Complex &c);
	friend Complex operator+(double &d,Complex &c);
	Complex operator+(double &d);
    friend Complex operator-(double &d,Complex &c);
	Complex operator-(double &d);
	friend Complex operator*(double &d,Complex &c);
	Complex operator*(double &d);
	friend Complex operator/(double &d,Complex &c);
	Complex operator/(double &d);
	friend Complex operator-(Complex &c);
	friend istream&operator>>(istream&,Complex&);
	friend ostream&operator<<(ostream&,Complex&);
 private:
	double real;
	double imag;
};

Complex Complex::operator+(Complex &c)
{
	return Complex(real+c.real,imag+c.imag);
}

Complex Complex::operator-(Complex &c)
{
	return Complex(real-c.real,imag-c.imag);
}

Complex Complex::operator*(Complex &c)
{
	return Complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);
}

Complex Complex::operator/(Complex &c)
{
	double d;
	d=c.real*c.real+c.imag*c.imag;
	return Complex((real*c.real+imag*c.imag)/d,(imag*c.real-real*c.imag)/d);
}

Complex operator+(double &d,Complex &c)
{
    return Complex(d+c.real,c.imag);
}
Complex Complex::operator+(double &d)
{
	return Complex(real+d,imag);
}

Complex operator-(double &d,Complex &c)
{
	return Complex(d-c.real,c.imag);
}
Complex Complex::operator-(double &d)
{
	return Complex(real-d,imag);
}

Complex operator*(double &d,Complex &c)
{
	return Complex(d*c.real,d*c.imag);
}
Complex Complex::operator*(double &d)
{
	return Complex(real*d,imag*d);
}

Complex operator/(double &d,Complex &c)
{
	return Complex(d/c.real,d/c.imag);
}
Complex Complex::operator/(double &d)
{
	return Complex(real/d,imag/d);
}

Complex operator-(Complex &c)
{
    return Complex(-c.real,-c.imag);
}

istream&operator>>(istream&input,Complex &c)
{
    cout<<"请输入复数的实部和虚部:";
    input>>c.real>>c.imag;
    return input;
}

ostream&operator<<(ostream &output,Complex &c)
{
    output<<"("<<c.real;
    if(c.imag>=0)
        output<<"+";
    output<<c.imag<<"i)"<<endl;
    return output;
}


int main()
{
    double d=2;
    Complex c1,c2,c3;
    cin>>c1;
	cout<<"c1=";
	cout<<c1;

	cin>>c2;
	cout<<"c2=";
	cout<<c2;

	c3=c1+c2;
	cout<<"c1+c2=";
	cout<<c3;

	c3=c1-c2;
	cout<<"c1-c2=";
	cout<<c3;

	c3=c1*c2;
	cout<<"c1*c2=";
	cout<<c3;

	c3=c1/c2;
	cout<<"c1/c2=";
	cout<<c3;

	c3=d+c1;
	cout<<"2+c1=";
	cout<<c3;

	c3=c1+d;
	cout<<"c1+2=";
	cout<<c3;

	c3=d-c1;
	cout<<"2-c1=";
	cout<<c3;

	c3=c1-d;
	cout<<"c1-2=";
	cout<<c3;

	c3=d*c1;
	cout<<"2*c1=";
	cout<<c3;

	c3=c1*d;
	cout<<"c1*2=";
	cout<<c3;

	c3=d/c1;
	cout<<"2/c1=";
	cout<<c3;

	c3=c1/d;
	cout<<"c1/2=";
	cout<<c3;

	c3=-c1;
	cout<<"-c1=";
	cout<<c3;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值