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

Algoritmikë dhe Programim i Avancuar Seminar 3

1. Përcaktoni nje klase qe mban te dhenat e nje studenti si me poshte:


 numriAmzes:integer - nje numer me 6 shifra
 emri:string - string me max 30 karaktere
 mbiemi:string - string me max 30 karaktere
 mosha:integer – nga 12 deri ne 105
Ndertoni dhe implementoni funksionet per ruajtjen dhe printimin e te dhenave.

2. Percaktoni dhe implementoni nje klase Vektor qe mban te dhena te tipit integer. Ndertoni metodat :
a. Konstruktorin qe ka si parameter madhesine e vektorit
b. Destruktorin
c. setMadhesia – qe fshin vektorin e vjeter dhe krijon nje vektror te ri
d. getMadhesia – qe kthen madhesine e vektorit
e. lexo – qe lexon vektorin nga perdoruesi
f. printo – printon vektorin
g. mesatare – llogarit mesataren e vektorit
h. minimum – gjen vleren minimale te vektorit
i. inverto – inverton vektorin
j. rendit – rendit vektorin ne rend rrites

Zgjidhje

Vektor.h

class Vektor
{
public:
Vektor(int madhesia);
~Vektor();

int Getmadhesia();
void Setmadhesia(int val);
void lexo ();
void printo ();
int minimum ();
int mesatare();
void inverto();
void rendit();

private:
int madhesia;
int *v;
};

Vektor.cpp
#include <iostream>
#include "Vektor.h"
1
Algoritmikë dhe Programim i Avancuar Seminar 3
using namespace std;

Vektor::Vektor(int madhesia)
{
this->madhesia = madhesia;
v = new int[madhesia];
}

// Destructor to release the dynamic memory


Vektor::~Vektor ()
{
delete [] v;
}

void Vektor::Setmadhesia(int madhesia)


{
madhesia = madhesia;
delete [] v;
v = new int[madhesia];
}

int Vektor::Getmadhesia()
{
return madhesia;
}

void Vektor::lexo ()
{
for (int i = 0; i < madhesia; i++) {
cin >> v[i];
}
}

void Vektor::printo()
{
for (int i = 0; i < madhesia; i++) {
cout << "V [" << i <<"]: " << v[i] << endl;
}
}

int Vektor:: minimum()


{

int min =v[0];


for (int i=0; i<madhesia; i++) {
if (v[i] < min) {
min = v[i];
}
}

2
Algoritmikë dhe Programim i Avancuar Seminar 3
return min;
}

int Vektor:: mesatare()


{

float shuma;
float mesatare;
for (int i=0; i<madhesia; i++) {
shuma+=v[i];
}
mesatare = shuma/madhesia;

return mesatare;
}

void Vektor:: inverto()


{

int fillimi=0, fundi=madhesia-1, temp;


while(fillimi<fundi) {
temp=v[fillimi];
v[fillimi]=v[fundi];
v[fundi]=temp;
fundi --;
fillimi++;
}
}

void Vektor:: rendit()


{
int temp;
for(int i=0; i<madhesia-1; i++) {
for(int j=i; j<madhesia; j++) {
if(v[i]<v[j]) {
temp=v[i];
v[i]=v[j];
v[j]=temp;
}
}
}
}

main.cpp
#include <iostream>
#include "Vektor.h"
using namespace std;

int main()
{
3
Algoritmikë dhe Programim i Avancuar Seminar 3
int permasa;
cout <<"Jepni numrin e elementeve te vektorit: ";
cin>>permasa;

Vektor v1(permasa);

cout <<"Madhesia e vektorit: "<<v1.Getmadhesia()<<endl;


cout <<"Jepni elementet e vektorit: "<<endl;
v1.lexo();
cout <<"Elementet e vektorit jane: "<<endl;
v1.printo();

cout <<"Vlera minimale: "<<v1.minimum()<<endl;

cout <<"Vlera mesatare: "<<v1.mesatare()<<endl;

v1.inverto();

cout <<"Vektori reverse: "<<endl;


v1.printo();

v1.rendit();

cout <<"Vektori i renditur: "<<endl;


v1.printo();

return 0;
}

3. Krijoni nje klase Autor e cila permban:


