Vous êtes sur la page 1sur 12

Laboratory 6

1) /*Write a C++ application that implements a class called F1Pilot. The


class defines the private
variables name (array of characters), team (array of characters), age (int),
best_time (int) and
pole_position_nr. As public members, the class contains mutator and accessor
methods for
each of the classs attributes.
In function main, create 3 different instances of the F1Pilot class and use
the mutator methods
for initializing each objects data with the corresponding information read
from the keyboard.
Using the accessor methods, display all the data related to the pilot that has
the best time.*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class PilotF1
{
char *nume;
char *echipa;
int varsta;
int record;
int nr_pole_position;
public:
PilotF1() {}
PilotF1(PilotF1& pilot);
void write_nume(char n[]);
void write_echipa(char e[]);
void write_varsta(int v);
void write_record(int r);
void write_nr_pole_position(int p);
const char *get_nume();
const char *get_echipa();
int get_varsta();
int get_record();
int get_nr_pole_position();
};
void Afisare(PilotF1 *pilot);
int main(void)
{
int nr_piloti,best[2]={0,0};//best[0] reprezinta valoarea recordului
mare, best[1] reprezinta i-ul pilotului;
char bufc[256];
int bufi;
printf("Introduceti cati piloti doriti sa
introduceti:");scanf("%d",&nr_piloti);
PilotF1 *p=new PilotF1[nr_piloti];

for(int i=0;i<nr_piloti;i++)//scriere date


{
printf("Introduceti datele pentru pilotul nr.%d:\n",i+1);
printf("Nume:");scanf("%s",bufc);
p[i].write_nume(bufc);
printf("Echipa:");scanf("%s",bufc);
p[i].write_echipa(bufc);
printf("Varsta:");scanf("%d",&bufi);
p[i].write_varsta(bufi);
printf("Record:");scanf("%d",&bufi);
p[i].write_record(bufi);
printf("Nr Pole Position:");scanf("%d",&bufi);
p[i].write_nr_pole_position(bufi);
}
for(int i=0;i<nr_piloti;i++)//comparare record
{
if(best[0]<p[i].get_record())
{
best[0]=p[i].get_record();
best[1]=i;
}
}
Afisare(&p[best[1]]);//afisare best
return 0;
}
PilotF1::PilotF1(PilotF1& pilot)
{
nume=new char[(strlen(pilot.nume))];
echipa=new char[(strlen(pilot.echipa))];
strcpy(nume,pilot.nume);
strcpy(echipa,pilot.echipa);
varsta=pilot.varsta;
record=pilot.record;
nr_pole_position=pilot.nr_pole_position;
}
void PilotF1::write_nume(char n[])
{
if(nume!=NULL) delete nume;
nume=new char[strlen(n)+1];
strcpy(nume,n);
}
void PilotF1::write_echipa(char e[])
{
if(echipa!=NULL) delete echipa;
echipa=new char[strlen(e)+1];
strcpy(echipa,e);
}
void PilotF1::write_varsta(int v)
{
varsta=v;
}
void PilotF1::write_record(int r)
{

record=r;
}
void PilotF1::write_nr_pole_position(int p)
{
nr_pole_position=p;
}
const char *PilotF1::get_nume()
{
return nume;
}
const char *PilotF1::get_echipa()
{
return echipa;
}
int PilotF1::get_varsta()
{
return varsta;
}
int PilotF1::get_record()
{
return record;
}
int PilotF1::get_nr_pole_position()
{
return nr_pole_position;
}
void Afisare(PilotF1 *pilot)
{
printf("Pilotul %s apartinand echipei %s avand varsta %d de ani cu un
numar de %d pole positionuri a obtinut un record de %d",pilot>get_nume(),pilot->get_echipa(),pilot->get_varsta(),pilot>get_nr_pole_position(),pilot->get_record());

2) /*Modify the 2-nd example so that the code could be launched into
execution*/
#include <iostream>
using namespace std;
class Test2
{
int x;
public:
Test2()
{
cout<<"\nApel constructor explicit vid.";
}
void write_x(int y) { x=y; }

};
int main(void)
{
Test2 ob1;
int a;
cout<<"\nIntroduceti valoarea variabilei de tip \"int\" din clasa: ";
cin>>a;
ob1.write_x(a);
return 0;
}

