Advertisement
nex036ara

klasa_komplex

Nov 27th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. //komplex.hpp
  2.  
  3.  
  4.  
  5. #ifndef COMPLEX_DEF
  6. #define COMPLEX_DEF
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Complex{
  11.  
  12.     private:
  13.         double re;
  14.         double im;
  15.     public:
  16.         Complex();
  17.         Complex(double, double);
  18.         Complex(const Complex&);
  19.  
  20.         void setRe(double);
  21.         void setIm(double);
  22.  
  23.         double getRe()const;
  24.         double getIm()const;
  25.  
  26.         friend Complex operator+(const Complex&,const Complex&);
  27.         friend Complex operator-(const Complex&, const Complex&);
  28.         friend Complex operator*(const Complex&, const Complex&);
  29.         friend Complex operator/(const Complex&, const Complex&);
  30.         friend istream& operator>>(istream& ,Complex&);
  31.         friend ostream& operator<<(ostream&, Complex&);
  32.         friend bool operator==(const Complex&, const Complex&);
  33.         friend bool operator!=(const Complex&, const Complex&);
  34.  
  35.         Complex& operator=(const Complex&);
  36.         Complex& operator+=(const Complex&);
  37.         Complex& operator*=(const Complex&);
  38.         Complex& operator/=(const Complex&);
  39.         Complex& operator-=(const Complex&);
  40.  
  41.         const Complex& operator++();
  42.         const Complex operator++(int);
  43.  
  44. };
  45. #endif
  46.  
  47.  
  48. //komplex.cpp
  49.  
  50.  
  51. #include "koplex.hpp"
  52.  
  53. Complex:: Complex() {re = 0; im = 0;}
  54.    Complex::   Complex(double rr, double ii){re = rr; im = ii;}
  55.        Complex:: Complex(const Complex &z) { re =z.re; im = z.im; }
  56.  
  57.  
  58.  
  59.         double Complex:: getRe()const{return re;}
  60.         double Complex:: getIm()const{return im;}
  61.  
  62.         Complex operator+(const Complex &x,const Complex &y) {
  63.             Complex w(x.re+y.re, x.im+y.im);
  64.             return w;
  65.             }
  66.  
  67.  
  68.  
  69.         Complex operator-(const Complex &x, const Complex &y){
  70.         Complex w(x.re-y.re, x.im-y.im);
  71.             return w;
  72.  
  73.         }
  74.         Complex operator*(const Complex &x, const Complex &y){
  75.  
  76.         Complex w(x.re*y.re-x.im*y.im, x.im*y.re+x.re*y.im);
  77.         return w;
  78.  
  79.         }
  80.         Complex operator/(const Complex &x, const Complex &y){
  81.         double d = y.re*y.re + y.im*y.im;
  82.         Complex w((x.re*y.re+x.im*y.im)/d, (x.im*y.re-x.re*y.im)/d);
  83.         return w;
  84.  
  85.         }
  86.         istream &operator>>(istream &in ,Complex &z){
  87.         in>>z.re>>z.im;
  88.         return in;
  89.         }
  90.         ostream &operator<<(ostream &out, Complex &z){
  91.         if(z.im==0) out<<z.re;
  92.         if(z.re!=0 && z.im>0) out<<z.re<<"+"<<z.im<<"i";
  93.         if(z.re!=0 && z.im<0) out<<z.re<<z.im<<"i";
  94.         if(z.re==0 && z.im!=0) out<<z.im<<"i";
  95.  
  96.  
  97.         return out;
  98.         }
  99.  
  100.         bool operator==(const Complex &x, const Complex &y){
  101.         return (x.re==y.re) && (x.im==y.im);
  102.  
  103.         }
  104.         bool operator!=(const Complex &x, const Complex &y){
  105.         return (x.re!=x.im) || (x.im!=y.im);
  106.  
  107.         }
  108.  
  109.         Complex& Complex:: operator=(const Complex &z){
  110.         re = z.re;
  111.         im = z.im;
  112.         return *this;
  113.         }
  114.         Complex& Complex:: operator+=(const Complex &z){
  115.         re+=z.re;
  116.         im+=z.im;
  117.         return *this;
  118.         }
  119.  
  120.         Complex& Complex:: operator*=(const Complex &z){
  121.         re = re*z.re - im*z.im;
  122.         im = im*z.re + z.im*re;
  123.         return *this;
  124.         }
  125.         Complex& Complex:: operator/=(const Complex &z){
  126.         double d = z.re*z.re + z.im*z.im;
  127.         re = (re*z.re + im*z.im)/d;
  128.         im = (im*z.re-re*z.im)/d;
  129.         return *this;
  130.         }
  131.  
  132.         Complex& Complex:: operator-=(const Complex &z){
  133.         re-=z.re;
  134.         im-=z.im;
  135.         return *this;
  136.         }
  137.  
  138.         const Complex& Complex:: operator++(){
  139.         re++;
  140.         im++;
  141.         return *this;
  142.         }
  143.         const Complex Complex:: operator++(int){
  144.         Complex w(re,im);
  145.         re++; im++;
  146.         return w;
  147.         }
  148. //main.cpp
  149.  
  150.  
  151. #include "koplex.hpp"
  152.  
  153.  
  154. int main(){
  155. Complex z1, z2(1,3), z3(4,5);
  156.  
  157. cout<<"z1:"<<z1<<endl;
  158. cout<<"z2:"<<z2<<endl;
  159. cout<<"z3:"<<z3<<endl;
  160. cout<<"z1++\t++z2\tz3++"<<endl;
  161. cout<<"\n"<<endl;
  162. z1++;
  163. ++z2;
  164. z3++;
  165.  
  166. cout<<"z1:"<<z1<<endl;
  167. cout<<"z2:"<<z2<<endl;
  168. cout<<"z3:"<<z3<<endl;
  169. cout<<"z1=z2=z3"<<endl;
  170. cout<<"\n"<<endl;
  171. z1=z2=z3;
  172. cout<<"z1:"<<z1<<endl;
  173. cout<<"z2:"<<z2<<endl;
  174. cout<<"z3:"<<z3<<endl;
  175.  
  176.  
  177.  
  178. return 0;
  179. }
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement