#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double real = 0.0, double image = 0.0);
~Complex();
Complex(const Complex& c1);
Complex& operator=(const Complex& c2);//赋值运算
Complex operator+(const Complex& c2);//两复数相加
Complex operator-(const Complex& c2);//两复数相减
Complex operator*(const Complex& c2);//两复数相乘
Complex operator/(const Complex& c2);//两复数相除
Complex operator+=(const Complex& c2);//复数对象+=
Complex operator-=(const Complex& c2);//复数对象-=
Complex operator*=(const Complex& c2);//复数对象*=
Complex operator/=(const Complex& c2);//复数对象/=
Complex operator++();//复数对象的前置++
Complex operator++(int);//复数对象的后置++
Complex operator--();//复数对象的前置--
Complex operator--(int);//复数对象的后置--
void display(); //输出函数
private:
double _real; //实部
double _image; //虚部
};
//构造函数
Complex::Complex(double real, double image)
:_real(real),
_image(image)
{
}
// 拷贝构造函数
Complex::Complex(const Complex& c1)
:_real(c1._real),
_image(c1._image)
{
}
//析构函数
Complex::~Complex()
{
}
//赋值函数
Complex& Complex::operator=(const Complex& c2)
{
_real = c2._real;
_image = c2._image;
return *this;
}
//两复数相加函数
Complex Complex::operator+(const Complex& c2)
{
Complex c;
c._real = _real + c2._real;
c._image= _image + c2._image;
return c;
}
//两复数相减函数
Complex Complex::operator-(const Complex& c2)
{
Complex c;
c._real = _real - c2._real;
c._image = _image - c2._image;
return c;
}
//两复数相乘函数
Complex Complex::operator*(const Complex& c2)
{
Complex c;
c._real = (_real * c2._real) - (_image*c2._image);
c._image = (_image * c2._real) + (_real*c2._image);
return c;
}
//两复数相除函数
Complex Complex::operator/(const Complex& c2)
{
Complex c;
c._real = (_real*c2._real + _image*c2._image) / (c2._real*c2._real + c2._image*c2._image);
c._image = (_image*c2._real - _real*c2._image) / (c2._real*c2._real + c2._image*c2._image);
return c;
}
//复数对象+=函数
Complex Complex::operator+=(const Complex& c2)
{
/*_real = _real + c2._real;
_image =_image + c2._image;
return *this;*/
*this = *this + c2;
return *this;
}
//复数对象的-=函数
Complex Complex::operator-=(const Complex& c2)
{
/*_real -= c2._real;
_image -= c2._image;
return *this;*/
*this = *this - c2;
return *this;
}
//复数对象的*=函数
Complex Complex::operator*=(const Complex& c2)
{
/*double a = _real;
double b = _image;
_real = (a * c2._real) - (b*c2._image);
_image = (b * c2._real) + (a*c2._image);
return *this;*/
*this = *this*c2;
return *this;
}
//复数对象的/=函数
Complex Complex::operator/=(const Complex& c2)
{
/*double a = _real;
double b = _image;
_real = (a*c2._real + b*c2._image) / (c2._real*c2._real + c2._image*c2._image);
_image = (b*c2._real - a*c2._image) / (c2._real*c2._real + c2._image*c2._image);
return *this;*/
*this = *this / c2;
return c2;
}
//复数对象的前置++函数
Complex Complex::operator++()
{
_real++;
_image++;
return *this;
}
//复数对象的后置++函数
Complex Complex::operator++(int)
{
Complex c = *this;
_real++;
_image++;
return c;
}
//复数对象的前置--函数
Complex Complex::operator--()
{
_real--;
_image--;
return *this;
}
//复数对象的后置--函数
Complex Complex::operator--(int)
{
Complex c = *this;
_real--;
_image--;
return c;
}
//输出函数
void Complex::display()
{
cout <<"("<< _real <<","<< _image << "i)" << endl;
}
void test1()//赋值运算函数测试
{
Complex d1(3, 4), d2;
d2 = d1.operator=(d1);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
}
void test2()//两复数相加函数测试
{
Complex d1(3, 4), d2(5, -10), d3;
d3 = d1.operator+(d2);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
cout << "d1+d2="; d3.display();
}
void test3()//两复数相减函数测试
{
Complex d1(3, 4), d2(1, 10), d3;
d3 = d1.operator-(d2);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
cout << "d1-d2="; d3.display();
}
void test4()//两复数相乘函数测试
{
Complex d1(3, 4), d2(1, 2), d3;
d3 = d1.operator*(d2);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
cout << "d1*d2="; d3.display();
}
void test5()//两复数相除函数测试
{
Complex d1(2, 4), d2(1, 2), d3;
d3 = d1.operator/(d2);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
cout << "d1/d2="; d3.display();
}
void test6()//复数对象+=函数测试
{
Complex d1(1, 2),d2(3,4);
d1=d1.operator+=(d2);
cout << "d1="; d1.display();
}
void test7()//复数对象的-=函数测试
{
Complex d1(1, 2),d2(3,4);
d1 = d1.operator-=(d2);
cout << "d1="; d1.display();
}
void test8()//复数对象的*=函数测试
{
Complex d1(3, 4),d2(1,2);
d1 = d1.operator*=(d2);
cout << "d1="; d1.display();
}
void test9()//复数对象的/=函数测试
{
Complex d1(1, 2),d2(3,4);
d1 = d1.operator/=(d2);
cout << "d1="; d1.display();
}
void test10()//复数对象的前置++函数测试
{
Complex d1(1, 2);
d1 = d1.operator++();
cout << "d1="; d1.display();
}
void test11()//复数对象的后置++函数测试
{
Complex d1(1, 2),d2;
d2 = d1.operator++(1);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
}
void test12()//复数对象的前置--函数测试
{
Complex d1(3, 4);
d1 = d1.operator--();
cout << "d1="; d1.display();
}
void test13()//复数对象的后置--函数测试
{
Complex d1(3, 4), d2;
d2 = d1.operator--(1);
cout << "d1="; d1.display();
cout << "d2="; d2.display();
}
int main()
{
test6();
system("pause");
return 0;
}
复数类Complex
最新推荐文章于 2020-08-15 18:29:40 发布