Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Kis Alexandru Lab 9

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 12

/*1.

Pornind de la exemplul 4 indepliniti urmatoarele cerinte:


a. scrieti metode pentru citirea/scrierea unei matrici
b. testati toti operatorii suprancarcati*/

#include<iostream>
using namespace std;

const int Linii=2;


const int Coloane=3;
class Matrix{
private:
const short rows;
const short cols;
int *elems;
public:
Matrix(const short rows, const short cols);
Matrix(const Matrix&);
~Matrix(void){delete elems;}
int& operator() (const short row,const short col);
Matrix& operator=(const Matrix&);
friend Matrix operator+(Matrix&,Matrix&);
friend Matrix operator-(Matrix&,Matrix&);
friend Matrix operator*(Matrix&,Matrix&);
const short Rows(void){return rows;}
const short Cols(void){return cols;}
void citire();
void afisare();
};

Matrix::Matrix(const short r, const short c):rows(r),cols(c){


if(rows>0&&cols>0)
elems=new int[rows * cols];
}
Matrix::Matrix(const Matrix&m):rows(m.rows),cols(m.cols){
int n=rows*cols;
if(rows>0&&cols>0)
elems=new int[n];
for(int i=0;i<n;i++)
elems[i]=m.elems[i];
}
int& Matrix::operator ()(const short row,const short col){
if(row>=0&&row<rows)
if(col>=0&&col<cols)
return elems[row*cols+col];
}
Matrix& Matrix::operator=(const Matrix &m){
if(this!=&m){
if(rows==m.rows&&cols==m.cols){
int n=rows*cols;
for(int i=0;i<n;i++)
elems[i]=m.elems[i];
}
}
return *this;
}
Matrix operator + (Matrix &p, Matrix &q){
if (p.rows==q.rows&&p.cols==q.cols){
Matrix m(p.rows,p.cols);
for (int r=0;r<p.rows;++r)
for (int c=0;c<p.cols;++c)
m(r,c)=p(r,c)+q(r,c);
return m;
}
}
Matrix operator - (Matrix &p, Matrix &q){
if(p.rows==q.rows&&p.cols==q.cols){
Matrix m(p.rows,p.cols);
for (int r=0;r<p.rows;++r)
for (int c=0;c<p.cols;++c)
m(r,c)=p(r,c)-q(r,c);
return m;
}
}
Matrix operator *(Matrix &p,Matrix &q){
if (p.cols==q.rows){
Matrix m(p.rows,q.cols);
for (int r=0;r<p.rows;++r)
for (int c=0;c<q.cols;++c){
m(r,c)=0.0;
for (int i=0;i<p.cols;++i)
m(r,c)+=p(r,i)*q(i,c);
}
return m;
}
}
void Matrix::citire(){
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++){
cout<<endl<<"Dati elem de pe linia"<<i<<"si coloana"<<j<<" ";
cin>>elems[cols*i+j];
}
}
void Matrix::afisare(){
for(int i=0;i<rows;i++){
cout<<endl;
for(int j=0;j<cols;j++){
cout<<elems[cols*i+j]<<"\t";
}
}
}
void main(void){
int i,j;
cout<<endl<<"Supraincarcarea operatorului():atribuire unui element:\n";
Matrix m(Linii,Coloane);
m(0,0)=10;m(0,1)=20;m(0,2)=30;
m(1,0)=15;m(1,1)=25;m(1,2)=35;
for(i=0;i<Linii;i++)
{
cout<<endl;
for(j=0;j<Coloane;j++)
cout<<m(i,j)<<"\t";
}
cout<<endl<<"Dati matricea";
m.citire();
cout<<endl<<"Matricea este:"<<endl;
m.afisare();

cout<<endl<<"Supraincarcarea operatorului=";
Matrix n=m;
cout<<endl;
for(i=0;i<Linii;i++){
cout<<endl;
for(j=0;j<Coloane;j++)
cout<<n(i,j)<<"\t";
}
Matrix m1(Linii,Coloane);
cout<<endl<<"Dati matricea";
m1.citire();
cout<<endl<<"Matricea este:"<<endl;
m1.afisare();

Matrix m2(Linii,Coloane);
cout<<endl<<"Supraincarcarea operatorului+";
m2=m+m1;
cout<<endl<<"Matricea rezultata din suma celor 2 matrici este:";
m2.afisare();
cout<<endl<<"Supraincarcarea operatorului-";
m2=m-m1;
cout<<endl<<"Matricea rezultata din diferenta celor 2 matrci este:";
m2.afisare();
Matrix m3(3,2);//matricea m are 2 linii si 3 coloane deci pentru a fi posibil
produsul m3 trebuie sa aiba 3 linii si 2 coloane
cout<<endl<<"Dati matricea pentru produs";
m3.citire();
Matrix m4(2,2);//matricea rezultata dupa produs va avea 2 linii si 2 coloane
cout<<endl<<"Supraincarcarea operatorului*";
m4=m*m3;
cout<<endl<<"Matricea rezultata din suma produselor celor 2 matrici este:";
m4.afisare();
}

