题目描述
1.定义一个复数类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;
}
2.定义一个复数类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;
}