a. Tre te dhena private: emri(const string), email (string) dhe gjinine (char ‘m’, ‘f’, ose ‘p’ ne rastin
kur eshte e papercaktuar.
b. Nje konstruktor me parametra.
c. Funksionet get dhe set per te dhenat.
d. Nje funksion per validimin e email nese pozicioni i @ nuk eshte ne fillim dhe ne fund te email.
e. Nje funksion print qe printon te dhenat ne formatin “emri (gjinia) dhe email”
Krijoni programin per testimin e klases qe krijuat.

Zgjidhje

Autor.h
#include <string>
using namespace std;
class Autor
{
public:

Autor(const string &, string &, char );


string getEmri()const;
4
Algoritmikë dhe Programim i Avancuar Seminar 3
string getEmail()const;
char getGjinia()const;
void setEmail(string &);
void setGjinia(char);
void printo();
bool valido(string);
bool operator==(const Autor &);

private:
string emri;
string email;
char gjinia;
};

Autor.cpp
#include "Autor.h"
#include <iostream>
using namespace std;
Autor::Autor(const string &e, string &em, char gjinia )
:emri(e)
{

if(valido(em))
setEmail(em);
else{
cout<<"Email eshte gabim";
exit(1);
}

setGjinia(gjinia);
}

string Autor::getEmri() const


{
return emri;
}

string Autor::getEmail()const
{
return email;
}

char Autor::getGjinia() const


{
return gjinia;
}

void Autor::setEmail(string &em)


{
5
Algoritmikë dhe Programim i Avancuar Seminar 3
this->email=em;
}

void Autor::setGjinia(char gjinia)


{
this->gjinia=gjinia;
}

void Autor::printo()
{
cout<<emri<<" ("<<gjinia<<") "<<email<<endl;

bool Autor::valido(string email)


{
if(email.at(0)=='@' || email.at(email.length()-1)=='@')
return false;
else
return true;

bool Autor::operator==(const Autor &b){


if(emri==b.getEmri() && email==b.getEmail() && gjinia==b.getGjinia())
return true;
else
return false;

main.cpp
include <iostream>
#include <string>
#include "Libri.h"

using namespace std;

int main()
{
const string e="Ismail Kadare";
string em="email@yahoo.com";
Autor a(e,em,'m');
a.printo();

const string e1="Ismail Kadare";


string em1="email@yahoo.com";

Autor b(e1,em1,'m');

6
Algoritmikë dhe Programim i Avancuar Seminar 3
if(a==b)
cout<<"Autoret jane te njejte"<<endl;
return 0;
}

4. Krijoni nje klase Libri. Supozojme se nje liber eshte shkruar nga vetem nje autor. Klasa Libri
permban:
a. Kater te dhena private emri(const string), autor (nje instance e klases autor qe krijuam ne
ushtrimin e pare), cmimi (double), sasiaStok (int me vlere defaul 0). Cmimi dhe sasia ne stok
duhet te jene vlera pozitive ose 0.
b. Konstruktorin me dhe pa parametra.
c. Funksionet get (per te gjitha fushat) dhe funksionet set vetem per te dhenat jo const.
d. Nje funksion qe kontrollon cmimin dhe nese futet nje vlere negative e ve ate ne vleren 0.
e. Nje funksion qe kontrollon sasine ne stok dhe nese futet nje vlere negative e ve ate ne vleren 0.
f. Nje funksion qe printon te dhenat e librit.
Krijoni programin per testimin e klases qe krijuat.

Zgjidhje

Libri.h

#include "Autor.h"

class Libri
{
public:
Libri(const string &,const Autor &,double, int=0);
string getTitull() const;
Autor getAutor() const;
double getCmimi() const;
int getSasia()const;
void setCmimi(double);
void setStok(int);
void checkCmimi();
void checkStok();
void printo();

private:
const string titulli;
const Autor autor;
double cmimi;
int sasiaStok;
};

Libri.cpp

#include<iostream>
#include "Libri.h"
7
Algoritmikë dhe Programim i Avancuar Seminar 3

Libri::Libri(const string &t,const Autor &a,double cmimi, int stok )


:titulli(t),autor(a)
{
setCmimi(cmimi);
checkCmimi();
setStok(stok);
checkStok();
}

string Libri::getTitull() const


{
return titulli;
}

Autor Libri::getAutor() const


{
return autor;
}

double Libri::getCmimi() const


{
return cmimi;
}

int Libri::getSasia()const
{
return sasiaStok;
}

void Libri::setCmimi(double cmimi)


{
this->cmimi=cmimi;
}

void Libri::setStok(int stok)


{
sasiaStok=stok;
}

void Libri::checkCmimi()
{
if(cmimi<0)
cmimi=0;
}

void Libri::checkStok()
{
if(sasiaStok<0)
8
Algoritmikë dhe Programim i Avancuar Seminar 3
sasiaStok=0;

void Libri::printo(){
cout<<"Titulli: "<<titulli<<"\n Autori: "<<autor.getEmri()<<" \n Cmimi: "<<cmimi<<"\n Stok:
"<<sasiaStok<<endl;
}

main.cpp

#include <iostream>
#include <string>
#include "Libri.h"

using namespace std;

int main()
{
const string e="Ismail Kadare";
string em="email@yahoo.com";
Autor a(e,em,'m');

const string titulli="Keshtjella";


double cmimi=900.5;
int sasia=100;

Libri l(titulli,a,cmimi,sasia);

Autor a1=l.getAutor();
cout<<a1.getEmail();

return 0;
}

You might also like