/*2. Sa se suprancarce operatorul [] astfel nct, folosit fiind asupra unor


obiecte din clasa Departament, ce contine
un tablou de obiecte de tip Angajat (clasa Angajat contine variabilele nume (sir de
caractere) si salariu (float)), sa
returneze toata informatia legata de angajatul al carui numar de ordine este trimis
ca parametru.*/

#include<iostream>
using namespace std;
#include<string.h>

class Departament;

class Angajat{
char nume[20];
float salariu;
public:
friend class Departament;
Angajat(){};
void afisare();
char* getter_n();
float getter_s();
};

class Departament{
int n;
Angajat *ang;
public:
Departament(int nr);
void citire();
Angajat operator[](int);
};

char * Angajat::getter_n(){
return nume;
}
float Angajat::getter_s(){
return salariu;
}
void Angajat::afisare(){
cout<<endl<<"Nume:"<<nume<<"Salariu:"<<salariu;
}
Departament::Departament(int nr)//consturctor
{
n=nr;
ang=new Angajat[n];
}

void Departament::citire()//initializam tabloul Angajat


{
for(int i=0;i<n;i++){
cout<<endl<<"Dati numele:";
cin>>ang[i].nume;
cout<<endl<<"Dati salariul:";
cin>>ang[i].salariu;
}
}
Angajat Departament::operator [](int index)//supraincarcarea operatorului[]
{
return ang[index-1];
}
void main(){
int nr,ind;
cout<<endl<<"Dati nr de angajati:";
cin>>nr;
Departament ob(nr);
ob.citire();
cout<<endl<<"Dati numarul de ordine al angajatului pentru care doriti
informatii:";
cin>>ind;
if((ind>=1)&&(ind<=nr)){
cout<<"Angajatul cu numarul de ordine"<<ind<<"este:\n";
ob[ind].afisare();
cout<<endl;
}
else
cout<<"Numarul de ordine este invalid!";
}
/*3. Sa se suprancarce operatorii new si delete ntr-una din clasele cu care s-a
lucrat anterior, n vederea alocarii si
eliberarii de memorie pentru un obiect din clasa respectiva.*/

#include<iostream>
using namespace std;
#include<string.h>

class Departament;

class Angajat{
char nume[20];
float salariu;
public:
friend class Departament;
Angajat(){}
Angajat(char *,float s);
void afisare();
char* getter_n();
float getter_s();

void *operator new(size_t marime);


void operator delete(void *a);
void *operator new[](size_t marime);
void operator delete[](void *a);
};

class Departament{
int n;
public:
Angajat *ang;
Departament(int nr);
Departament();
void citire();
Angajat operator[](int);

void *operator new[](size_t marime);


void operator delete[](void *a);
};
Angajat::Angajat(char *n,float s){
strcpy(nume,n);
salariu=s;
}

