Toshquvvatov Samandar swd014-L3 guruh talabasi 8-laboratoriya ishi


Download 117.5 Kb.
Sana03.06.2020
Hajmi117.5 Kb.
#114097
Bog'liq
Labaratoriya ishi 8 Suyunova M


Toshquvvatov Samandar

SWD014-L3 guruh talabasi

8-laboratoriya ishi

12-variant

OYD da juda murakkab saralash algoritmlari asosida dasturlar yaratish
Ishning maqsadi: C++ tilida classlar, kontener ko’nikmalari asosida murakkab saralash algortimlarni imkoniyatlaridan foydalanib dasturlash bilimlarini shakllantirish.

Topshiriq: Berilgan class va kontenerlar orqali murakkab saralash algoritmlaridan foydalanib dastur yozing.

1.

class hayvonlar:

nomi,

necha_yili_yashashi



class parrandalar :

turi


yashash joyi,

Array konteniridan foydalanib ishchilar parrandalar ro’yxatida necha yil yashashi bo’yicha saralang.

#include

#include

#include

#include

#include

using namespace std;

int n;

class hayvonlar



{

public:


string nomi;

int yil_yashash;

};

class parranda:public hayvonlar{



public:

string turi;

string yashash_joyi;

parranda(string nom, string t, int yy, string m){

this->nomi = nom;

this->turi = t;

this->yil_yashash = yy;

this->yashash_joyi = m;

}

int gety(){



return yil_yashash;

}

string getn(){



return nomi;

}

string gett(){



return turi;

}

string getyj(){



return yashash_joyi;

}

void print()



{

cout <

}

};

void add(list


& l)

{

int yy;



string nom, t, yj;

cout<<"Parrandalar soni: ";

cin>>n;

for(int i=0; i

cout<

cin>>nom;

cout<

cin>>t;


cout<cin>>yy;


cout<cin>>yj;


cout<l.push_back(parranda(nom,t,yy,yj));

}

}

void print(list


l)

{

cout<<"\n Nomi | turi | yashash yili | yashash manzili\n";



list
::iterator itr;

for (itr = l.begin();

itr != l.end();

itr++)


{

(*itr).print();

}

}

void Shell(list


l){

list
::iterator itr;

int a[n],k=0;

string b[n], c[n], d[n];

for (itr = l.begin();

itr != l.end();

itr++)

{

a[k]=(*itr).gety();



b[k]=(*itr).getn();

c[k]=(*itr).gett();

d[k]=(*itr).getyj();

k++;


}

for (int gap = k / 2; gap > 0; gap /= 2) {

for (int i = gap; i < k; i += 1) {

int temp = a[i];

int j;

for (j = i; j >= gap && a[j - gap] > temp; j -= gap)



a[j] = a[j - gap];

a[j] = temp; } }

list
l1;

for (int i = 0; i

l1.push_back(parranda(b[i], c[i], a[i], d[i]));

}

print(l1);



}

int main()

{

list
hayvon;



add(hayvon);

print(hayvon);

printf("\nSorting list:\n");

//Shell sort



Shell(hayvon);

}


2.

Sovitgich” classini yarating.

Maydonlari: Marka, Model, Narx,bo’yi_sm, eni_sm.

Array konteniridan foydalanib narxi bo’yicha saralang.



(kamida 37 xonali sonlar ketma-ketnligidan iborat bo‘lsin).

#include

#include

#include

#include

#include

using namespace std;

class sovitgich{

public:

string marka;



string model;

string narx;

int buy;

int eni;


sovitgich(string ma, string mo, int b, int e, string n){

this->marka = ma;

this->model = mo;

this->buy = b;

this->eni = e;

this->narx = n;

}

void print()



{

cout <

}

};

void add(vector& v)



{

int n; int b, e;

string ma,mo,narx;

cout<<"Sovitgich soni: ";

cin>>n;

for(int i=0; i

cout<<"Sovitgich Markasi: ";

cin>>ma;


cout<<"Sovitgich Modeli: ";

cin>>mo;


cout<<"Sovitgich bo'yi: ";

cin>>b;


cout<<"Sovitgich eni: ";

cin>>e;


cout<<"Sovitgich Narxi: ";

cin>>narx;

cout<

v.push_back(sovitgich(ma,mo,b,e,narx));

}

}

void printvec(vector v)



{

cout<<"\n Marka | Model | buyi | eni | narxi:\n";

vector::iterator itr;

for (itr = v.begin();

itr != v.end();

itr++)


{

(*itr).print();

}

}

static bool sortBynarx(const sovitgich &lhs, const sovitgich &rhs)



{

int n1 = lhs.narx.length(), n2 = rhs.narx.length();

if (n1 < n2)

return true;

if (n2 < n1)

return false;

// Agar uzunliklar bir xil bo'lsa

for (int i=0; i

{ if (lhs.narx[i] < rhs.narx[i])

return true;

if (lhs.narx[i] > rhs.narx[i])

return false;

} return false;

}

int main()



{

vector vec;

add(vec);

printvec(vec);

cout<<"\n\nSaralangan holda: \n";

sort(vec.begin(), vec.end(), sortBynarx);



printvec(vec);

}


Download 117.5 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling