IKKINCHI ASSISMENT TOPSHIRIG‘I
Xotirani ajratish va foydlanishga oid berilgan quyi dagi dastur bo‘yicha berilgan topshiriqlar kerakli fragmentlari asosida bajaring.
Bunda dasturdagi baʻzi o‘zgartirishlarni topish orqali topshiriqlar bosqichma – bosqich amalga oshriladi.
|
dastur
|
topshiriqlar
|
#include
using namespace std;
struct TStudent{
string name;
string numBook;
int course;
float rank;
};
int main() {
int n_students = 3;
TStudent* pS;
try {
pS = new struct TStudent[n_students];
}
catch (bad_alloc ba) {
cout << "Xotira ajratilmadi " << endl;
cout << ba.what() << endl;
return -1;
}
pS->name = "Aliyev Akbar";
pS->numBook = "965874123";
pS->rank = 3.93;
pS->course = 1;
(pS + 1)->name = "Bahromov Batir";
(pS + 1)->numBook = "20930032";
(pS + 1)->rank = 4.98;
pS[1].course = 3;
pS[2].name = "Qobilov Usmon";
pS[2].numBook ="l2l28983";
pS[2].rank = 4.32;
pS[2].course = 2;
cout << pS -> name.c_str() << endl;
cout << pS[1].rank << endl;
cout << (pS + 2) -> numBook.c_str() << endl;
delete[] pS;
system("pause");
return 0;
}
|
1. Dasturda nechta yangi tipdagi ob’ektlari yaratilgan.
Dasturda TStudent nomli struktura bo‘yicha 3 ta obyekt yaratilgan.
|
2. Dasturdagi barcha talabalar haqidagi ma’lumotni ekranga chiqaring.
Barcha talabalar haqida ma'lumotlar ekranga chiqarilgan:
Aliyev Akbar
3.93
20930032
|
3. Massiv o‘lchamini dinamik kattalashtirish uchun fragment yozing, masalan 5 talaba uchun.
Massiv o‘lchamini dinamik kattalashtirish uchun, n_students o‘zgaruvchi orqali dinamik ravishda yaratish mumkin. Misol uchun:
cout << "Enter the number of students: ";
cin >> n_students;
try {
pS = new struct TStudent[n_students];
}
catch (bad_alloc ba) {
cout << "Xotira ajratilmadi " << endl;
cout << ba.what() << endl;
return -1;
}
|
4. Yangi tip uchun get() va set() funksiyalarini yarating.
Yangi tip uchun get() va set() funksiyalari quyidagi ko‘rinishda yaratilishi mumkin:
struct TStudent {
string name;
string numBook;
int course;
float rank;
void set_name(string n) {
name = n;
}
string get_name() {
return name;
}
void set_numBook(string nb) {
numBook = nb;
}
string get_numBook() {
return numBook;
}
void set_course(int c) {
course = c;
}
int get_course() {
return course;
}
void set_rank(float r) {
rank = r;
}
float get_rank() {
return rank;
}
};
_____________________________
|
5. Massivdan berilgan qiymatni izlash dastur fragmentini yozing.
Massivdan berilgan qiymatni izlash uchun, for tsikli orqali massiv elementlari tekshiriladi. Misol uchun, agar numBook raqami 20930032 bo‘lsa, shu talabaning ma’lumotlari ekranga chiqariladi:
string numBook_to_find = "20930032";
for(int i = 0; i < n_students; i++) {
if(pS[i].numBook == numBook_to_find) {
cout << "Name: " << pS[i].name.c_str() << endl;
cout << "Number of book: " << pS[i].numBook.c_str() << endl;
cout << "Course: " << pS[i].course << endl;
cout << "Rank: " << pS[i].rank << endl;
}
}
|
6. Dasturda jami bo‘lib, necha o‘zgartirish kiritildi. Dasturda jami 5 ta o‘zgartirish kiritilgan. Birinchi o‘zgartirish, new operatoridan foydalanib, dinamik ravishda obyekt yaratish. Ikkinchi o‘zgartirish, c_str() funksiyasi yozilgan, bu esa string tipidagi ma'lumotlarni char tipiga aylantirish uchun kerak. Uchinchi o‘zgartirish, -> operatori yozilgan, bu esa o‘zgaruvchiga o‘xshash obyektdan ma'lumotlarni olish uchun kerak. To‘rtinchi o‘zgartirish, [] operatori yozilgan, bu esa massiv elementlariga murojat uchun kerak. Beshinchi o‘zgartirish, delete[] operatori yozilgan, bu esa dinamik ravishda yaratilgan obyektlarni bo‘shatish uchun kerak.
7. Shu dasturning analogini yaratish sizga mustaqil vazifadir.
#include
using namespace std;
struct TStudent{
string name;
string numBook;
int course;
float rank;
};
int main() {
int n_students = 3;
TStudent* pS = nullptr;
try {
pS = new TStudent[n_students];
}
catch (bad_alloc& ba) {
cout << "Xotira ajratilmadi " << endl;
cout << ba.what() << endl;
return -1;
}
pS->name = "Aliyev Akbar";
pS->numBook = "965874123";
pS->rank = 3.93;
pS->course = 1;
(pS + 1)->name = "Bahromov Batir";
(pS + 1)->numBook = "20930032";
(pS + 1)->rank = 4.98;
pS[1].course = 3;
pS[2].name = "Qobilov Usmon";
pS[2].numBook ="l2l28983";
pS[2].rank = 4.32;
pS[2].course = 2;
cout << pS->name << endl;
cout << pS[1].rank << endl;
cout << (pS + 2)->numBook << endl;
delete[] pS;
system("pause");
return 0;
}
|