Documente Academic
Documente Profesional
Documente Cultură
L01. Laborator 1 PDF
L01. Laborator 1 PDF
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 următoarele cerințe:
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-();
1
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"
2
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){
3
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 (coeficienţii) au valori fixate și sunt din clasa Complex, iar variabila x poate lua diferite
valori. Se vor respecta următoarele cerințe: