Mateusz Buczkowski - 2008-03-11 14:20:52

No... jeśli ktoś ma sensownie zrobione przeładowanie operatora = to niech się podzieli, bo ja mam to zrobione dosyć brutalnie ;)

Kod:

//
//  Liczby zespolone
//

#include <iostream>
#include <conio.h>

using namespace std;

//                      Predefinicje

class cComplex;

double absq(cComplex &newC);
cComplex conj(cComplex &newC);

//                     Deklaracja klasy

class cComplex
{     
      private:
              double re, im;
      public:
             //                         Konstruktory
             
             cComplex() {};
             cComplex(double newRe, double newIm = 0.0):
                             re(newRe), im(newIm) {};
             
             
             //                          Przeładowanie operatorów
             
             cComplex operator+ (cComplex &newC)       // Dodawanie dwóch zespolonych
             {
                      return cComplex(re + newC.re, im + newC.im);
             }
             cComplex operator- (cComplex &newC)       // Odejmowanie dwóch zespolonych
             {
                      return cComplex(re - newC.re, im - newC.im);
             }
             cComplex operator* (cComplex &newC)       // Mnożenie dwóch zespolonych
             {
                      return cComplex(re * newC.re + im * newC.im, re * newC.im - im * newC.re);
             }
             cComplex operator* (double newD)        // Mnożenie zespolonej przez rzeczywistą
             {
                      return cComplex(re * newD, im * newD);
             }
             
             cComplex operator= (cComplex newC)     // Przypisanie
             {
                      (*this).re = newC.re;
                      (*this).im = newC.im;
                      return cComplex(re = newC.re, im = newC.im);
             }

             
             //                          Metody
             void vDisplay()
             {
                  cout << "re : " << re << ", im : " << im << endl;
             }
             
             //                         Funkcje zaprzyjaźnione
             
             friend cComplex operator/ (cComplex &newC1, cComplex &newC2);
             friend cComplex conj(cComplex &newC);
             friend double absq(cComplex &newC);
};

//                     Funkcje

double absq(cComplex &newC)
{
       return newC.re * newC.re + newC.im * newC.im;
}

cComplex conj(cComplex &newC)
{
         return cComplex(newC.re, -newC.im);
}

cComplex operator/ (cComplex &newC1, cComplex &newC2)
{
        return (newC1 * newC2) * (1 / absq(newC2));
}

//              Program główny

int main()
{
    
    cComplex JB(3,5);
    cComplex KG(4,2);
    cComplex result;
    
    result = JB + KG;
    result.vDisplay();
    
    result = JB - KG;
    result.vDisplay();
    
    result = JB * KG;
    result.vDisplay();    
    
    result = JB / KG;
    result.vDisplay();    
    
    getch();
    return 0;
}

Aaa... no i mogą być błędy w działaniach matematycznych, ale to nie jest akurat najważniejsze :P

GotLink.pl