Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Хорин Степан КМБО-03-21 #45

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 199 additions & 2 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,204 @@
#include "animal.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
class Animal
{
private:
int Age; //возраст в годах
float Weight; //вес в кг

public:

int getAge() const { return Age; }
void setAge(int ValueAge) { Age = ValueAge; }

float getWeight() const { return Weight; }
void setWeight(float ValueWeight) { Weight = ValueWeight; }

virtual string about() const { return (stringstream() << "Age = "<< Age <<", "<<"Weight = "<<Weight).str(); }

protected:

Animal() {};
Animal(int ValueAge, float ValueWeight)
{
setAge(ValueAge);
setWeight(ValueWeight);
}

};

class Mammal : public Animal
{
private:
int DurFeeding; //длительность вскармливания в неделях
string CoatColor; //цвет шерсти

public:

int getDurFeeding() const { return DurFeeding; }
void setDurFeeding(int ValueDurFeeding) { DurFeeding = ValueDurFeeding; }

string getCoatColor() const { return CoatColor; }
void setCoatColor(string Color) { CoatColor = Color; }

virtual string about() const { return (stringstream() << Animal::about() << ", " << "Duration of feeding = " << DurFeeding << ", " << "Coat color = " << CoatColor).str(); }

protected:

Mammal() {};
Mammal(int ValueAge, float ValueWeight, int ValueDurFeeding, string Color)
{
setAge(ValueAge);
setWeight(ValueWeight);
setDurFeeding(ValueDurFeeding);
setCoatColor(Color);
};

};


class Reptile : public Animal
{
private:
int NumEggs; //количество яиц в кладке
int DurMolting; //длительность линьки в днях, включая подготовку к ней

public:

int getNumEggs() const { return NumEggs; }
void setNumEggs(int ValueEggs) { NumEggs = ValueEggs; }

int getDurMolting() const { return DurMolting; }
void setDurMolting(int ValueMolting) { DurMolting = ValueMolting; }

virtual string about() const { return (stringstream() << Animal::about() << ", " << "Number of eggs = " << NumEggs << ", " << "Duration of molting = " << DurMolting).str(); }


protected:
Reptile() {};
Reptile(int ValueAge, float ValueWeight, int ValueEggs, int ValueMolting)
{
setAge(ValueAge);
setWeight(ValueWeight);
setNumEggs(ValueEggs);
setDurMolting(ValueMolting);
};


};


class Hedgehog : public Mammal
{
private:
int NumNeedles; //количество иголок

public:

int getNumNeedles() const { return NumNeedles; }
void setNumNeedles(int ValueNeedles) { NumNeedles = ValueNeedles; }

Hedgehog() {};
Hedgehog(int ValueAge, float ValueWeight, int ValueDurFeeding, string Color, int ValueNeedles)
{
setAge(ValueAge);
setWeight(ValueWeight);
setDurFeeding(ValueDurFeeding);
setCoatColor(Color);
setNumNeedles(ValueNeedles);
};

virtual string about() const { return (stringstream() << Mammal::about() << ", " << "Number of needles = " << NumNeedles).str(); }


};

class Giraffe : public Mammal
{
private:
float NeckLength; //длина шеи в метрах
public:

float getNeckLength() const { return NeckLength; }
void setNeckLength(float ValueLength) { NeckLength = ValueLength; }

Giraffe() {};
Giraffe(int ValueAge, float ValueWeight, int ValueDurFeeding, string Color, float ValueLength)
{
setAge(ValueAge);
setWeight(ValueWeight);
setDurFeeding(ValueDurFeeding);
setCoatColor(Color);
setNeckLength(ValueLength);
};

virtual string about() const { return (stringstream() << Mammal::about() << ", " << "Neck length = " << NeckLength).str(); }
};


class Cobra : public Reptile
{
private:
float Length; //длина в метрах
public:

float getLength() const { return Length; }
void setLength(float ValueLength) { Length = ValueLength; }

Cobra() {};
Cobra(int ValueAge, float ValueWeight, int ValueEggs, int ValueMolting, float ValueLength)
{
setAge(ValueAge);
setWeight(ValueWeight);
setNumEggs(ValueEggs);
setDurMolting(ValueMolting);
setLength(ValueLength);
};

virtual string about() const { return (stringstream() << Reptile::about() << ", " << "Length = " << Length).str(); }
};