3) /*Starting with the code that exemplifies the matrix operations, add the
specific methods for:
- displaying the elements from the main diagonal of the matrix, in case the
matrix is square;
if not, display a significant message;
- displaying the elements which are below the secondary diagonal;
- displaying a matrix that has identical dimensions with the original matrix
and its elements
can have as possible values 0s (if the corresponding element is greater than
a certain
threshold value) or 1s (otherwise);
*/
#include <iostream>
using namespace std;
class Matrix
{
//atribute
int matrix[10][10], dim1, dim2;
public:
//constructor explicit vid
Matrix()
{
//variabile locale
int i, j;
cout<<"\nIntroduceti dimensiunile matricii: ";
cin>>dim1;
cin>>dim2;
cout<<"\nIntroduceti elementele matricii: ";
for(i=0; i<dim1; i++)
{
for(j=0; j<dim2; j++)
{
cout<<"\nmatrix["<<i<<"]["<<j<<"] = ";
cin>>matrix[i][j];
}
}
}

//metoda de afisare a matricii din clasa; implementare in cadrul


clasei
void displayMatrix()
{
//variabile locale
int i, j;
cout<<"\nElementele matricii: ";
for(i=0; i<dim1; i++)
{
cout<<"\n";
for(j=0; j<dim2; j++)
{
//apelul metodei private care returneaza
valoarea unui element din matrice
cout<<returnElement(i, j)<<" ";
}
}
cout<<"\n";
}
void displayDiagonal()
{
if(dim1==dim2)
{
cout<<"Diagonala principala contine urmatoarele
elemente: ";
for(int i=0;i<dim1;i++)
{
cout<<returnElement(i,dim1-1-i);
if(i!=dim1-1) cout<<", ";
else cout<<"."<<endl;
}
}
else
{
cout<<"Matricea nu este patratica=>diagonala principala
nu poate fi afisata";
}
cout<<endl;
}
void displayunderDiagonal()
{
//variabile locale
int i, j;
cout<<"\nElementele matricii: ";
for(i=0; i<dim1; i++)
{
cout<<"\n";
for(j=0; j<dim2; j++)
{
//apelul metodei private care returneaza
valoarea unui element din matrice
if(i>j) cout<<returnElement(i, j)<<" ";
}

}
cout<<"\n";
}
void displayAnother(int v)
{
//variabile locale
int i, j, t;
cout<<"\nElementele matricii: ";
for(i=0; i<dim1; i++)
{
cout<<"\n";
for(j=0; j<dim2; j++)
{
//apelul metodei private care returneaza
valoarea unui element din matrice
t=returnElement(i, j)>v?0:1;
cout<<t<<" ";
}
}
cout<<"\n";
}
//declararea metodei de afisare a elementelor unei coloane
void displayColumn(int col);
private:
//declararea metodei de afisare a unui element
int returnElement(int row, int column);
};
//implementarea externa a metodelor (publice sau private) declarate in
clasa
void Matrix::displayColumn(int col)
{
if(col<0||col>dim2)
{
cout<<"\nColoana cu numarul "<<col<<" nu exista in
matricea din clasa!\n";
}
else
{
cout<<"\nElementele coloanei "<<col<<": ";
for(int i=0; i<dim1; i++)
{
cout<<returnElement(i, col)<<" ";
}
}
cout<<endl;
}
int Matrix::returnElement(int row, int column)
{
return matrix[row][column];
}
int main(void)
{
Matrix m1; //instantiere

int c;
m1.displayMatrix();
cout<<"\nIntroduceti un numar de coloana ale carei elemente vor fi
afisate: ";
cin>>c;
m1.displayColumn(c);
m1.displayDiagonal();
m1.displayunderDiagonal();
cout<<"\nIntroduceti o valoare de referinta pentru matrice:";
cin>>c;
m1.displayAnother(c);
//incercare (imposibila) de a accesa direct un membru privat al clasei
//m1.returnElement(0, 0);
return 0;
}