char * Angajat::getter_n(){
return nume;
}
float Angajat::getter_s(){
return salariu;
}
void Angajat::afisare(){
cout<<endl<<"Nume:"<<nume<<"Salariu:"<<salariu;
}
void *Angajat::operator new(size_t marime){
printf("\n Supraincarc local operatorul new pt clasa Angajat");
return malloc(marime);
}
void Angajat::operator delete(void *a){
printf("\nSupraincarc local operatorul delete pentru clasa Angajat.");
free(a);
}
void *Angajat::operator new[](size_t marime){
printf("\nSupraincarc local operatorul new[] pt clasa Angajat");
return malloc(marime);
}
void Angajat::operator delete[](void *a){
printf("\nSupraincarc local operatorul delete[] pt clasa Angajat.");
free(a);
}
Departament::Departament(int nr)//consturctor
{
n=nr;
for(int i=0;i<n;i++)
ang=new Angajat[n];
if(!ang){
printf("\Nu am putut aloca memorie pt. ang.");
exit(0);
}
}

void Departament::citire()//initializam tabloul Angajat


{
for(int i=0;i<n;i++){
cout<<endl<<"Dati numele:";
cin>>ang[i].nume;
cout<<endl<<"Dati salariul:";
cin>>ang[i].salariu;
}
}
Angajat Departament::operator [](int index)//supraincarcarea operatorului[]
{
return ang[index-1];
}
void *Departament::operator new[](size_t marime){
printf("\nSupraincarc local operatorul new[] pt clasa departament");
return malloc(marime);
}
void Departament::operator delete[](void *a){
printf("\nSupraincarc local operatorul delete[] pt clasa Departament.");
free(a);
}

void main(){
int nr,ind;
cout<<endl<<"Dati nr de angajati:";
cin>>nr;
Angajat *a=new Angajat("Popescu",200);
cout<<"\nInformatia despre angajat:";
a->afisare();

Departament ob(nr);
Departament *ob2=new Departament[2];
ob2[0]=Departament(1);
ob2[1]=Departament(2);

ob.citire();
cout<<endl<<"Dati angajatul cu numarul de ordine:";
cin>>ind;
if((ind>=1)&&(ind<=nr)){
cout<<"Angajatul cu numarul de ordine"<<ind<<"este:\n";
ob[ind].afisare();
}
else
cout<<"Numarul de ordine este invalid!";
delete a;
delete []ob2;
}

/* 4. Sa se scrie programul care implementeaza metoda int myFunction(), care n


functie de numarul de parametri primiti,
returneaza fie valoarea primita, fie produsul variabilelor de intrare.*/

#include<iostream>
using namespace std;

class Functii{
int a,b;
public:
Functii(){}
Functii(int x,int y){
a=x;
b=y;
}
int myFunction(int x){
return x;
}
int myFunction(int x,int y){
return x*y;
}
};
void main(){
int a,b;
cout<<"introduceti a:";
cin>>a;
cout<<"introduceti b:";
cin>>b;

Functii f(a,b);
printf("\nvaloarea primului parametru este :%d",f.myFunction(a));
printf("\nprodusul paramatrilor este:%d\n",f.myFunction(a,b));
}
/*5. Sa se scrie programul care utilizeaza o clasa numita Calculator si care are n
componenta sa (definite ca metode
publice) functiile:
- int calcul(int x) care returneaza patratul valorii primite;
- int calcul(int x, int y) care returneaza produsul celor doua valori primite;
- float calcul(int x, int y, int z) care returneaza rezultatul nlocuirii n
formula f(x,y,z) = (x-y)(x+z)/2 a valorilor
primite;
Programul primeste prin linia de comanda toti parametrii necesari.*/

#include<iostream>
using namespace std;
#include<stdlib.h>

class Calculator{
public:
int calcul(int x);
int calcul(int x, int y);
float calcul(int x, int y, int z);
};
int Calculator::calcul(int x){
return x*x;
}
int Calculator::calcul(int x, int y){
return x*y;
}
float Calculator::calcul(int x, int y, int z){
return ((x-y)*(x+z)/2.0);
}

