1- laboratoriya ishi konteynerlar (collections)



Download 89,18 Kb.
bet19/20
Sana20.03.2022
Hajmi89,18 Kb.
#502935
1   ...   12   13   14   15   16   17   18   19   20
Masalani yechish goyasi:
queue ва priority_queue konteynerlari to’plami yaratiladi. que ва priority_queue ning push() iteratoridan foydalanib, queue ва priority_queue to’plamga qiymatlar o’zlashtiriladi. Ichma ich sikl jarayonidan foydalanib, bir xil hajmli imtixonga formasiz kelgan talabalar soni aniqlanadi. Umumiy talabalar sonidan formasiz kelgan talabalar soni ayrilib imtixonga umuman kelmagan talabalar soni aniqlanadi.
Dastur matni:

  1. #include "stdafx.h"

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. using namespace System;

  9. using namespace std;

  10. class Talaba{

  11. int yosh;

  12. std::string ism;

  13. std::string fam;

  14. float step;

  15. bool imtixon;

  16. public:

  17. bool get_imtixon(){return imtixon;}

  18. void set(int Y, std::string I, std::string F, float S, bool Im){

  19. yosh = Y;

  20. ism = I;

  21. fam = F;

  22. step = S;

  23. imtixon = Im;

  24. }

  25. void get(){

  26. cout<<"Ismi: "<

  27. cout<<"Familiyasi: "<

  28. cout<<"Yoshi: "<

  29. cout<<"Stependiyasi: "<

  30. cout<<"Formaning mavjudligi: "<

  31. }

  32. };

  33. int main(){

  34. int n; cout<<"Talabalar sonini kiriting:= "; cin>>n; cin.ignore();




  1. queue Navbat[100];

  2. Talaba t[100];

  3. string I,F; int Y; float S; bool Im=false;

  4. for (int i = 0; i < n; i++)

  5. {

  6. cout<

  7. cout<<"Ism: "; cin>>I;

  8. cout<<"Fam: "; cin>>F;cin.ignore();

  9. cout<<"Yosh: "; cin>>Y;

  10. cout<<"Step: "; cin>>S;cin.ignore();

  11. cout<<"Formaning bor yo'qligi: 0 yo'q, bor 1: "; cin>>Im;

  12. t[i].set(Y,I,F,S,Im);

  13. Navbat[i].push(t[i]);

  14. }

  15. cout<

  16. int f_soni=0;

  17. for (int i = 0; i < n; i++){

  18. if(t[i].get_imtixon() == 0){

  19. cout<

  20. f_soni++;

  21. Navbat[i].front().get();

  22. }

  23. }

  24. cout<<"Formasiz kelgan talabalar soni: "<




  1. cout<

  2. f_soni=0;

  3. for (int i = 0; i < n; i++){

  4. if(t[i].get_imtixon() == 1){

  5. cout<

  6. f_soni++;

  7. Navbat[i].front().get();

  8. }

  9. }

  10. cout<<"Formada kelgan talabalar soni: "<




  1. cout<




  1. for (int i = 0; i < n; i++){

  2. cout<

  3. Navbat[i].front().get();

  4. }

  5. cout<<"Guruhdagi jami talabalar soni: "<

  6. getchar();

  7. getchar();

  8. }



Dastur natijasi:

Talabalar sonini kiriting:= 3
1 - talaba haqida kiriting
Ism: Oybek
Fam: Mallayev
Yosh: 28
Step: 500000
Formaning bor yo'qligi: 0 yo'q, bor 1: 1
2 - talaba haqida kiriting
Ism: Akbar
Fam: Sattarov
Yosh: 18
Step: 750000
Formaning bor yo'qligi: 0 yo'q, bor 1: 0
3 - talaba haqida kiriting
Ism: Odil
Fam: Ishniyazov
Yosh: 20
Step: 560000
Formaning bor yo'qligi: 0 yo'q, bor 1: 1


Imtixonga formasiz kelgan talabalar

1 - talaba haqidagi ma'lumotlar


Ismi: Akbar
Familiyasi: Sattarov
Yoshi: 18
Stependiyasi: 750000
Formaning mavjudligi: 0
Formasiz kelgan talabalar soni: 1


Imtixonga formada kelgan talabalar

1 - talaba haqidagi ma'lumotlar


Ismi: Oybek
Familiyasi: Mallayev
Yoshi: 28
Stependiyasi: 500000
Formaning mavjudligi: 1

2 - talaba haqidagi ma'lumotlar


Ismi: Odil
Familiyasi: Ishniyazov
Yoshi: 20
Stependiyasi: 560000
Formaning mavjudligi: 1
Formada kelgan talabalar soni: 2


Imtixonga jami kelgan talabalar ro'yhati

1 - talaba haqidagi ma'lumotlar


Ismi: Oybek
Familiyasi: Mallayev
Yoshi: 28
Stependiyasi: 500000
Formaning mavjudligi: 1

2 - talaba haqidagi ma'lumotlar


Ismi: Akbar
Familiyasi: Sattarov
Yoshi: 18
Stependiyasi: 750000
Formaning mavjudligi: 0

3 - talaba haqidagi ma'lumotlar


Ismi: Odil
Familiyasi: Ishniyazov
Yoshi: 20
Stependiyasi: 560000
Formaning mavjudligi: 1
Guruhdagi jami talabalar soni: 3



2.10. Standart algoritmlar va iteratorlardan foydalanib, amaliy dasturlar yaratish


