定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是复数类对象,也可以其中一个是整数。例如:c1+c2,c1+i,i+c1均合法(其中i为整数,c1、c2为复数)。在main函数中调用重载运算符,分别求两个复数之和、复数和整数之和
#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);
本回答被网友采纳(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
}Complex operator +(Complex &c,double i){ return Complex(i+c.real,c.imag);}Complex operator +(double i,Complex &c)
C++ 一个复数类,运算符重载 + ,实现复数和复数的相加。
include <iostream>using namespace std;class Complex{public: Complex(){} Complex(float i,float j) { this->a = i; this->b = j; } Complex& operator+(Complex &x); float getA(){return a;} float getB(){return b;}private: float a;\/\/实数 ...
用C++语言设计一个虚数类,要求虚数类中重载运算符加减,主函数定义类...
代码如下:\/*1.复数(实部运算 +虚部运算) 1+2i 1-3i 加:2-i 减 0-5i1 重载实现复数一个输入和输出 普通写法实现输入 调用函数的形式实现输出2. 类重载实现复数的加法和减法 加法:类重载 减法:友元重载*\/#include<iostream>using namespace std;class A{private:int...
定义一个复数类complex。
Complex operator +(Complex &); \/\/重载运算符+ Complex operator -(Complex &); \/\/重载运算符- \/\/实现 Complex Complex::operator +( Complex &c){ Complex t;t.Real = this->Real + c.Real;t.Image = this->Image + c.Image;return t;} Complex Complex::operator -( Complex...
...设计一个完整的复数complex类,要求利用运算符重载实现复数的加法(+...
class Complex{\/\/定义复数类 double real;\/\/实部 double image;\/\/虚部 public:Complex( double r=0.0,double i=0.0 )\/\/构造 :real(r),image(i)\/\/初始化列表 {} Complex operator+( const Complex& cc )const\/\/两个复数相加 { return Complex(real+cc.real,image+cc.image);} Complex ...
c++编写一个复数类Complex
complex(double x,double y){ real=x;imagi=y;countP++;} complex(complex &C){ real=C.real;imagi=C.imagi;countP++;} ~complex(){countP--;} void SetReal(double x){real=x;} void SetImagi(double y){imagi=y;} double GetReal(){return real;} double GetImagi(){return imagi...
C++编写复数计算类
Complex operator+(Complex & com1,Complex & com2)\/\/重载- { Complex temp;temp.real = com1.real +com2.real;temp.imag = com1.imag + com2.imag;return temp;} void Complex::DisplayData()\/\/输出Data { cout<< "the Data is:"<<endl;cout<<"("<<real<<","<<imag<<")"...
C++如何定义一个复数类,并支持四则运算
cout<<"两个复数值为:";cout<<"A=";n1.display();cout<<" B=";n2.display();cout<<endl;cout<<endl<<endl<<"两个复数的加法:"<<endl;complex result1;result1=n1+n2;n1.display();cout<<"+";n2.display();cout<<"=";result1.display();cout<<endl<<endl<<"两个复数的减法...
急!C++定义一个复数类Complex,使下面的代码能够工作?
{ private:double re;double im;public:Complex(double re,double im=0){ this->re = re;this->im = im;} void add(const Complex& c){ this->re += c.re;this->im += c.im;} void sub(const Complex& c){ this->re -= c.re;this->im -= c.im;} void show(){ if(...
C++编程题 定义并实现一个复数类Complex,使得下面的代码能够工作_百度...
\/\/Complex(Complex &Complex); \/\/这个拷贝构造函数你下面没有定义 对象名不能与类名相同,改成小写 Complex(Complex &complex);void put_real1(float); \/\/获得复数的实部 void put_imaginary1(float); \/\/获得复数的虚部 put应该是用于输出函数名的,严格来说应用Set_,你自己改吧 voi...