void main(int argc,char* argv[]){


Calculator ob1;
if(argc<4){
cout<<"\nNu ati introdus datele!\nTrebuie sa introduceti 3 numere
intregi in linia de comanda!\n";
exit(0);
}
cout<<"\nPatratul primului parametru este:"<<ob1.calcul(atoi(argv[1]))<<endl;
cout<<"\nProdusul celor 2 parametrii esre:";
cout<<ob1.calcul(atoi(argv[1]),atoi(argv[2]))<<endl;
cout<<"\nREzultatul calculului cu formula f(x,y,z)=(x-y)(x+y)/2 este:";
cout<<ob1.calcul(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]))<<endl;
}

/*6. Sa se defineasca clasa Student avnd ca date membre private: numele (?ir de
caractere), note (pointer de tip ntreg) ?i
nr_note (int). Clasa mai con?ine un constructor cu parametri, un constructor de
copiere, o func?ie de suprancarcare a operatorului de atribuire, o
metoda de setare a notelor, o metoda de afi?are a atributelor ?i un destructor. Sa
se instan?ieze obiecte folosind constructorul cu parametri, un alt
obiect folosind constructorul de copiere, afi?nd de fiecare data atributele
obiectului creat. Realiza?i o opera?ie de copiere a unui obiect n alt obiect,
ambele fiind create in prealabil. Afi?ati rezultatul copierii.*/

#include<iostream>
using namespace std;
#include<string.h>

class Student{
char nume[20];
int *note;
int nr_note;
public:
Student(char n[],int *p,int nr);
Student(Student &ob1);
Student& operator=(Student &s);
void setter(char n[],int *p,int nr);
void afisare();
~Student(void);
};

Student::Student(char n[],int *p,int nr){


strcpy(nume,n);
nr_note=nr;
note=new int[nr_note];
for(int i=0;i<nr_note;i++)
*(note+i)=p[i];
}
Student::Student(Student &ob1){
cout<<"\nConstructor de copiere!\n";
strcpy(nume,ob1.nume);
nr_note=ob1.nr_note;
note=new int[nr_note];
for(int i=0;i<nr_note;i++)
*(note+i)=ob1.note[i];
}
Student& Student::operator=(Student &s){
cout<<"\nCopiere prin utilizarea supraincarcarii operatorului de
atribuire!\n";
if(this!=&s){
strcpy(nume,s.nume);
nr_note=s.nr_note;
note=new int[nr_note];
for(int i=0;i<nr_note;i++)
note[i]=s.note[i];
}
return *this;
}
void Student::setter(char n[],int *p,int nr){
strcpy(nume,n);
note=p;
nr_note=nr;
}
void Student::afisare(){
cout<<endl<<"Numele studentului este:"<<nume;
cout<<endl<<"Acesta a obtinut urmatoarele note:";
for(int i=0;i<nr_note;i++)
cout<<*(note+i)<<" ";
cout<<endl;
}
Student::~Student(void){
delete[]note;
}

void main(){
char num[20];
int not[20],*p=&not[0],nr;
cout<<endl<<"Dati numele:";
cin>>num;
cout<<endl<<"Dati numarul de note:";
cin>>nr;
cout<<endl<<"Dati notele:";
for(int i=0;i<nr;i++)
cin>>*(p+i);

Student ob1(num,p,nr);//instantiem un obiect cu constructorul cu parametrii


ob1.afisare();
Student ob2(ob1);//instantiem obiectul 2 cu constructorul de copiere
cout<"\nAm creat un alt obiect cu aceleasi caracteristici:\n";
ob2.afisare();
//creem un nou obiect pe care il initializam cu constructorul cu parametrii
cout<<endl<<"Dati numele studentului:";
cin>>num;
cout<<endl<<"Dati numarul de note ale studentului:";
cin>>nr;
cout<<endl<<"Dati notele studentului:";
for(int i=0;i<nr;i++)
cin>>*(p+i);

Student ob3(num,p,nr);
ob3.afisare();
cout<<"\nRealizam o operatie de asignare intre obiectele create:\n";
ob3=ob1;
ob3.afisare();
}