Ishning maqsadi: C++ dasturlash tilida Standart algoritmlar va iteratorlar sinflari va uning qayta yuklangan turlaridan foydalanish ko’nikmalarini egallsh.
Masalaning qo’yilishi:
Standart algoritmlar va iteratorlar asosida yaratilgan to’plam elementlarini
copy(from, from_end, to),
copy_if(from, from_end, to, pred),
copy_n(from, count, to),
copy_backward(from, from_end, to_end),
move(from, from_end, to),
move_backward(from, from_end, to_end),
swap_ranges(a, a_end, b),
fill(to, to_end, value), fill_n(to, n, value),
generate(to, to_end, gen),
generate(v.begin(), v.end(), rand),
generate_n(to, n, gen), reverse(begin, end),
reverse_copy(from, from_end, to),
find(begin, end, value),
mismatch(a, a_end, b, b_end, eq),
max_element(begin, end, comp),
min_element(begin, end, comp),
for_each(begin, end, fun),
replace(begin, end, old_val,
new_val), remove(begin, end, value),
remove_if(begin, end, pred),
sort(begin, end, comp),
iteratorlar yordamida qayta ishlash.
Masala:
Matnli faylda N ta talabaning familiyasi, ismi va yoshi yozilgan. Ushbu ma’lumotlarni to’plamga o’zlashtirib yoshi bo’yicha saralovchi dastru tuzing.
Masalani yechish goyasi:
Talaba haqida har xil turdagi ma’lumotlarni boshqarish uchun “Talaba” nomli struktura yaratiladi. istream_iterator iteratoridan foydalanib, matnli fayldan ma’lumotlar o’zlashtiriladi va ostream_iterator iteratoridan foydalanib, ma’lumotlarni matnli faylga yoki natijalar oynasiga chiqarish mumkin. Yosh bo’yicha saralash uchun sort(), ma’lumotlarni vector ga joylashtirish uchun copy(), ixtiyoriy yoshdan ixtiyoriy yoshgacha ma’lumotlarni chiqarish uchun copy_if() algoritmlaridan foydalaniladi. “Talaba” sun’iy tur bo’lganligi uchun kiritish va chiqarish oqimlari(>>, <<) qayta yuklanadi.


Dastur matni:

  1. #include "stdafx.h"

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include




  1. using namespace System;

  2. using namespace std;

  3. struct Talaba{

  4. std::string firstname, secondname;

  5. size_t age;

  6. };




  1. bool comparator(const Talaba& p1, const Talaba& p2){

  2. return p1.age < p2.age;

  3. }

  4. std::ostream& operator << (std::ostream& out, const Talaba& p){

  5. out << p.firstname << " " << p.secondname << " " << p.age;

  6. return out;

  7. }




  1. std::istream& operator>>(std::istream& in, Talaba& p){

  2. in >> p.firstname >> p.secondname >> p.age;

  3. return in;

  4. }

  5. struct predicate{

  6. size_t begin, end;

  7. predicate(int p1, int p2): begin(p1), end(p2) {}

  8. bool operator ()(const Talaba &p){

  9. return (p.age > begin) && (p.age < end);

  10. }

  11. };




  1. void File_out_in(){

  2. std::ifstream fin("input.txt");

  3. std::ofstream fout("output.txt");

  4. if(fin==NULL){cout<<"fayl not found"; return;}

  5. std::vector v;




  1. std::copy(std::istream_iterator(fin), std::istream_iterator(), std::inserter(v, v.end()));

  2. std::sort(v.begin(), v.end(), comparator);




  1. std::copy_if(v.begin(), v.end(), std::ostream_iterator(std::cout, "\n"), predicate(20, 25));




  1. std::copy(v.begin(), v.end(), std::ostream_iterator(fout, "\n"));

  2. //std::copy(v.begin(), v.end(), std::ostream_iterator(cout, "\n"));

  3. }

  4. int main(){

  5. File_out_in();

  6. getchar();

  7. getchar();

  8. }

Dastur natijasi:

Natijalar oynasi
Salimov Nuriddin 21
Sharipov Komil 21
Mallayev Oybek 22
Holiqov Abdulla 23
Abdullayev Jalil 24

Input.txt fayli fayl
Mallayev Oybek 22
Salimov Nuriddin 21
Holiqov Abdulla 23
Abdullayev Jalil 24
Sharipov Komil 21

Yuqorida keltirilgan algoritmalarning baza birlariga misollar keltiramiz:


reverse_copy shabloni:
template
OutputIterator reverse_copy (BidirectionalIterator first,
BidirectionalIterator last, OutputIterator result)
{
while (first!=last) {
--last;
*result = *last;
++result;
}
return result;
}
reverse_copy algoritmiga misol:

  1. #include "stdafx.h"

  2. // reverse_copy ga misollar

  3. #include // std::cout

  4. #include // std::reverse_copy

  5. #include // std::vector

  6. using namespace std;

  7. int main () {

  8. int Int_massiv[] ={1,2,3,4,5,6,7,8,9};

  9. vector myvector;




  1. myvector.resize(9); // joy ajratish




  1. reverse_copy (Int_massiv, Int_massiv+9, myvector.begin());




  1. // tarkibni chop qilish:

  2. cout << "myvector tarkibi:";

  3. for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)

  4. cout << ' ' << *it;




  1. cout << '\n';

  2. getchar();

  3. return 0;

  4. }

Dastur natijasi: myvector tarkibi: 9 8 7 6 5 4 3 2 1
Find shabloni:
template
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}

find(begin, end, value) algoritmiga misol:

  1. #include "stdafx.h"

  2. // find misol

  3. #include // std::cout

  4. #include // std::find

  5. #include // std::vector

  6. using namespace std;

  7. int main () {

  8. int Int_massiv[] = { 10, 20, 30, 40 };

  9. int * p;




  1. p = find (Int_massiv, Int_massiv+4, 30);

  2. if (p != Int_massiv+4)

  3. cout<<"Int_massiv da element topilgan: "<<*p<<'\n';

  4. else


  5. Download 89,18 Kb.

    Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   20




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish