Complex复数类

来源:互联网 发布:数据库设计包括 编辑:程序博客网 时间:2024/05/22 00:43
#include<iostream>#include<stdlib.h>using namespace std;class Complex{public:Complex(double real,double imag):_real(real),_imag(imag){}Complex(const Complex& s):_real(s._real),_imag(s._imag){} Complex& operator=(Complex& s){if(this!=&s){_real=s._real;_imag=s._imag;}return *this; } ~Complex() { }public:Complex operator+(const Complex& s){_real=_real+s._real;_imag=_imag+s._imag;return *this;}Complex operator-(const Complex& s){_real=_real-s._real;_imag=_imag-s._imag;return *this;}Complex &operator+=(const Complex& s){_real=_real+s._real;_imag=_imag+s._imag;return *this;}Complex &operator-=(const Complex& s){_real=_real-s._real;_imag=_imag-s._imag;return *this;}Complex& operator++(int){_real++;_imag++;return *this;}Complex& operator++(){Complex tmp=*this;_real++;_imag++;return tmp;}bool operator==(const Complex& s){if((_real==s._real)&&(_imag==s._imag))return true;elsereturn false;}bool operator!=(const Complex& s){if((_real!=s._real)||(_imag!=s._imag))return true;elsereturn false;}bool operator>(const Complex& s){if((_real>s._real)&&(_imag>s._imag))return true;elsereturn false;}bool operator>=(const Complex& s){if((_real>=s._real)&&(_imag>=s._imag))return true;elsereturn false;}bool operator<(const Complex& s){if((_real<s._real)&&(_imag<s._imag))return true;elsereturn false;}bool operator<=(const Complex& s){if((_real<=s._real)&&(_imag)<=(s._imag))return true;elsereturn false;}void Display(){cout<<_real<<" "<<_imag<<endl;}private:double _real;double _imag;};void main(){Complex c(2.0,3.0);Complex c1(c);Complex c3(4.0,3.0);Complex c4(5.0,3.0);c3=c4=c;Complex c5=c3-=c;c.Display();c1.Display();c3.Display();c4.Display();c5.Display();system("pause");}