复数类Complex

来源:互联网 发布:网站数据分析工具 编辑:程序博客网 时间:2024/06/06 15:43
#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;}

原创粉丝点击