4) /*Write a C++ class that has as private variable a date field. The date is
defined as a structure
declared outside the class and it contains the fields day int, month int,
year int. The class
contains public accessor and mutator methods that are capable of using the
private
information. The class also contains two public methods that:
- test the validity of the stored date;
- write into a file all the dates from the current year that precede
chronologically the class
stored date;
In the main method, after instantiating the class and after reading from the
keyboard all the
components of a date, call the member methods and then verify the obtained
results.*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct data
{
int zi;
int luna;
int an;
};
class Calendar
{
data calend;
public:
void write_data(data cal)
{
calend.zi=cal.zi;
calend.luna=cal.luna;

calend.an=cal.an;
}
data get_data()
{
return calend;
}
int validate(data cal)
{
if((cal.zi>=1&&cal.zi<=31)&&(cal.luna>=1&&cal.luna<=12)&&(cal.an>=1&&cal.an<=1
00000)) return 1;
else return 0;
}
void write_file(const char *calea)
{
FILE *f;
if(!(f=fopen(calea,"w")))
{
printf("Eroare la crearea fisierului\n");
return;
}
for(int i=calend.luna;i<=12;i++)
{
for(int j=calend.zi;j<=31;j++)
{
fprintf(f,"%d.%d.%d\n",j,i,calend.an);
}
}
printf("Datele au fost scrise cu success!\n");
fclose(f);
}
};
int main(void)
{
Calendar cal;
data tmp;
printf("Introduceti o data in formatul dd/zz/aaaa: ");scanf("%d/%d/
%d",&tmp.zi,&tmp.luna,&tmp.an);
cal.write_data(tmp);
if(cal.validate(cal.get_data())) cal.write_file("txt.txt");
else printf("Data nu are formatul corect. Datele nu au fost scrise in
fisier.\n");
printf("Data scrisa in clasa este: %d/%d/
%d",cal.get_data().zi,cal.get_data().luna,cal.get_data().an);
return 0;
}

6) /*Write a C++ application that defines a class called Triangle. The class
contains as private
variables the triangle's sides a, b and c. The methods included in the class
will determine
whether the values of a,b and c form a triangle and will calculate the shape's
area and
perimeter. Write a distinct method that will print a specific message if the
triangle is right*/
#include <iostream>
using namespace std;
class Triunghi {
private:
float a, b, c;
public:
void verif_dreptunghic();
Triunghi(int x, int y, int z) {
a=x;
b=y;
c=z;
}
int verifica_laturi() {
if(a+b>c && a+c>b && b+c>a)
return 1;
else
return 0;
}

float calcul_arie() {
float p, arie;
p=(a+b+c)/2;
arie=sqrt(p*(p-a)*(p-b)*(p-c));
return arie;
}

int calcul_perimetru() {
int perimetru;
perimetru=a+b+c;
return perimetru;
}
};
void Triunghi::verif_dreptunghic() {
if(a*a+b*b==c*c || a*a + c*c==b*b || b*b+c+c==a*a)

cout<<"Triunghiul este dreptunghic."<<endl;


else
cout<<"Triunghiul nu este dreptunghic!!!"<<endl;
}
void main () {
int x, y, z, perimetru;
float arie;
cout<<"Dati prima latura: ";
cin>>x;
cout<<"Dati a doua latura: ";
cin>>y;
cout<<"Dati a treia latura: ";
cin>>z;
Triunghi t(x, y, z);
if(t.verifica_laturi()==1) {
arie=t.calcul_arie();
cout<<"Aria este: "<<arie<<endl;
perimetru=t.calcul_perimetru();
cout<<"Perimetrul este: "<<perimetru<<endl;
t.verif_dreptunghic();
}
else
cout<<"Laturile alese nu pot forma un triunghi."<<endl;
}

/*Write a class named Safe that has as private attributes the cipher and the
amount of money.
Implement the private accessor and mutator methods getMoney() and putMoney().
The public
methods putInSafe() and getFromSafe() will call the previous methods only if
the cipher sent
as parameter matches the value stored inside the class. Display a message if
the cipher is not
correct.*/
#include <iostream>
#include<conio.h>
using namespace std;
class Seif
{
private:
float suma;
float cifru;
float get_sum ()

{
return suma;
}
void set_sum (float s)
{
suma=s;
}
public:
void puneInSeif (float summ )
{
float m=0;
set_sum(0);
m=get_sum();
set_sum(summ+m);
cout<<"\n Noua suma din cont este :"<<summ+m;
}
float scoateDinSeif (float summ1)
{
float su=0;
su=get_sum();
return summ1;
set_sum(su-summ1);
cout<<"\n Suma extrasa din cont este :"<<summ1;
cout<<"\n Noua suma din cont este :"<<su-summ1;
}
int Cifru (int p)
{
cifru=1234;
if(p!=cifru)
return 0;
else
return 1;
}
};
void main (void)
{
Seif card1; //instantiere
float pin, summ;
int x;
cout<<"\nIntroduceti pinul: "; cin>>pin;
while (card1.Cifru(pin)==0)
{
cout<<"\nParola gresita!! Reintroduceti: ";
cin>>pin;
}
cout<<"\nPentru a efectua o depunere introduceti valoarea 1, pentru a efectua
o extragere introduceti valoarea 2: ";
cin>>x;
if (x==1)
{
cout<<"\nSuma pe care doriti sa o depuneti: ";
cin>>summ;

card1.puneInSeif(summ);
}
else
if (x==2)
{
cout<<"\nSuma pe care doriti sa o scoateti: ";
cin>>summ;
card1.scoateDinSeif(summ);
}
else
{
cout<<"Introduceti din nou valoarea: ";
cin>>x;
}
getche ();
}

Vous aimerez peut-être aussi