Вы находитесь на странице: 1из 8

# UNIVERSITATEA TEHNIC A MOLDOVEI

CATEDRA
TI

REFERAT
La C++.
LUCRAREA DE LABORATOR NR. 1
TEMA:
Structura i mecanismul abstraciei

A elaborate:

St. gr. TI

A verificat:

Lisnic Inga
Chiinu 2015

Varianta VI:

Sarcina:
) Image , :
, , ,
, . ,
- .
, , .
b) Matrix-.
Complex, - .
,

. :
(i,j).
( ), .
. .
, ,
.

Listingul Programului:
a)
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int x=0,y=0,i=0,n=0;
class Image{
public:
char *den;
char *form;
char *s;
int dimens;
int dimens_oct;
int compres;//%
Image(char *a,char *b,int c,int d,int e){den=new char [strlen(a)+1];
form=new char[strlen(b)+1];
dimens=c;
dimens_oct=d;
compres=e; }
//Image(const Image &s);
//Image();
class Image *data;
Image(){
cout<<"Introduceti n";
cin>>n;
for(i=0;i<n;i++)
{
printf("Introduceti denumirea fisierului ",i+1," ");
cin>>data[i].den;
printf("Introduceti formatul de compresie ",i+1," ");
cin>>data[i].form;
cout<<"Introduceti dimensiunea imaginii "<<i+1<<" ";
cin>>data[i].dimens;
cout<<"Introduceti dimensiunea in octeti "<<i+1<<" ";
cin>>data[i].dimens_oct;
cout<<"Introduceti compresia in % "<<i+1<<" ";
cin>>data[i].compres;
}

## cout<<"\nAti introdus datele:\n";

for(i=0;i<n;i++)
{ cout<<"Fisier "<<i+1<<endl;
printf("Denumirea fisierului: %s \n",data[i].den);
printf("Formatul de compresie: %s \n",data[i].form);
cout<<"Dimensiunea imaginii: "<<data[i].dimens<<endl;
cout<<"Dimensiunea in octeti: "<<data[i].dimens_oct<<endl;
cout<<"Compresia: "<<data[i].compres<<" %"<<endl<<endl;
}
}
void modif(void){
printf("\nDoriti sa modificati denumirea ? (1/0) ");
cin>>x;
if(x==1){
printf("\nIntroduceti nr fisierului ");
cin>>y;
printf("\nIntroduceti denumirea fisierului ");
scanf("%s",&data[y-1].den);
cout<<"Fisier "<<y-1<<endl;
printf("Denumirea fisierului: %s \n",data[y-1].den);
printf("Formatul de compresie: %s \n",data[y-1].form);
cout<<"Dimensiunea imaginii: "<<data[y-1].dimens<<endl;
cout<<"Dimensiunea in octeti: "<<data[y-1].dimens_oct<<endl;
cout<<"Compresia: "<<data[y-1].compres<<" %"<<endl<<endl;
} else exit(0);
}
};
int main()
{
//Image("","",0,0,0);
Image a;a.modif();
getch();
}

