C++:运算符重载2(双目:复数的减法)

来源:互联网 发布:linux nmon 编辑:程序博客网 时间:2024/03/29 21:41

运算符重载2(双目:复数的减法)

Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte
Total Submit:460 Accepted:298

Description

定义复数类,用友元函数重载复数的减法。

Input

输入数据有若干行。每行上有四个整数,前两个表示一个复数的实部和虚部,后两个表示另一个复数的实部和虚部。

Output

对于每一组数据,输出两复数的差,格式参照样例输出。

Sample Input

1 2 3 4
4 3 2 1
1 2 3 2
3 2 3 1

Sample Output

-2-2i
2+2i
-2+0i
0+1i


代码块:

#include<iostream>using namespace std;class complex{    double real,image;public:    complex(double r=0,double i=0)    {        real=r;        image=i;    }    friend complex operator-(complex &a);    //friend complex operator+(double n,complex &a);    //friend complex operator+(complex &a,double n);    void disp()    {         if(image<0)        cout<<real<<image<<"i"<<endl;        else cout<<real<<"+"<<image<<"i"<<endl;    }complex operator-(complex&a){     real=real-a.real;     image=image-a.image;    return complex(real,image);}};int main(){    int a,b,c,d;    double t,tt;    while (cin>>a>>b>>c>>d)    {        complex a1(a,b),a2(c,d),a3;        a3=a1-a2;        a3.disp();    }    return 0;}
1 0
原创粉丝点击