# 【题解】【运算符重载】Complex

## 题目描述

1.定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算。将运算符函数重载为非成员，非友元的普通函数。

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class Complex{
private:
double real,image;
public:
Complex(double real=0,double image=0);
double get_real();
double get_image();
void display();
};

int main(){
Complex operator+(Complex &c1,Complex &c2);
Complex c1(3,4),c2(5,-10),c3;
c3 = c1 + c2;
c3.display();

return 0;
}
Complex::Complex(double real,double image){
this->real = real;
this->image = image;
}
double Complex::get_real(){
return real;
}
double Complex::get_image(){
return image;
}
void Complex::display(){
if(image > 0)
cout<<real<<"+"<<image<<"i"<<"\n";
else if(image < 0)
cout<<real<<image<<"i"<<"\n";
else
cout<<real<<"\n";
}
Complex operator+(Complex &c1,Complex &c2){
return Complex(c1.get_real()+c2.get_real(),c1.get_image()+c2.get_image());
}


2.定义一个复数类Complex，重载运算符+、-、*、/

#include<iostream>
using namespace std;
class Complex{
public:
Complex(){real = 0;image = 0;}
Complex(double r,double i){real = r;image = i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double image;
};
int main(){
Complex c1(3,4),c2(5,-10),c3;
c3 = c1 + c2;
c3.display();
c3 = c1 - c2;
c3.display();
c3 = c1 * c2;
c3.display();
c3 = c1 / c2;
c3.display();
return 0;
}
void Complex::display(){
if(image > 0)
cout<<real <<"+"<<image<<"i"<<"\n";
else if(image < 0)
cout<<real<<image<<"i"<<"\n";
else
cout<<real;
}
Complex Complex::operator+(Complex &c2){
Complex temp;
temp.real = real + c2.real;
temp.image = image + c2.image;
return temp;
}
Complex Complex::operator-(Complex &c2){
Complex temp;
temp.real = real - c2.real;
temp.image = image - c2.image;
return temp;
}
Complex Complex::operator*(Complex &c2){
Complex temp;
temp.real = real * c2.real - image*c2.image;
temp.image = image*c2.real + real*c2.image;
return temp;
}
Complex Complex::operator/(Complex &c2){
Complex temp;
temp.real = (real * c2.real + image*c2.image) / (c2.real*c2.real + c2.image*c2.image);
temp.image = (image*c2.real - real*c2.image) /	(c2.real*c2.real + c2.image*c2.image);
return temp;
}


3.定义一个复数类Complex,重载运算符 “+” ，使之能用于复数的加法运算。参加运算的两个运算量都可以是类对象，也可以其中一个是整数，顺序任意。例如c1 + c2 , 3 + c2 , c2 + 3。编写以上程序。

#include<iostream>
using namespace std;
class Complex{
public:
Complex(){real = 0;image = 0;}
Complex(double r,double i){real = r;image = i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
Complex operator+(int &i);
friend Complex operator+(int &,Complex &);
void display();
private:
double real;
double image;
};
int main(){
Complex c1(3,4),c2(5,-10),c3;
c3 = c1 + c2;
c3.display();
int i=5;
c3 = i + c1;
c3.display();
c3 = c1 + i;
c3.display();
return 0;
}
void Complex::display(){
if(image > 0)
cout<<real <<"+"<<image<<"i"<<"\n";
else if(image < 0)
cout<<real<<image<<"i"<<"\n";
else
cout<<real;
}
Complex Complex::operator+(Complex &c2){
Complex temp;
temp.real = real + c2.real;
temp.image = image + c2.image;
return temp;
}
Complex Complex::operator-(Complex &c2){
Complex temp;
temp.real = real - c2.real;
temp.image = image - c2.image;
return temp;
}
Complex Complex::operator*(Complex &c2){
Complex temp;
temp.real = real * c2.real - image*c2.image;
temp.image = image*c2.real + real*c2.image;
return temp;
}
Complex Complex::operator/(Complex &c2){
Complex temp;
temp.real = (real * c2.real + image*c2.image) / (c2.real*c2.real + c2.image*c2.image);
temp.image = (image*c2.real - real*c2.image) /	(c2.real*c2.real + c2.image*c2.image);
return temp;
}
Complex Complex::operator+(int &i){
Complex temp;
temp.real = real + i;
temp.image = image;
return temp;
}
Complex operator+(int &i,Complex &c2){
Complex temp;
temp.real = i + c2.real;
temp.image = c2.image;
return temp;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

夜幕下，浅浅的笑

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

07-09 2591

04-16 1808
05-30 984
10-19 370
01-20 1万+
12-14 404
01-21 207
02-16 367
09-24 149
06-03 1746
05-20
09-18