/*7. Sa se implementeze clasa Complex care suprancarca operatorii aritmetici cu


scopul de a efectua adunari, scaderi,
inmultiri si mpartiri de numere complexe.
Observatie: numerele complexe vor fi definite ca avnd o parte reala si una
imaginara, ambii coeficienti fiind reprezentati
prin numere n virgula mobila.*/

#include<iostream>
using namespace std;
#define Pi 3.14
#include<math.h>

class Complex{
double re;
double im;
public:
Complex(double x=0,double y=0);
double getter_re();
double getter_im();
void afisare();
double modul();
double argum();
Complex operator +(Complex &ob1);//calc suma a 2 nr complexe
Complex operator -(Complex &ob1);//calc diferenta a 2 nr complexe
Complex operator *(Complex &ob1);//calc produsul a 2 nr complexe
Complex operator /(Complex &ob1);//calc raportul a 2 nr complexe
Complex operator !();//calculeaza radacina patrata a ob curent,argum,oartea
reala si partea imaginara
};
Complex::Complex(double x,double y){
re=x;
im=y;
}
double Complex::getter_re(){
return re;
}
double Complex::getter_im(){
return im;
}
Complex Complex::operator +(Complex &ob1){
Complex obb;
obb.re=re+ob1.re;
obb.im=im+ob1.im;
return obb;
}
Complex Complex::operator -(Complex &ob1){
Complex obb;
obb.re=re-ob1.re;
obb.im=im-ob1.im;
return obb;
}
double Complex::modul()//calculeaza modulul unu numar complex
{
return sqrt(re*re+im*im);
}
double Complex::argum(){
double a=atan(im/re);
if(re<0)return Pi+a;
if(im>0)return(2*Pi+a);
}
Complex Complex::operator !()//calculeaza radacina patrata a ob curent ,argum,parte
reala si parte imaginara
{
Complex z;
double a=modul();
double alfa;
alfa=argum();
a=sqrt(a);
z.re=a*cos(alfa);//scriem nr complex sub forma trigonometrica
z.im=a*sin(alfa);
return z;
}
Complex Complex::operator *(Complex &ob1)//calculeaza produsul a 2 nr complexe
{
Complex obb;
obb.re=re*ob1.re-im*ob1.im;
obb.im=re*ob1.im+im*ob1.re;
return obb;
}
Complex Complex::operator /(Complex &ob1)//calculeaza raportul a 2 nr complexe
{
Complex z;
double a=ob1.re*ob1.re+ob1.im*ob1.im;
if(a!=0){
z.re=(re*ob1.re+im*ob1.im)/a;
z.im=(im*ob1.re-re*ob1.im)/a;
return z;
}
}
void Complex::afisare(){
if(im<0)
cout<<re<<im<<"j";
else
cout<<re<<"+"<<im<<"j";
}
void main(){
double x,y,x1,y1;
cout<<endl<<"Dati partea reala a primului numar complex:";
cin>>x;
cout<<endl<<"Dati aprtea imaginara pentru primul nr complex:";
cin>>y;
Complex z(x,y);
cout<<endl<<"Primul numar complex este:";
z.afisare();

cout<<endl<<"Dati partea reala pentru al doilea numar complex:";


cin>>x1;
cout<<endl<<"Dati aprtea imaginara pentru al doilea nr complex:";
cin>>y1;
Complex z1(x1,y1);
cout<<endl<<"Al doilea numar complex este:";
z1.afisare();

Complex ob;
ob=z+z1;
cout<<endl<<"Suma celor 2 numere complexe este:";
ob=z-z1;
cout<<endl<<"Diferenta celor 2 numere complexe este:";
ob=z*z1;
cout<<endl<<"Produsul celor 2 numere complexe este:";
ob=z/z1;
cout<<endl<<"Raportul celor 2 numere complexe este:";
ob.afisare();
cout<<endl;
}

You might also like