/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 作者: 耿娜
* 完成日期: 2013年 4 月 24 日
* 版本号: 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+(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);
void display();
private:
double real;
double 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,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);
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i"<<")"<<endl;
}
int main()
{
double d=2;
Complex c1(4,4),c2;
cout<<"c1=";
c1.display();
c2=d+c1;
cout<<"2+c1=";
c2.display();
c2=c1+d;
cout<<"c1+2=";
c2.display();
c2=d-c1;
cout<<"2-c1=";
c2.display();
c2=c1-d;
cout<<"c1-2=";
c2.display();
c2=d*c1;
cout<<"2*c1=";
c2.display();
c2=c1*d;
cout<<"c1*2=";
c2.display();
c2=d/c1;
cout<<"2/c1=";
c2.display();
c2=c1/d;
cout<<"c1/2=";
c2.display();
c2=-c1;
cout<<"-c1=";
c2.display();
return 0;
}
第八周项目一
最新推荐文章于 2023-12-29 19:52:19 发布