Ajdod class va uning maydonlari
Sana 23.07.2022 Hajmi 0,71 Mb. #844328
Bog'liq
Dasturlash 5- laboratoriya
Jo`rayev Bekzod
Dasturlash 5-laboratoriya ishi
1 - mashq.
№
Ajdod class va uning maydonlari
Voris class (ajdod class maydonlaridan foydalangan holda)
Obektlar massivini yarating.
Murakkab saralash algortimlaridan foydalanib saralash metodlari yarating.
(saralang o’sib borish tartibida yoki kamayib borish tartibida va alifbo tartibida)
11.
class tovar:
nomi,
narxi,
chiqarilgan yili
class dori_darmonlar;
saqlanish_muddati
Array konteyneridan foydalanib ishchilar dori darmonlar ro’yxatida chiqarilgan yili bo’yicha saralang.
#include
using namespace std ;
class product {
public:
string name;
float price{};
int produced_year{};
};
class Drugs : public product {
public:
int keepingLimit{};
public:
void set();
int getKeepingLimit() const;
string getName();
int getProducedYear();
float getPrice();
string toString();
};
void Drugs::set() {
cout << "Item Name = ";
cin >> name;
cout << "Price = ";
cin >> price;
cout << "Produced year = ";
cin >> produced_year;
cout << "Keeping limit by years = ";
cin >> keepingLimit;
}
string Drugs::getName() {
return this->name;
}
int Drugs::getProducedYear() {
return this->produced_year;
}
float Drugs::getPrice() {
return this->price;
}
int Drugs::getKeepingLimit() const {
return this->keepingLimit;
}
string Drugs::toString() {
return "{ nomi : " + this->getName() + ", narxi : " + to_string(this->getPrice()) +
", ishlab chiqarilgan yili : " + to_string(this->getProducedYear()) +
", saqlash muddati : " + to_string(this->getKeepingLimit()) + " }";
}
void sortingByProducedYear(Drugs drugs[], int length) {
int swapCount = -1, keptSwapCount = 0;
while (swapCount != keptSwapCount) {
swapCount = keptSwapCount;
for (int index = 0; index < length; ++index)
if (drugs[index].getProducedYear() - drugs[index + 1].getProducedYear() < 0) {
Drugs temp = drugs[index];
drugs[index] = drugs[index + 1];
drugs[index + 1] = temp;
swapCount++;
}
}
}
void print(Drugs drugs[], int length) {
for (int i = 0; i < length; i++)
cout << i + 1 << ". dori - " << drugs[i].toString() << endl;
}
int main() {
int n;
cout << "dorilar sonini kiritiong - ";
cin >> n;
Drugs drugs[n];
for (int i = 0; i < n; i++) {
cout << i + 1 << ". Drug fields " << endl;
drugs[i].set();
}
sortingByProducedYear(drugs, n);
print(drugs, n);
}
2 - mashq.
11.
“Kinofilmlar” classini yarating.
Maydonlari: Nomlanishi, davomiyligi (minut da), aktyorlar soni.
Deque konteyneridan foydalanib davomiyligi bo’yicha saralang.
(kamida 29 xonali sonlar ketma-ketligidan iborat bo‘lsin).
#include
#include
#include
using namespace std;
class Films {
private:
string name;
string duration ;
int amountOFActors{};
public:
void setFilm();
string getName() const;
string getDuration() const;
int getAmountOfActors() const;
string toString() const;
};
void Films::setFilm() {
cout << "Name = ";
cin >> this->name;
cout << "Duration (in Minutes) = ";
cin >> this->duration;
cout << "Amount of played Actors = ";
cin >> this->amountOFActors;
}
string Films::getName() const {
return name;
}
string Films::getDuration() const {
return this->duration;
}
int Films::getAmountOfActors() const {
return this->amountOFActors;
}
string Films::toString() const {
return "{ nomi : " + this->getName() + ", davomiyligi : " + this->getDuration() +
", qatnashgan aktyorlar soni : " + to_string(this->getAmountOfActors()) + " }";
}
bool comparing(const Films& film1, const Films& film2) {
if (film1.getDuration().length() > film2.getDuration().length())
return true;
else if (film1.getDuration().length() < film2.getDuration().length())
return false;
for (int i = 0; i < film1.getDuration().length(); ++i)
if ((int)film1.getDuration()[i] > (int)film2.getDuration()[i])
return true;
else if((int)film1.getDuration()[i] < (int)film2.getDuration()[i])
return false;
return false;
}
void print(deque films) {
deque::iterator it;
for (it = films.begin(); it != films.end(); ++it)
cout << it->toString() << endl;
}
int main() {
int n;
cout << "Please enter amount of movies - ";
cin >> n;
deque films;
for (int i = 0; i < n; ++i) {
Films aFilm;
cout << i+1 << " - kino " << endl;
aFilm.setFilm();
films.push_back(aFilm);
}
sort(films.begin(), films.end(), comparing);
deque::iterator it;
print(films);
return 0;
}
Do'stlaringiz bilan baham: