Masalani yechish g‘oyasi:
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:
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace System;
using namespace std;
class Talaba{
int yosh;
std::string ism;
std::string fam;
float step;
bool imtixon;
public:
bool get_imtixon(){return imtixon;}
void set(int Y, std::string I, std::string F, float S, bool Im){
yosh = Y;
ism = I;
fam = F;
step = S;
imtixon = Im;
}
void get(){
cout<<"Ismi: "<
cout<<"Familiyasi: "<
cout<<"Yoshi: "<
cout<<"Stependiyasi: "<
cout<<"Formaning mavjudligi: "<
}
};
int main(){
int n; cout<<"Talabalar sonini kiriting:= "; cin>>n; cin.ignore();
queue Navbat[100];
Talaba t[100];
string I,F; int Y; float S; bool Im=false;
for (int i = 0; i < n; i++)
{
cout<
cout<<"Ism: "; cin>>I;
cout<<"Fam: "; cin>>F;cin.ignore();
cout<<"Yosh: "; cin>>Y;
cout<<"Step: "; cin>>S;cin.ignore();
cout<<"Formaning bor yo'qligi: 0 yo'q, bor 1: "; cin>>Im;
t[i].set(Y,I,F,S,Im);
Navbat[i].push(t[i]);
}
cout<
int f_soni=0;
for (int i = 0; i < n; i++){
if(t[i].get_imtixon() == 0){
cout<
f_soni++;
Navbat[i].front().get();
}
}
cout<<"Formasiz kelgan talabalar soni: "<
cout<
f_soni=0;
for (int i = 0; i < n; i++){
if(t[i].get_imtixon() == 1){
cout<
f_soni++;
Navbat[i].front().get();
}
}
cout<<"Formada kelgan talabalar soni: "<
cout<
for (int i = 0; i < n; i++){
cout<
Navbat[i].front().get();
}
cout<<"Guruhdagi jami talabalar soni: "<
getchar();
getchar();
}
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 g‘oyasi:
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:
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace System;
using namespace std;
struct Talaba{
std::string firstname, secondname;
size_t age;
};
bool comparator(const Talaba& p1, const Talaba& p2){
return p1.age < p2.age;
}
std::ostream& operator << (std::ostream& out, const Talaba& p){
out << p.firstname << " " << p.secondname << " " << p.age;
return out;
}
std::istream& operator>>(std::istream& in, Talaba& p){
in >> p.firstname >> p.secondname >> p.age;
return in;
}
struct predicate{
size_t begin, end;
predicate(int p1, int p2): begin(p1), end(p2) {}
bool operator ()(const Talaba &p){
return (p.age > begin) && (p.age < end);
}
};
void File_out_in(){
std::ifstream fin("input.txt");
std::ofstream fout("output.txt");
if(fin==NULL){cout<<"fayl not found"; return;}
std::vector v;
std::copy(std::istream_iterator(fin), std::istream_iterator(), std::inserter(v, v.end()));
std::sort(v.begin(), v.end(), comparator);
std::copy_if(v.begin(), v.end(), std::ostream_iterator(std::cout, "\n"), predicate(20, 25));
std::copy(v.begin(), v.end(), std::ostream_iterator(fout, "\n"));
//std::copy(v.begin(), v.end(), std::ostream_iterator(cout, "\n"));
}
int main(){
File_out_in();
getchar();
getchar();
}
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:
#include "stdafx.h"
// reverse_copy ga misollar
#include // std::cout
#include // std::reverse_copy
#include // std::vector
using namespace std;
int main () {
int Int_massiv[] ={1,2,3,4,5,6,7,8,9};
vector myvector;
myvector.resize(9); // joy ajratish
reverse_copy (Int_massiv, Int_massiv+9, myvector.begin());
// tarkibni chop qilish:
cout << "myvector tarkibi:";
for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
getchar();
return 0;
}
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:
#include "stdafx.h"
// find misol
#include // std::cout
#include // std::find
#include // std::vector
using namespace std;
int main () {
int Int_massiv[] = { 10, 20, 30, 40 };
int * p;
p = find (Int_massiv, Int_massiv+4, 30);
if (p != Int_massiv+4)
cout<<"Int_massiv da element topilgan: "<<*p<<'\n';
else
Do'stlaringiz bilan baham: |