Sunteți pe pagina 1din 4

Programare orientat pe obiecte

Laborator 1

Laborator 1
Tem de laborator
1. S se proiecteze i s se implementeze clasa Complex care s permit lucrul cu numere complexe. Se vor
respecta urmtoarele cerine:

Clasa va avea un constructor implicit, un constructor de copiere i un constructor folosit la conversii


din expresii de tipul "a+bi"
Se vor declara dou metode folosite pentru calculul modulului i argumentului unui numr complex
Se va declara o funcie prieten care s permit calculul distanei dintre dou numere complexe
Se vor defini metode pentru citirea/scrierea prii reale/imaginare
Se vor suprancrca operatorii: +,-,*,/,==,!=, -,~,++(prefix),++(postfix),+=,-=,*=,/=
Se va implementa o funcie de conversie a unui obiect din clasa complex la tipul double
Se vor suprancrca operatorii >> i <<

Complex.h
#include <iostream>
using namespace std;
class Complex{
double re, im;
public:
//constructori
Complex(double x=0, double y=0);
Complex(const Complex& );
explicit Complex(const char*);//constructor folosit la conversii din expresii de tipul "a+bi"
//metode
double modul();
double argument();
friend double distanta(const Complex&,const Complex&);
double getRe();
void setRe(double);
double getIm();
void setIm(double);
//supraincarcare operatori
Complex operator+(const Complex&);
Complex operator-(const Complex&);
Complex operator*(const Complex&);
Complex operator/(const Complex&);
int operator==(const Complex&);
int operator!=(const Complex&);
Complex operator-();

Programare orientat pe obiecte

Laborator 1

Complex operator~();//conjugat
Complex& operator++();//prefix
Complex operator++(int);//postfix
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);
operator double() const;//functie de conversie
friend ostream& operator<<(ostream& os, const Complex&);
friend istream& operator>>(istream& is, Complex&);
};

Source.cpp
#include "Complex.h"
Complex::Complex(double re,double im){
this->re=re;
this->im=im;
}
Complex::Complex(const Complex& z){
re=z.re;
im=z.im;
}
Complex::Complex(const char* str){
char* pos_plus = (char*)strrchr(str,'+');
char* pos_minus = (char*)strrchr(str,'-');
if(pos_plus==NULL){
re = strtod (str,&pos_minus);
im=strtod (pos_minus, NULL);
}
else if(pos_minus==NULL){
re = strtod (str,&pos_plus);
im=strtod (pos_plus, NULL);
}
else{
re = strtod (str, (pos_plus>pos_minus?&pos_plus:&pos_minus));
im=strtod (pos_plus>pos_minus?pos_plus:pos_minus, NULL);
}
}
double Complex::modul(){
return sqrt(re*re+im*im);
}
double Complex::argument(){
return atan2(im,re);
}
double distanta(const Complex& z1,const Complex& z2){
return sqrt((z1.re-z2.re)*(z1.re-z2.re)+(z1.im-z2.im)*(z1.im-z2.im));
}
double Complex::getRe(){
return re;
}
void Complex::setRe(double re){

Programare orientat pe obiecte

Laborator 1

this->re=re;
}
double Complex::getIm(){
return im;
}
void Complex::setIm(double im){
this->im=im;
}
Complex Complex::operator+(const Complex& c){
return Complex(re+c.re,im+c.im);
};
Complex Complex::operator-(const Complex& c){
return Complex(re-c.re,im-c.im);
};
Complex Complex::operator*(const Complex& z){
return Complex(re*z.re-im*z.im,re*z.im+im*z.re);
};
Complex Complex::operator/(const Complex& z){
double t=z.re*z.re+z.im*z.im;
return Complex((re*z.re+im*z.im)/t ,
(im*z.re-re*z.im)/t);
};
int Complex::operator==(const Complex& z){
return re==z.re && im==z.im;
};
int Complex::operator!=(const Complex& z){
return re!=z.re || im!=z.im;
};
Complex Complex::operator-(){
return Complex(-re, -im);
};
Complex Complex::operator~(){
return Complex(re, -im);
};
Complex& Complex::operator++(){
re++;
return *this;
};
Complex Complex::operator++(int){
Complex t(*this);
re++;
return t;
};
Complex& Complex::operator+=(const Complex& z){
if(this!=&z){
re+=z.re;
im+=z.im;
};
return *this;
};
Complex& Complex::operator-=(const Complex& z){
if(this!=&z){
re-=z.re;
im-=z.im;
};
return *this;
};
Complex& Complex::operator*=(const Complex& z){

Programare orientat pe obiecte

Laborator 1

if(this!=&z){
double t=re;
re=re*z.re-im*z.im;
im=t*z.im+im*z.re;
};
return *this;
};
Complex& Complex::operator/=(const Complex& z){
if(this!=&z){
double t1=re, t2=z.re*z.re+z.im*z.im;
re=(re*z.re+im*z.im)/t2;
im=(im*z.re-t1*z.im)/t2;
};
return *this;
};
Complex::operator double() const {
return re;
};
ostream& operator<<(ostream& out, const Complex& z){
if(z.re==0)out<<z.im<<(z.im?"i":"");
else if(z.im==0)out<<z.re;
else out << z.re<<(z.im>0?"+":"")<<z.im<<"i";
return out;
};
istream& operator>>(istream& in, Complex& z){
in >> z.re >> z.im;
return in;
};
void main(){
double r=5;
Complex c(r,9);//constructor
Complex z(c);//constructor copiere
c=5;//implicit
z=c+(Complex)5/z++;
c=(Complex)"7+5.2i";
r=(double)c;//operator conversie
cin>>c;
cout<<c<<endl;
}

2. . S se proiecteze i s se implementeze clasa EcGr2 care va avea elemente de forma ax2+bx+c n care
numerele a,b,c (coeficienii) au valori fixate i sunt din clasa Complex, iar variabila x poate lua diferite
valori. Se vor respecta urmtoarele cerine:

Clasa va avea un constructor implicit i un constructor de copiere


Se vor declara funcii membru pentru citirea/scrierea coeficienilor
Se va declara o funcie membru pentru evaluarea expresiei pentru un x dat
Se va declara o funcie pentru determinarea rdcinilor
Se vor suprancrca operatorii + i * ca funcii nemembre pentru adunarea a dou expresii respective
pentru nmulirea cu scalari

S-ar putea să vă placă și