class Crocodile : public Reptile
{
private:
int NumTeeth;
public:

int getNumTeeth() const { return NumTeeth; }
void setNumTeeth(int ValueTeeth) { NumTeeth = ValueTeeth; }

Crocodile() {};
Crocodile(int ValueAge, float ValueWeight, int ValueEggs, int ValueMolting, int ValueTeeth)
{
setAge(ValueAge);
setWeight(ValueWeight);
setNumEggs(ValueEggs);
setDurMolting(ValueMolting);
setNumTeeth(ValueTeeth);
};

virtual string about() const { return (stringstream() << Reptile::about() << ", " << "Number of teeth = " << NumTeeth).str(); }
};



int main()
{
Hedgehog a1(3, 0.8, 6, "gray", 4000);
cout << "Hedgehog: " << a1.about() << endl;

Giraffe a2(15, 700, 48, "yellow-brown", 1.9);
cout << "Giraffe: " << a2.about() << endl;

Cobra a3(17, 6, 20, 10, 3);
cout << "Cobra: " << a3.about() << endl;

Crocodile a4(55, 400, 10, 5, 67);
cout << "Crocodile : " << a4.about() << endl;

return 0;
}
164 changes: 115 additions & 49 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,115 @@
#include <iostream>
#include "electricity.h"

using namespace std;

bool Object::isConnectedTo(const Object& other) const
{
// TODO
return false;
}

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
// TODO
return false;
}

Switch::Switch(const std::string& name)
: Object(name)
, a1("A1")
, a2("A2")
{
}

const Pole* Switch::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}

const Pole* Switch::getPole(size_t idx) const
{
// TODO
return nullptr;
}

int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;

// TODO: создать цепь из генератора, выключателя и светильника

return 0;
}
#include <iostream>
#include "electricity.h"

using namespace std;

bool Object::isConnectedTo(const Object& other) const
{
for (int i=1;i<=getPoleCount();i++){
for (int j=1;j<=other.getPoleCount();j++){
if (getPole(i)->connectedObjectPole==other.getPole(j)->name && getPole(i)->name!=other.getPole(j)->name)
return true;
}
}
return false;
}

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
if (poleName!=otherPoleName){
getPole(poleName)->connectedObject=const_cast<Object*>(&other);
getPole(poleName)->connectedObjectPole=otherPoleName;
getPole(otherPoleName)->connectedObjectPole=poleName;
getPole(otherPoleName)->connectedObject=const_cast<Object*>(this);
return true;
}
return false;
}

Switch::Switch(const std::string& name)
: Object(name)
, a1("A1")
, a2("A2")
{}
Light::Light(const std::string & name)
: Object(name)
, a1("A1")
, a2("A2")
{}
Generator::Generator(const std::string & name)
: Object(name)
, a1("A1") //Faza ~ фаза
, a2("A2") //Neytral ~ нейтраль
, a3("A3") //Zemly ~ земля
{}

const Pole* Switch::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}

const Pole* Switch::getPole(size_t idx) const
{
if (idx==1)
return &a1;
if (idx==2)
return &a2;
return nullptr;
}
const Pole* Light::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}

const Pole* Light::getPole(size_t idx) const
{
if (idx==1)
return &a1;
if (idx==2)
return &a2;
return nullptr;
}
const Pole* Generator::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
if (name == a3.name)
return &a3;
return nullptr;
}

const Pole* Generator::getPole(size_t idx) const
{
if (idx==1)
return &a1;
if (idx==2)
return &a2;
if (idx==3)
return &a3;
return nullptr;
}
int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
//создать цепь из генератора, выключателя и светильника
Switch sw1;
Light lamp;
Generator gen;
gen.connect("A2",lamp,"A1"); // Нейтраль с 1ым полюсом лампы
gen.connect("A1",lamp,"A2"); // Фаза со 2ым полюсаом лампы
gen.connect("A2",sw1,"A1"); // Нейтраль с 1ым полюсом переключателя
gen.connect("A3",sw1,"A2"); //Земля со 2ым полюсом переключателя
return 0;
}
Loading