b)
#include <iostream>
#include <conio.h>
using namespace std;
class matrix
{
private:
float **A;
int n, m, err;
public:
matrix();
matrix(int, float);
matrix(int, int, float);
matrix(const matrix&);
~matrix();
void intro();
void show();
void suma(const matrix &matr);
void produs(const matrix &matr);
void diferenta(const matrix &matr);

## void inmultire(float val);

float get_element(int,int);
void print_err();
int get_err();
int get_n();
int get_m();
};
/***********************************************/
matrix::matrix()
{
err=0;
n=m=1;
if (!(A=new float*[n])) err=1;
else
if (!(A[0]=new float[m])) err=1;
else
{
A[n][m]=0;
}
}
/***********************************************/
matrix::matrix(int dim, float val=0)
{
err=0;
if (dim>=0)
{
n=m=dim;
if (!(A=new float*[n])) err=1;
else
for(int i=0; i<n; i++)
if (!(A[i]=new float[m])) err=1;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
A[i][j]=val;
}
else err=2;
}
/***********************************************/
matrix::matrix(int n1, int m1, float val=0)
{
n=n1;
m=m1;
err=0;
if ((n>=0)&&(m>=0))
{
if (!(A=new float*[n])) err=1;
else
for(int i=0; i<n; i++)
if (!(A[i]=new float[m])) err=1;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) A[i][j]=val;
} else err=2;
}
/***********************************************/
matrix::matrix(const matrix& B)
{

err=0;
n=B.n;
m=B.m;
if (!(A=new float*[n])) err=1;
else
for(int i=0; i<n; i++)
if (!(A[i]=new float[m])) err=1;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) A[i][j]=B.A[i][j];
}
/***********************************************/
matrix::~matrix()
{
err=0;
for(int i=0; i<n; i++)
delete[] A[i];
delete[] A;
A=NULL;
n=m=0;
}
/***********************************************/
int matrix::get_n()
{
err=0;
return n;
}
/***********************************************/
int matrix::get_m()
{
err=0;
return m;
}
/***********************************************/
int matrix::get_err()
{
return err;
}
/***********************************************/
float matrix::get_element(int i,int j)
{
if((i<0)&(i>n)&(j<0)&(j>m)) { err=5; return -1;}
else
return A[i][j];
}
/***********************************************/
void matrix::print_err()
{
switch (err)
{
case 0: { cout<<"Totul e OK."<<endl; break;}
case 1: { cout<<"Nu este de ajuns memorie."<<endl; break;}
case 2: { cout<<"Dimensiunea matricei trebuie sa fie pozitiva."<<endl; break;}
case 3: { cout<<"Trecerea peste limitele matricei."<<endl; break;}
case 4: { cout<<"Dimensiunile matricelor nu coincid."<<endl; break;}
case 5: { cout<<"Matricele nu sunt compatibile pentru inmultire."<<endl; break;}
}

## cout<<"Press any key to continue..."<<endl;

getch();
}
/***********************************************/
void matrix::intro( )
{
cout<<"n="<<n<<" m="<<m<<endl;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
cin>>A[i][j];
}
/***********************************************/
void matrix:: show( )
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
}
/***********************************************/
void matrix::suma(const matrix &matr)
{
err=0;
//matrix temp(n,m);
if ((n==matr.n)&&(m==matr.m))
{
for(int i=0; i<matr.n; i++)
for(int j=0; j<matr.m; j++)
A[i][j]+=matr.A[i][j];
}
else err=4;
}
/***********************************************/
void matrix::diferenta(const matrix &matr)
{
err=0;
if ((n==matr.n)&&(m==matr.m))
{
for(int i=0; i<matr.n; i++)
for(int j=0; j<matr.m; j++)
A[i][j]-=matr.A[i][j];
}
else err=4;
}
/***********************************************/
void matrix::produs(const matrix &vec)
{
err=0;
matrix temp(n,vec.m);
if (m==vec.n)
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)

{
float tmp=0;
for(int k=0; k<vec.n; k++)
tmp+=A[i][k]*vec.A[k][j];
temp.A[i][j]=tmp;
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
A[i][j]=temp.A[i][j];
}
else err=5;
}
/***********************************************/
void matrix::inmultire( float val)
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
A[i][j]*=val;
}
/***********************************************/
int main()
{
matrix m1(3);
cout<<"Dati prima matrice"<<endl;
m1.intro();
cout<<"Matricea introdusa: "<<endl;
m1.show();
matrix m2(3);
cout<<endl<<"Dati a doua matrice"<<endl;
m2.intro();
cout<<"Matricea introdusa: "<<endl;
m2.show();
getch();
m1.suma(m2);
cout<<"Suma m1+m2: "<<endl;
m1.show();
getch();
m2.diferenta(m1);
cout<<"Diferentza m2-m1: "<<endl;
m2.show();
getch();
m1.produs(m2);
cout<<"Produsul m1*m2: "<<endl;
m1.show();
getch();