第九周项目1——复数类中的运算符重载(续)

/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2015年5月13日
*版本号:v1.0
*
*问题描述:利用运算符重载进行复数的大小比较和计算(重载插入流运算符“<<”和流提取运算符“>>”
*/
#include<iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    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+(double &i,Complex &c2);
    friend Complex operator-(double &i,Complex &c2);
    friend Complex operator*(double &i,Complex &c2);
    friend Complex operator/(double &i,Complex &c2);
    friend Complex operator+(Complex &c1,double &i);
    friend Complex operator-(Complex &c1,double &i);
    friend Complex operator*(Complex &c1,double &i);
    friend Complex operator/(Complex &c1,double &i);
    Complex operator-();
    //实现输入、输出的运算符重载
    friend ostream& operator << (ostream& output, const Complex& c);
    friend istream& operator >> (istream& input, Complex& c);
private:
    double real;
    double imag;
};
//下面定义成员函数
Complex Complex::operator-()
{
    real=-real;
    imag=-imag;
    return *this;
}
//实现输入、输出的运算符重载

istream& operator >> (istream& input, Complex& c)
{
    double a,b;
    char x,y;
    do
    {
        cout<<"输入复数按照a+bi或a-bi的形式:";
        input>>a>>x>>b>>y;
    }
    while(!((x=='+'||x=='-')&&y=='i'));
    c.real=a;
    c.imag=(x=='+')?b:-b;
    return input;
}
ostream& operator << (ostream& output, const Complex& c)
{
    output<<"("<<c.real;
    if(c.imag>=0)   output<<"+";
    output<<c.imag<<"i)";
    return output;

}
Complex operator+(Complex &c1,Complex &c2)
{
    return Complex (c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(Complex &c1,Complex &c2)
{
    return Complex (c1.real-c2.real,c1.imag-c2.imag);
}
Complex operator*(Complex &c1,Complex &c2)
{
    return Complex (c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
Complex operator/(Complex &c1,Complex &c2)
{
    Complex c;
    c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    return c;
}
Complex operator+(double &i,Complex &c2)
{
    return Complex (i+c2.real,c2.imag);
}
Complex operator+(Complex &c1,double &i)
{
    return Complex (i+c1.real,c1.imag);
}
Complex operator-(double &i,Complex &c2)
{
    return Complex (i-c2.real,c2.imag);
}
Complex operator-(Complex &c1,double &i)
{
    return Complex (i-c1.real,c1.imag);
}
Complex operator*(double &i,Complex &c2)
{
    return Complex (i*c2.real,i*c2.imag);
}
Complex operator*(Complex &c1,double &i)
{
    return Complex (i*c1.real,i*c1.imag);
}
Complex operator/(double &i,Complex &c2)
{
    return Complex (i*c2.real/(c2.real*c2.real+c2.imag*c2.imag),i*c2.imag/(c2.real*c2.real+c2.imag*c2.imag));
}
Complex operator/(Complex &c1,double &i)
{
    return Complex (c1.real/i,c1.imag/i);
}
//下面定义用于测试的main()函数
int main()
{
    Complex c1,c2,c3;
    double d=11;
    cin>>c1;
    cout<<"c1="<<c1<<endl;
    cin>>c2;
    cout<<"c2="<<c2<<endl;
    cout<<"d="<<d<<endl;
    cout<<"-c1="<<(-c1)<<endl;
    c3=c1+c2;
    cout<<"c1+c2="<<c3<<endl;
    cout<<"c1+d="<<(c1+d)<<endl;
    cout<<"d+c1="<<(d+c1)<<endl;
    c3=c1-c2;
    cout<<"c1-c2="<<c3<<endl;
    cout<<"c1-d="<<(c1-d)<<endl;
    cout<<"d-c1="<<(d-c1)<<endl;
    c3=c1*c2;
    cout<<"c1*c2="<<c3<<endl;
    cout<<"c1*d="<<(c1*d)<<endl;
    cout<<"d*c1="<<(d*c1)<<endl;
    c3=c1/c2;
    cout<<"c1/c2="<<c3<<endl;
    cout<<"c1/d="<<(c1/d)<<endl;
    cout<<"d/c1="<<(d/c1)<<endl;
    return 0;
}


 

 

多运用才能熟悉。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值