#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double r,double i):real(r),imag(i){}
Complex operator +(Complex &s);
Complex operator -(Complex &s);
Complex operator *(Complex &s);
Complex operator /(Complex &s);
void display();
private:
double real;
double imag;
};
Complex Complex:: operator +(Complex &s)
{
return(Complex(real+s.real,imag+s.imag));
}
Complex Complex:: operator -(Complex &s)
{
return(Complex(real-s.real,imag-s.imag));
}
Complex Complex:: operator *(Complex &s)
{
return(Complex(real*s.real,imag*s.imag));
}
Complex Complex:: operator /(Complex &s)
{
return(Complex(real/s.real,imag/s.imag));
}
void Complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
int main()
{
Complex c1(4.0,3.0),c2(2.0,1.0);
Complex c3=c1+c2;
c3.display();
c3=c1-c2;
c3.display();
c3=c1*c2;
c3.display();
c3=c1/c2;
c3.display();
}
2
最新推荐文章于 2024-08-12 08:30:00 发布