第9周-运算符重载(二)-复数类中的运算符重载(续)

/* 
* Copyright (c) 2014, 烟台大学计算机学院 
* All rights reserved. 
* 文件名称:test.cpp 
* 作    者:刘畅 
* 完成日期:2015 年 5  月  4 日 
* 版 本 号:v1.0 
* 
* 问题描述:在第八周项目1<a target=_blank href="http://blog.csdn.net/liuchang54/article/details/45256303" target="_blank">第八周-运算符重载--实现复数类中的运算符重载-完整的类</a>上
           (1)再定义一目运算符 -,-c相当于0-c。
           (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,
            改造原程序中对运算结果显示方式,使程序读起来更自然。 
* 输入描述: ;
* 程序输出: 按要求输出。

代码如下:

#include <iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    Complex operator-();

    friend istream& operator >> (istream& input, Complex& c);
    friend ostream& operator << (ostream& output,const Complex& c);

    friend Complex operator+(Complex &c1, Complex &c2);
    friend Complex operator+(double d1, Complex &c2);
    friend Complex operator+(Complex &c1, double d2);
    friend Complex operator-(Complex &c1, Complex &c2);
    friend Complex operator-(double d1, Complex &c2);
    friend Complex operator-(Complex &c1, double d2);
    friend Complex operator*(Complex &c1, Complex &c2);
    friend Complex operator*(double d1, Complex &c2);
    friend Complex operator*(Complex &c1, double d2);
    friend Complex operator/(Complex &c1, Complex &c2);
    friend Complex operator/(double d1, Complex &c2);
    friend Complex operator/(Complex &c1, double d2);
    void display();
private:
    double real;
    double imag;
};

Complex Complex::operator-()
{
    return(0-*this);
}

istream& operator >> (istream& input, Complex& c)
{
    int a,b;
    char s,i;
    while(!((s=='+'||s=='-')&&i=='i'))
    {
        input>>a>>s>>b>>i;
    }
    c.real=a;
    c.imag=(s=='+')?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)
{
    Complex c;
    c.real=c1.real+c2.real;
    c.imag=c1.imag+c2.imag;
    return c;
}
Complex operator+(double d1, Complex &c2)
{
    Complex c(d1,0);
    return c+c2;
}
Complex operator+(Complex &c1, double d2)
{
    Complex c(d2,0);
    return c1+c;
}

Complex operator-(Complex &c1, Complex &c2)
{
    Complex c;
    c.real=c1.real-c2.real;
    c.imag=c1.imag-c2.imag;
    return c;
}
Complex operator-(double d1, Complex &c2)
{
    Complex c(d1,0);
    return c-c2;
}

Complex operator-(Complex &c1, double d2)
{
    Complex c(d2,0);
    return c1-c;
}

Complex operator*(Complex &c1, Complex &c2)
{
    Complex c;
    c.real=c1.real*c2.real-c1.imag*c2.imag;
    c.imag=c1.imag*c2.real+c1.real*c2.imag;
    return c;
}
Complex operator*(double d1, Complex &c2)
{
    Complex c(d1,0);
    return c*c2;
}
Complex operator*(Complex &c1, double d2)
{
    Complex c(d2,0);
    return c1*c;
}

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 d1, Complex &c2)
{
    Complex c(d1,0);
    return c/c2;
}
Complex operator/(Complex &c1, double d2)
{
    Complex c(d2,0);
    return c1/c;
}



int main()
{
    Complex c1(3,4),c2,c3;
    double d=11;
    cout<<"c1=";
    cout<<c1<<endl;
    cout<<"-c1=";
    cout<<-c1<<endl;
    cout<<"c2:";
    cin>>c2;
    cout<<"c2=";
    cout<<c2<<endl;
    cout<<"d="<<d<<endl<<endl;

    c3=c1+c2;
    cout<<"c1+c2="<<c3<<endl;
    cout<<"c1+d="<<(c1+d)<<endl;
    cout<<"d+c1="<<(d+c1)<<endl<<endl;

    c3=c1-c2;
    cout<<"c1-c2="<<c3<<endl;
    cout<<"c1-d="<<(c1-d)<<endl;
    cout<<"d-c1="<<(d-c1)<<endl<<endl;

    c3=c1*c2;
    cout<<"c1*c2="<<c3<<endl;
    cout<<"c1*d="<<(c1*d)<<endl;
    cout<<"d*c1="<<(d*c1)<<endl<<endl;

    c3=c1/c2;
    cout<<"c1/c2="<<c3<<endl;
    cout<<"c1/d="<<(c1/d)<<endl;
    cout<<"d/c1="<<(d/c1)<<endl<<endl;
    return 0;
}

运行结果:

第9周-运算符重载(二)-复数类中的运算符重载(续)_第1张图片

学习心得:

玩了两天终于开始做项目了。



你可能感兴趣的:(编程,C++,类,对象,博客)