Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //komplex.hpp
- #ifndef COMPLEX_DEF
- #define COMPLEX_DEF
- #include <iostream>
- using namespace std;
- class Complex{
- private:
- double re;
- double im;
- public:
- Complex();
- Complex(double, double);
- Complex(const Complex&);
- void setRe(double);
- void setIm(double);
- double getRe()const;
- double getIm()const;
- friend Complex operator+(const Complex&,const Complex&);
- friend Complex operator-(const Complex&, const Complex&);
- friend Complex operator*(const Complex&, const Complex&);
- friend Complex operator/(const Complex&, const Complex&);
- friend istream& operator>>(istream& ,Complex&);
- friend ostream& operator<<(ostream&, Complex&);
- friend bool operator==(const Complex&, const Complex&);
- friend bool operator!=(const Complex&, const Complex&);
- Complex& operator=(const Complex&);
- Complex& operator+=(const Complex&);
- Complex& operator*=(const Complex&);
- Complex& operator/=(const Complex&);
- Complex& operator-=(const Complex&);
- const Complex& operator++();
- const Complex operator++(int);
- };
- #endif
- //komplex.cpp
- #include "koplex.hpp"
- Complex:: Complex() {re = 0; im = 0;}
- Complex:: Complex(double rr, double ii){re = rr; im = ii;}
- Complex:: Complex(const Complex &z) { re =z.re; im = z.im; }
- double Complex:: getRe()const{return re;}
- double Complex:: getIm()const{return im;}
- Complex operator+(const Complex &x,const Complex &y) {
- Complex w(x.re+y.re, x.im+y.im);
- return w;
- }
- Complex operator-(const Complex &x, const Complex &y){
- Complex w(x.re-y.re, x.im-y.im);
- return w;
- }
- Complex operator*(const Complex &x, const Complex &y){
- Complex w(x.re*y.re-x.im*y.im, x.im*y.re+x.re*y.im);
- return w;
- }
- Complex operator/(const Complex &x, const Complex &y){
- double d = y.re*y.re + y.im*y.im;
- Complex w((x.re*y.re+x.im*y.im)/d, (x.im*y.re-x.re*y.im)/d);
- return w;
- }
- istream &operator>>(istream &in ,Complex &z){
- in>>z.re>>z.im;
- return in;
- }
- ostream &operator<<(ostream &out, Complex &z){
- if(z.im==0) out<<z.re;
- if(z.re!=0 && z.im>0) out<<z.re<<"+"<<z.im<<"i";
- if(z.re!=0 && z.im<0) out<<z.re<<z.im<<"i";
- if(z.re==0 && z.im!=0) out<<z.im<<"i";
- return out;
- }
- bool operator==(const Complex &x, const Complex &y){
- return (x.re==y.re) && (x.im==y.im);
- }
- bool operator!=(const Complex &x, const Complex &y){
- return (x.re!=x.im) || (x.im!=y.im);
- }
- Complex& Complex:: operator=(const Complex &z){
- re = z.re;
- im = z.im;
- return *this;
- }
- Complex& Complex:: operator+=(const Complex &z){
- re+=z.re;
- im+=z.im;
- return *this;
- }
- Complex& Complex:: operator*=(const Complex &z){
- re = re*z.re - im*z.im;
- im = im*z.re + z.im*re;
- return *this;
- }
- Complex& Complex:: operator/=(const Complex &z){
- double d = z.re*z.re + z.im*z.im;
- re = (re*z.re + im*z.im)/d;
- im = (im*z.re-re*z.im)/d;
- return *this;
- }
- Complex& Complex:: operator-=(const Complex &z){
- re-=z.re;
- im-=z.im;
- return *this;
- }
- const Complex& Complex:: operator++(){
- re++;
- im++;
- return *this;
- }
- const Complex Complex:: operator++(int){
- Complex w(re,im);
- re++; im++;
- return w;
- }
- //main.cpp
- #include "koplex.hpp"
- int main(){
- Complex z1, z2(1,3), z3(4,5);
- cout<<"z1:"<<z1<<endl;
- cout<<"z2:"<<z2<<endl;
- cout<<"z3:"<<z3<<endl;
- cout<<"z1++\t++z2\tz3++"<<endl;
- cout<<"\n"<<endl;
- z1++;
- ++z2;
- z3++;
- cout<<"z1:"<<z1<<endl;
- cout<<"z2:"<<z2<<endl;
- cout<<"z3:"<<z3<<endl;
- cout<<"z1=z2=z3"<<endl;
- cout<<"\n"<<endl;
- z1=z2=z3;
- cout<<"z1:"<<z1<<endl;
- cout<<"z2:"<<z2<<endl;
- cout<<"z3:"<<z3<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement