Vous êtes sur la page 1sur 11

POP STANCA FLORINA, GR 2011, LAB9

------------------------------------------------------------------------

/* Stanca Pop, 2011, lab09, pb01 */

/* Starting with the 4-th example, resolve the following tasks:


a. write methods for reading/writing a matrix;
b. test all the overloaded operators;
c. display the elements located on both diagonals; */

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <conio.h>

const int Linii = 2;


const int Coloane = 2;

class Matrix
{
private:
const short rows;
const short cols;
int *elems;
int a[Linii][Coloane];

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();
void afisdp();
void afisds();
};

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];

else
printf("\nAuto-atribuire!\n");

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::afisare()
{
for (int i = 0; i<rows; i++)
{

for (int j = 0; j<cols; j++)


cout << elems[i*cols + j] << "\t";

cout << endl;

void Matrix::citire()

for (int i = 0; i<rows; i++)


for (int j = 0; j<cols; j++)
{
cout<<"\nElementul[" << i + 1 << "," << j + 1
<< "]=";
cin>>elems[i*cols + j];
}
}

void Matrix::afisdp()
{

cout<<"\nElementele de pe diagonala principala sunt: ";


for (int i = 0; i < rows; i++)
{
cout << a[i][i] << " ";
}
}

void Matrix::afisds()
{
cout << "\n Elementele de pe ds : ";
for (int i = 0; i<rows; i++)
cout << a[i][rows - 1 - i] << " ";
}

void main(void)
{
int i, j;
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;
m.afisare();

cout << "\n\nDati elementele matricii:\n";


Matrix m1(Linii, Coloane), n(Linii, Coloane);
m1.citire();
cout << "\nAti introdus matricea:\n";
m1.afisare();
n = m + m1;
cout << "\nSuma celor doua matrici este:\n";
n.afisare();
n = m - m1;
cout << "\nDiferenta celor doua matrici este:\n";
n.afisare();

Matrix m2(2, 2), n1(2, 2);


cout << "\n\nDati elementele unei noi matrici:\n";
m2.citire();
cout << "\nAti introdus matricea:\n";
m2.afisare();
cout << "\nProdusul celor doua matrici este:\n";
n1 = m*m2;
n1.afisare();
m2.afisdp();
m2.afisds();
_getch();
}
----------------------------------------------------------------------------------

/* Stanca Pop, 2011, lab09, pb02 */

/* Overload the [] operator for the Department class that contains an


array of Employee objects
(that has as variables the name (character array) and the salary
(float)). When the operator is
applied to a Department object, it returns (or displays) all the data
related to the Employee
object with that index.*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

class Angajat
{
char nume[256];
float salariu;
public:
Angajat() {}
Angajat(char *n,float s)
{
strcpy(nume,n);
salariu=s;
}

Angajat& operator=(const Angajat&);

char *get_nume(void)
{
return nume;
}

float get_salariu(void)
{
return salariu;
}
};

class Departament
{
Angajat a[5];
public:
Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
void operator[] (const int i);
};

Angajat& Angajat::operator = (const Angajat &a)


{
strcpy(nume,a.nume);
salariu=a.salariu;
return *this;
}

Departament::Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat


a4)
{
a[0]=a0;
a[1]=a1;
a[2]=a2;
a[3]=a3;
a[4]=a4;
}

void Departament::operator[] (const int i)


{
cout<<"Angajatul cu indexul "<<i<<", se numeste:
"<<a[i].get_nume()<<" si are salariul de: "<<a[i].get_salariu()<<" RON."
<<endl;
}

void main()
{
int ind;

Angajat a0(" Pop Stanca",1815);


Angajat a1(" Talpas Denisa",1420);
Angajat a2(" Mihuc Teo",1750);
Angajat a3(" Pasca Roxana",1940);
Angajat a4(" Pop Bogdan",1250);
Departament d(a0,a1,a2,a3,a4);

cout<<"\n Dati indexul angajatului: ";


cin>>ind;
d[ind];

_getch();
}//end main

---------------------------------------------------------------------------

/* Stanca Pop, 2011, lab09, pb03 */

/* Overload the new and delete operators for one of the classes
implemented before, in order to
allocate / de- allocate the necessary amount of memory. */

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

class Angajat
{
char nume[256];
float salariu;
public:
Angajat() {}
Angajat(char *n,float s) { strcpy(nume,n);salariu=s; }
Angajat& operator=(const Angajat&);
char *get_nume(void) { return nume; }
float get_salariu(void) { return salariu; }
};

class Departament
{
Angajat a[5];
public:
Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
Departament() {}
void set(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
void operator[] (const int i);
void *operator new(size_t dim);
void operator delete(void *p);
};

Angajat& Angajat::operator = (const Angajat &a)


{
strcpy(nume,a.nume);
salariu=a.salariu;
return *this;
}

Departament::Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat


a4)
{
a[0]=a0;
a[1]=a1;
a[2]=a2;
a[3]=a3;
a[4]=a4;
}

void Departament::set(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4)


{
a[0]=a0;
a[1]=a1;
a[2]=a2;
a[3]=a3;
a[4]=a4;
}

void Departament::operator[] (const int i)


{
cout<<"Angajatul cu indexul "<<i<<", se numeste:
"<<a[i].get_nume()<<" si are salariul de: "<<a[i].get_salariu()<<"
RON."<<endl;
}

void* Departament::operator new(size_t dim)


{
return new Departament;
}

void Departament::operator delete(void *p)


{
delete p;
}

void main()
{
int ind;

Angajat a0(" Pop Stanca",1815);


Angajat a1(" Talpas Denisa",1420);
Angajat a2(" Mihuc Teo",1750);
Angajat a3(" Pasca Roxana",1940);
Angajat a4(" Pop Bogdan",1250);

Departament d(a0,a1,a2,a3,a4);
Departament *e=new Departament;
cout<<" Index: ";
cin>>ind;
d[ind];
e->set(a0,a1,a2,a3,a4);
e[2];

_getch();
}

-------------------------------------------------------------------------

/* Stanca Pop, 2011, lab9, pb04 */

/* Write a program that implements the simple functions myFunction(...)


that returns either the value of the received
parameter or the product of all the values sent to the function. The
criteria that controls the functioning way of the
method is the number of received parameters. */

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <conio.h>
using namespace std;

class Obiect
{

public:
int myFunction(int x)
{
return x;
}

int myFunction(int x, int y)


{
return x*y;
}

};

void main()
{
Obiect ob;

cout<<ob.myFunction(3);
cout<<"\n";
cout<<ob.myFunction(3, 5);
_getch();
}
------------------------------------------------------------------------

/* Stanca Pop, 2011, lab09, pb05 */

/* Write the program that uses a class called Calculator that has as
public methods:
- int calcul(int x) that returns the square value of x;
- int calcul(int x, int y) that returns the product of x and y;
- float calcul(int x, int y, int z) that returns the result of
f(x,y,z) = (x-y)(x+z)/2;
The program receives the parameters from the command line.
Consider the case when all the methods are static. Is it possible to
have in the same time static and non-static public methods? */

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;

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 (int)pow((double)x,2);
}
int Calculator::calcul(int x, int y)
{
return x*y;
}
float Calculator::calcul(int x, int y, int z)
{
return (float)(x-y)*(x+z)/2;
}

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


