题目
定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除.
运算符重载作为Complex的类的成员函数,编程序,求两个复数的和,差,积,商.
程序:
#include
using namespace std;
class Complex //定义complex类
{public:
Complex(){real=0;imag=0;} //定义构造函数
Complex(double r,double i){real=r;imag=i;} //构造函数重载
Complex operator+(Complex &c2); //声明复数相加的函数
Complex operator-(Complex &c2); //声明复数相减的函数
Complex operator*(Complex &c2); //声明复数相乘的函数
Complex operator/(Complex &c2); //声明复数相除的函数
void display();
private:
double real; //实部
double imag; //虚部
};
Complex Complex::operator+(Complex &c2) //定义复数相加的函数
{Complex c;
c.real=real+c2.real; //实部相加
c.imag=imag+c2.imag; //虚部相加
return c;}
Complex Complex::operator-(Complex &c2) //定义复数相减的函数
{Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;}
Complex Complex::operator*(Complex &c2) //定义复数相乘的函数
{Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
运算符重载作为Complex的类的成员函数,编程序,求两个复数的和,差,积,商.
程序:
#include
using namespace std;
class Complex //定义complex类
{public:
Complex(){real=0;imag=0;} //定义构造函数
Complex(double r,double i){real=r;imag=i;} //构造函数重载
Complex operator+(Complex &c2); //声明复数相加的函数
Complex operator-(Complex &c2); //声明复数相减的函数
Complex operator*(Complex &c2); //声明复数相乘的函数
Complex operator/(Complex &c2); //声明复数相除的函数
void display();
private:
double real; //实部
double imag; //虚部
};
Complex Complex::operator+(Complex &c2) //定义复数相加的函数
{Complex c;
c.real=real+c2.real; //实部相加
c.imag=imag+c2.imag; //虚部相加
return c;}
Complex Complex::operator-(Complex &c2) //定义复数相减的函数
{Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;}
Complex Complex::operator*(Complex &c2) //定义复数相乘的函数
{Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
提问时间:2020-05-22
答案
#include
using namespace std;
class Complex //定义complex类
{public:
Complex(){real=0;imag=0;} //定义构造函数
Complex(double r,double i){real=r;imag=i;} //构造函数重载
friend Complex operator+(Complex &c1,Complex &c2); //声明复数相加的函数
friend Complex operator-(Complex &c1,Complex &c2); //声明复数相减的函数
friend Complex operator*(Complex &c1,Complex &c2); //声明复数相乘的函数
friend Complex operator/(Complex &c1,Complex &c2); //声明复数相除的函数
void display();
private:
double real; //实部
double imag; //虚部
};
Complex operator+(Complex &c1,Complex &c2) //定义复数相加的函数
{Complex c;
c.real=c1.real+c2.real; //实部相加
c.imag=c1.imag+c2.imag; //虚部相加
return c;}
Complex operator-(Complex &c1,Complex &c2) //定义复数相减的函数
{Complex c;
c.real=c1.real-c2.real;
c.imag=c2.imag-c2.imag;
return 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/(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;}
void Complex::display() //定义输出函数
{cout
using namespace std;
class Complex //定义complex类
{public:
Complex(){real=0;imag=0;} //定义构造函数
Complex(double r,double i){real=r;imag=i;} //构造函数重载
friend Complex operator+(Complex &c1,Complex &c2); //声明复数相加的函数
friend Complex operator-(Complex &c1,Complex &c2); //声明复数相减的函数
friend Complex operator*(Complex &c1,Complex &c2); //声明复数相乘的函数
friend Complex operator/(Complex &c1,Complex &c2); //声明复数相除的函数
void display();
private:
double real; //实部
double imag; //虚部
};
Complex operator+(Complex &c1,Complex &c2) //定义复数相加的函数
{Complex c;
c.real=c1.real+c2.real; //实部相加
c.imag=c1.imag+c2.imag; //虚部相加
return c;}
Complex operator-(Complex &c1,Complex &c2) //定义复数相减的函数
{Complex c;
c.real=c1.real-c2.real;
c.imag=c2.imag-c2.imag;
return 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/(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;}
void Complex::display() //定义输出函数
{cout
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
- 1课文中柳公权是个怎样的堂堂正正的君子
- 2一架飞机在两城之间飞行,风速为24千米/小时,顺风飞行需2小时50分,逆风飞行需要3小时. (1)求无风时飞机的飞行速度; (2)求两城之间的距离.
- 3Indeed,after his first greeting and a careless kiss,James took no notice of Maggie either,except
- 4画线提问,括号内为画线部分
- 5根据条件,先判断题中所给的是哪两种相关联的量,它们成什么比例,在写出等式.
- 6观在天下奇观,观潮,世界观中的意思.
- 7关于武汉长江大桥的诗句
- 8某段铁路所有车站共发行132种普通车票,那么这段铁路共有车站数是( ) A.8 B.12 C.16 D.24
- 9eighty fifty forty thirty rectangle square triangle 这些单词是剑桥英语jion in 几年级学的
- 10一处岩石包含砾石,沙子等物质,该岩石属于沉积岩吗
热门考点