Untitled
unknown
plain_text
a year ago
7.5 kB
5
Indexable
//#include <iostream> //#include <string> //#include <vector> //using namespace std; // //class Animal //{ //protected: // string name; // vector<int> stages; //public: // Animal(string name, vector<int> stages) : name(name), stages(stages) // { // } // virtual bool isValid() = 0; // // int getTotalDevlopmentTime() { // int total = 0; // for (int stage : stages) // { // total += stage; // } // return total; // } // // string getName() // { // return name; // } //}; // //class Muoi : public Animal //{ //public: // Muoi(vector<int>stages) : Animal("Muoi", stages){} // // bool isValid() override // { // return stages.size() == 4 && stages[0] >= 1 && stages[0] <= 3 && stages[1] >= 4 && stages[1] <= 10 && stages[2] >= 2 && stages[2] <= 3 && stages[3] >= 5 && stages[3] <= 8; // } //}; // // //class Ech : public Animal { //public: // Ech(vector<int> stages) : Animal("Ech", stages) {} // // bool isValid() override { // return stages.size() == 4 && stages[0] >= 7 && stages[0] <= 21 && stages[1] >= 28 && stages[1] <= 63 && stages[2] >= 7 && stages[2] <= 28 && stages[3] >= 730 && stages[3] <= 1460; // } //}; // //class Buom : public Animal { //public: // Buom(vector<int> stages) : Animal("Buom", stages) {} // // bool isValid() override { // return stages.size() == 4 && stages[0] >= 3 && stages[0] <= 8 && stages[1] >= 15 && stages[1] <= 16 && stages[2] >= 10 && stages[2] <= 10 && stages[3] >= 2 && stages[3] <= 3; // } //}; // //bool isBiodiverse(const vector<Animal*>& animals) { // bool coMuoi = false, coEch = false, coBuom = false; // for (auto animal : animals) { // if (animal->getName() == "Muoi") coMuoi = true; // else if (animal->getName() == "Ech") coEch = true; // else if (animal->getName() == "Buom") coBuom = true; // } // return coMuoi && coEch && coBuom; //} //void inputAnimals(vector<Animal*>& animals, int N) { // for (int i = 0; i < N; ++i) { // string type; // vector<int> stages(4); // cout << "Enter type and stages for animal " << i + 1 << " (type stages): "; // cin >> type; // for (int j = 0; j < 4; ++j) { // cin >> stages[j]; // } // // if (type == "Muoi") { // animals.push_back(new Muoi(stages)); // } // else if (type == "Ech") { // animals.push_back(new Ech(stages)); // } // else if (type == "Buom") { // animals.push_back(new Buom(stages)); // } // } //} // //int main() { // int N; // cout << "Enter number of animals: "; // cin >> N; // // vector<Animal*> animals; // // inputAnimals(animals, N); // // cout << "Animals with invalid development stages:" << endl; // for (int i = 0; i < animals.size(); ++i) { // if (!animals[i]->isValid()) { // cout << "Animal at position " << i + 1 << " is invalid." << endl; // } // } // // if (isBiodiverse(animals)) { // cout << "The ecosystem is biodiverse." << endl; // } // else { // cout << "The ecosystem is not biodiverse." << endl; // } // // // Cleanup // for (auto animal : animals) { // delete animal; // } // // return 0; //} //Câu 2 // //#include <iostream> //using namespace std; // //class Money { //private: // int iEuro; // int iCent; //public: // Money(int euro = 0, int cent = 0) : iEuro(euro), iCent(cent) { // quydoi(); // } // void quydoi() // { // if (iCent > 100) // { // iEuro += iCent / 100; // iCent %= 100; // } // } // friend istream& operator>>(std::istream& input, Money& money) // { // input >> money.iEuro >> money.iCent; // money.quydoi(); // return input; // } // // // Overloading << operator for output // friend ostream& operator<<(ostream& out, const Money& m) { // out << m.iEuro << " euros " << m.iCent << " cents"; // return out; // } // // // Overloading ++ operator (prefix) // Money& operator++() { // ++iCent; // quydoi(); // return *this; // } // // // Overloading ++ operator (postfix) // Money operator++(int) { // Money temp = *this; // ++(*this); // return temp; // } // // // Friend function to overload prefix ++ // /*friend Money& operator++(Money& m) { // ++m.iCent; // m.quydoi(); // return m; // }*/ //}; //int main() { // Money m; // cin >> m; // cout << "Current money: " << m << endl; // m++; // cout << "After incrementing (postfix ++): " << m << endl; // ++m; // cout << "After incrementing (prefix ++): " << m << endl; // return 0; //} //Câu 3 #include <iostream> #include <vector> using namespace std; class Dongvat { protected: string name; vector<int> stages; public: Dongvat(string name, vector<int> stages) : name(name), stages(stages) {} virtual bool isValid() = 0; int getTotalDevelopmentTime() { int total = 0; for (int stage : stages) { total += stage; } } string getName() { return name; } }; class Muoi : public Dongvat { public: Muoi(vector<int> stages) : Dongvat("Muoi", stages) {} bool isValid() override { return stages.size() == 4 && stages[0] >= 1 && stages[0] <= 3 && stages[1] >= 4 && stages[1] <= 10 && stages[2] >= 2 && stages[2] <= 3 && stages[3] >= 5 && stages[3] <= 8; } }; class Ech : public Dongvat { public: Ech(vector<int> stages) : Dongvat("Ech", stages) {} bool isValid() override { return stages.size() == 4 && stages[0] >= 7 && stages[0] <= 21 && stages[1] >= 28 && stages[1] <= 63 && stages[2] >= 7 && stages[2] <= 28 && stages[3] >= 730 && stages[3] <= 1460; } }; class Buom : public Dongvat { public: Buom(vector <int> stages) : Dongvat("Buom", stages) {} bool isValid() override { return stages.size() == 4 && stages[0] >= 3 && stages[0] <= 8 && stages[1] >= 15 && stages[1] <= 16 && stages[2] >= 10 && stages[2] <= 10 && stages[3] >= 2 && stages[3] <= 3; } }; class Ecosystem { private: vector<Dongvat*> dongvats; public: void inputAnimals(int n) { for (int i = 0; i < n; i++) { string type; vector<int> stages(4); cout << "Nhap loai dong vat: "; cin >> type; cout << "Nhap cac giai doan: "; for (int j = 0; j < 4; j++) { cin >> stages[j]; } if (type == "Muoi") { dongvats.push_back(new Muoi(stages)); } if (type == "Ech") { dongvats.push_back(new Ech(stages)); } if (type == "Buom") { dongvats.push_back(new Buom(stages)); } } } bool Biodiverse() { bool coMuoi = false, coEch = false, coBuom = false; for (auto d : dongvats) { if (d->getName() == "Muoi") coMuoi = true; else if (d->getName() == "Ech") coEch = true; else if (d->getName() == "Buom") coBuom = true; } return coMuoi && coEch && coBuom; } void checkInvalidDongvat() { cout << "Dong vat nay khong hop le" << endl; for (int i = 0; i < dongvats.size(); i++) { if (!dongvats[i]->isValid()) { cout << "Dong vat o vi tri" << i + 1 << "khong hop le." << endl; } } } }; int main() { int n; cin >> n; Ecosystem ecosystem; ecosystem.inputAnimals(n); ecosystem.checkInvalidDongvat(); if (ecosystem.Biodiverse()) { cout << "Khu sinh thai nay da dang" << endl; } else cout << "Khu sinh thai nay khong da dang" << endl; return 0; }
Editor is loading...
Leave a Comment