(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r=0,double i=0):real(r),imag(i)
{
};
friend Complex operator +(Complex &c1,Complex &c2);
friend Complex operator +(Complex &c,double i);
friend Complex operator +(double i,Complex &c);
friend ostream& operator << (ostream&,Complex&); //声明重载运算符“<<”
friend istream& operator >> (istream&,Complex&); //声明重载运算符“>>”
private:
double real;
double imag;
};
Complex operator +(Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator +(Complex &c,double i)
{
return Complex(i+c.real,c.imag);
}
Complex operator +(double i,Complex &c)
{
return Complex(i+c.real,c.imag);
}
ostream& operator << (ostream& output,Complex& c) //定义重载运算符“<<”
{
output<<"("<<c.real<<"+"<<c.imag<<"i)";
return output;
}
istream& operator >> (istream& input,Complex& c) //定义重载运算符“>>”
{
cout<<"input real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
int main( )
{
Complex c1(3,4),c2(3,2),c3,c4,c5;
c3=c1+c2;
c4=5+c2;
c5=c1+5;
cout<<"c3="<<c3<<endl;
cout<<"c4="<<c4<<endl;
cout<<"c5="<<c5<<endl;
return 0;
}
代码为:
#include <iostream> //用VC++时改为∶ #include <iostream.h>
using namespace std; //用VC++时为取消此行
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator+(int &i);
friend Complex operator+(int&,Complex &);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c)
{return Complex(real+c.real,imag+c.imag);}
Complex Complex::operator+(int &i)
{return Complex(real+i,imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
Complex operator+(int &i,Complex &c)
{return Complex(i+c.real,c.imag);}
int main()
{Complex c1(3,4),c2(5,-10),c3;
int i=5;
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=i+c1;
cout<<"i+c1=";
c3.display();
c3=c1+i;
cout<<"c1+i=";
c3.display();
return 0;
}
扩展资料:
注意事项
可以用下列结构描述复数信息;
struct complex
{
int real;
int image;
};
试写出两个通用函数,分别用来求两复数的和与积。其函数原型分别为:
struct complex add(struct complex c1,struct complex c2);
即参数和返回值用结构变量。
输入:
输入数据有若干行。每行上有四个整数,前两个表示一个复数的实部和虚部,后两个表示另一个复数的实部和虚部。
#include<iostream>
usingnamespacestd;
classComplex
{
public:
Complex(){real=0;imag=0;}//默认构造函数
Complex(doubler){real=r;imag=0;}//转换构造函数
Complex(doubler,doublei){real=r;imag=i;}//构造函数
friendComplexoperator+(Complexc1,Complexc2);//重载运算符+函数为友元函数
voiddisplay();//定义display函数
private:
doublereal;
doubleimag;
};
//重载运算符+函数
Complexoperator+(Complexc1,Complexc2)
{
returnComplex(c1.real+c2.real,c1.imag+c2.imag);
}
//display函数
voidComplex::display()
{
cout<<"("<<real;
if(imag>=0)cout<<"+";
cout<<imag<<"i)"<<endl;
}
intmain()
{
Complexc1(4,5),c2(3,-7),c3;//定义Complex类对象c1,c2,c3
inti;
c3=c1+c2;
c3.display();
cout<<"Pleaseenternumber:";
cin>>i;
c3=i+c1;
c3.display();
c3=c1+i;
c3.display();
system("pause");
return0;
}
扩展资料:
double用法
double是C语言中的双精度浮点数类型,用来表示实数。
1定义:
doublevar_name;
这样定义一个名字为var_name的double类型变量。
2赋值:
var_name=100.325;
同其它类型的赋值语句一样,对var_name赋值用=运算符,右侧可以是任意表达式。
3输入:
scanf("%lf",&var_name);
%lf格式符号对应double,可以用来对double类型输入,以下输出类似。
4输出:
printf("%lf",var_name);