Skip to main content
edited tags
Link
agneau
  • 341
  • 3
  • 11
Source Link
agneau
  • 341
  • 3
  • 11

operations with complex numbers in C++

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

class Complex {
private:
    double Rez, Imz, ro, fi;

public:
    Complex() {
        Rez = 0.;
        Imz = 0.;
        ro = 0.;
    fi = 0.;
}

Complex(double x, double y) {
    Rez = x;
    Imz = y;
    ro = sqrt(pow(x,2)+pow(y,2));
    fi = atan(y/x);
 }

~Complex() {}

double get_Rez() {return Rez;} 
double get_Imz() {return Imz;} 
double get_ro() {return ro;} 
double get_fi() {return fi;}

void set_Rez(double a) {Rez = a; ro = sqrt(pow(Rez,2)+pow(Imz,2)); fi = acos(Rez/ro);}
void set_Imz(double a) {Imz = a; ro = sqrt(pow(Rez,2)+pow(Imz,2)); fi = acos(Rez/ro);}
void set_ro(double a) {ro = a; Rez = ro*cos(fi); Imz = ro*sin(fi);}
void set_fi(double a) {fi = a; Rez = ro*cos(fi); Imz = ro*sin(fi);}

Complex & inverso() {       
    this->set_Rez(this->get_Rez()/pow(this->get_ro(),2));
    this->set_Imz(-this->get_Imz()/pow(this->get_ro(),2));
    return *this;
}

};

These two are the functions to work with my complex numbers. Is it possible to change them in order to make them accept also doubles as input? In that case, would it be possible to make them give a double as an output? Is it correct to say that my functions haven't got side effects? Would it be better to put the functions in the class as data members?

Complex somma(Complex a, Complex b) {
    Complex c;
    c.set_Rez(a.get_Rez()+b.get_Rez());
    c.set_Imz(a.get_Imz()+b.get_Imz());
    return c;
}

Complex prodotto(Complex a, Complex b) {
    Complex c;
    c.set_ro(a.get_ro()*b.get_ro());
    c.set_fi(a.get_fi()+b.get_fi());
    return c;
}