{
float afis;
Calculator calc;
switch(argc)
{
case 2: afis=(float)calc.calcul(atoi(argv[1]));break;
case 3:
afis=(float)calc.calcul(atoi(argv[1]),atoi(argv[2]));break;
case 4:
afis=calc.calcul(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]));break;
default: cout<<"Numar invalid de argumente primite de la
linia de comanda";return 1;
}
cout<<"Rezultatul in functie de parametrii este: "<<afis<<endl;
_getch();
}

---------------------------------------------------------------

/* Stanca Pop, 2011, lab09, pb07 */

/* Implement a class called Complex that overloads the arithmetical


operators (+, -, *, /) for performing the corresponding
operations when applied to Complex instances (use both friend functions
(*,/) and member methods (+,-)). */

#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<stdio.h>
#include<math.h>

class Complex
{
float r;
float i;
public:
Complex();
Complex(float real);
Complex(float real,float imaginar);
Complex& operator=(const Complex&);
friend Complex operator+(Complex&, Complex&);
friend Complex operator-(Complex&, Complex&);
friend Complex operator*(Complex&, Complex&);
friend Complex operator/(Complex&, Complex&);
float get_modul(Complex);
float get_faza(Complex);
float get_re();
float get_im();
void print();
};
Complex::Complex()
{ r=0; i=0;
}
Complex::Complex(float real)
{
r=real; i=0;
}
Complex::Complex(float real,float imaginar)
{
r=real; i=imaginar;
}
Complex& Complex::operator = (const Complex &c)
{
i=c.i;
r=c.r;
return *this;
}
Complex operator + (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.i+b.i;
rez.r=a.r+b.r;
return rez;
}
Complex operator - (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.i-b.i;
rez.r=a.r-b.r;
return rez;
}
Complex operator * (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.r*b.i+a.i*b.r;
rez.r=a.r*b.r-a.i*b.i;
return rez;
}
Complex operator / (Complex &a, Complex &b)
{
Complex rez;
rez.i=(a.i*b.r-a.r*b.i)/pow(b.r,2)-pow(b.i,2);
rez.r=(a.r*b.r+a.i*b.i)/pow(b.r,2)-pow(b.i,2);
return rez;
}
float Complex::get_modul(Complex c) { return
sqrt(pow(c.r,2)+pow(c.i,2)); }
float Complex::get_faza(Complex c) { return
atan(c.i/c.r); }
float Complex::get_re() { return r; }
float Complex::get_im() { return i; }
void Complex::print() { printf("%.2f+%.2fi",r,i); }

int main(void)
{
Complex a(5,4),b(4,2),s,d,p,i;
s=a+b;
d=a-b;
p=a*b;
i=a/b;
printf("\n a=");a.print();
printf("\n b=");b.print();
printf("\n a+b=");s.print();
printf("\n a-b=");d.print();
printf("\n a*b=");p.print();
printf("\n a/b=");i.print();
_getch();
}
---------------------------------------------------------------------------

Vous aimerez peut-être aussi