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 Сорокин Матвей #37

Open
wants to merge 6 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
47 changes: 46 additions & 1 deletion animals/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@

using namespace std;

Animal::Animal(float weight_ ,int numof_limbs_) {
weight = weight_;
numof_limbs = numof_limbs_;
}
Insects::Insects(float weight_ ,int numof_limbs_, int numof_wings_) : Animal(weight_ ,numof_limbs_){
numof_wings = numof_wings_;
}
Beatle::Beatle(float weight_,int numof_limbs_,int numof_wings_,int numof_mustache_) : Insects(weight_ ,numof_limbs_,numof_wings_){
numof_mustache = numof_mustache_;
}
ButterFly::ButterFly(float weight_,int numof_limbs_,int numof_wings_,string colorof_wings_) : Insects(weight_ ,numof_limbs_,numof_wings_){
colorof_wings = colorof_wings_;
}

Animal::~Animal() {}

string Animal::about() const {
stringstream ss;
ss << "Weight = " << weight << '\n' << "Number of Limbs = " << numof_limbs;
return ss.str();
}

string Insects::about() const {
stringstream ss;
ss << Animal::about() << '\n' << "Number of wings = " << numof_wings;
return ss.str();
}

string Beatle::about() const {
stringstream ss;
ss << Insects::about() << '\n' << "Number of mustache = " << numof_mustache;
return ss.str();
}

string ButterFly::about() const {
stringstream ss;
ss << Insects::about() << '\n' << "Color of wings = " << colorof_wings;
return ss.str();
}

int main() {
Beatle dungbeetle(0.002,6,4,2);
ButterFly cupidinidae(0.000001,6,4,"Blue");
cout << "Cupidinidae:" << '\n' << cupidinidae.about();
cout << '\n';
cout << "Dungbeatle:" << '\n' <<dungbeetle;
return 0;
}
}
81 changes: 78 additions & 3 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,93 @@
#pragma once

#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;

class Animal {
public:
private:
float weight; // kg
int numof_limbs;
protected:
Animal(float weight_ ,int numof_limbs_);
virtual ~Animal();
public:
float getWeight() const { return weight; }
void setWeight(float newValue) { weight = newValue; }
int getNumofLimbs() const { return numof_limbs; }
void setNumofLimbs(int newValue) { numof_limbs = newValue; }
virtual std::string about() const;
};

class Mammal : public Animal {
class Insects : public Animal{
private:
int numof_wings;
protected:
Insects(float weight_ ,int numof_limbs_, int numof_wings_);
public:
int getNumofWings() const { return numof_wings; }
void setNumofWings(int newValue) { numof_wings = newValue; }
virtual std::string about() const;

};

class Reptilies : public Animal{
private:
bool isShell;
float tongueLength; //meters
};

class Mammal : public Animal {
private:
float pregnancyDuration; // days
int numof_niples;
};

class Beatle : public Insects{
private:
int numof_mustache;
public:
int getNumofMustache() const { return numof_mustache; }
void setNumofMustache(int newValue) { numof_mustache = newValue; }
virtual std::string about() const;
Beatle(float weight_,int numof_limbs_,int numof_wings_,int numof_mustache_);
};

class Cat : public Mammal {
class ButterFly : public Insects{
private:
string colorof_wings;
public:
string getColorofWings() const { return colorof_wings; }
void setColorofWings(string newValue) { colorof_wings = newValue; }
virtual std::string about() const;
ButterFly(float weight_,int numof_limbs_,int numof_wings_,string colorof_wings_);
};

class Snake : public Reptilies{
private:
bool isPoisonous;
};

class Turtle : public Reptilies{
private:
float shellStrength; //kg
};

class Cat : public Mammal{
private:
float vibrissaLength; // meters
};

class Dog : public Mammal{
private:
float dobermanLength; // meters
};

inline std::ostream& operator <<(std::ostream& os, const Beatle& beatle) {
return os << beatle.about();
}

inline std::ostream& operator <<(std::ostream& os, const ButterFly& bttfly) {
return os << bttfly.about();
}
124 changes: 114 additions & 10 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,148 @@ using namespace std;

bool Object::isConnectedTo(const Object& other) const
{
size_t n = (this)->getPoleCount();
for(size_t i = 0;i < n;i++){
if(((this->getPole(i+1))->connectedObject) == &other) return true;
}
// TODO
return false;
}

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
// TODO
return false;
if(this->isConnectedTo(other)) return false;
else {
//disconnect to this
Object* nowConnectThis = (this->getPole(poleName))->connectedObject;
string nwCnctThs = (this->getPole(poleName))->connectedObjectPole;
if (nowConnectThis != nullptr) {
(nowConnectThis->getPole(nwCnctThs))->connectedObject = nullptr;
(nowConnectThis->getPole(nwCnctThs))->connectedObjectPole = "";
(this->getPole(poleName))->connectedObject = const_cast<Object*>(&other);
(this->getPole(poleName))->connectedObjectPole = otherPoleName;
} //connect to this
else {
(this->getPole(poleName))->connectedObject = const_cast<Object*>(&other);
(this->getPole(poleName))->connectedObjectPole = otherPoleName;
}

//disconnect to other
Object* nowConnectOther = (other.getPole(otherPoleName))->connectedObject;
string nwCnctOthr = (other.getPole(otherPoleName))->connectedObjectPole;
Pole* othr = const_cast<Pole*>(other.getPole(otherPoleName));
if (nowConnectOther != nullptr){
(nowConnectOther->getPole(nwCnctOthr))->connectedObject = nullptr;
(nowConnectOther->getPole(nwCnctOthr))->connectedObjectPole = "";
othr->connectedObject = this;
othr->connectedObjectPole = poleName;
} //connect to other
else{
othr->connectedObject = this;
othr->connectedObjectPole = poleName;
}

return true;
}
}

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

Lamp::Lamp(const std::string& name)
:Object(name)
, l1("L1")
, l2("L2")
{
}

Generator::Generator(const std::string& name)
:Object(name)
, p("Plus")
, m("Minus")
, g("Ground")
{
}

const Pole* Switch::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
if (name == s1.name)
return &s1;
if (name == s2.name)
return &s2;
return nullptr;
}
const Pole* Lamp::getPole(const string& name) const
{
if (name == l1.name)
return &l1;
if (name == l2.name)
return &l2;
return nullptr;
}
const Pole* Generator::getPole(const string& name) const
{
if (name == p.name)
return &p;
if (name == m.name)
return &m;
if (name == g.name)
return &g;
return nullptr;
}

const Pole* Switch::getPole(size_t idx) const
{
if (idx == 1)
return &s1;
if (idx == 2)
return &s2;
// TODO
return nullptr;
}
const Pole* Lamp::getPole(size_t idx) const
{
if (idx == 1)
return &l1;
if (idx == 2)
return &l2;
// TODO
return nullptr;
}
const Pole* Generator::getPole(size_t idx) const
{
if (idx == 1)
return &p;
if (idx == 2)
return &m;
if (idx == 3)
return &g;
return nullptr;
}



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

Switch sw0;
Lamp l;
Generator g;
sw0.connect("S1", l,"L1");
l.connect("L2",g,"Plus");
g.connect("Minus",sw0,"S2");
if(sw0.isConnectedTo(g) && sw0.isConnectedTo(l)) cout << "Switch is connected\n";
else cout << "Switch is not connected";
if(l.isConnectedTo(g) && l.isConnectedTo(sw0)) cout << "Lamp is connected\n";
else cout << "Lamp is not connected";
if(g.isConnectedTo(l) && g.isConnectedTo(sw0)) cout << "Generator is connected\n";
else cout << "Generator is not connected";
// TODO: создать цепь из генератора, выключателя и светильника

return 0;
Expand Down
31 changes: 27 additions & 4 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Object;
/// Полюс устройства.
/// К одному полюсу может быть подключен один любой другой полюс этого же
/// устройства, или же любой полюс любого другого устройства.
///
///
/// У каждого полюса есть своё название, которое получает значение по умолчанию
/// при создании устройства, но может быть изменено позднее.
/// </summary>
Expand Down Expand Up @@ -50,8 +50,8 @@ class Object {
/// </summary>
/// <param name="idx">Индекс полюса, от <c>0</c> до значения, возвращаемого <see cref="getPoleCount()"/>.</param>
/// <returns>Полюс с указанным индексом, или <c>nullptr</c>, если такой полюс не существует.</returns>
Pole* getPole(size_t idx) { /* TODO */ return nullptr; }

//Pole* getPole(size_t idx) { /* TODO */ return nullptr; }
Pole* getPole(size_t idx) {return const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx));}
/// <summary>
/// Возвращает полюс по внутреннему индексу устройства.
/// </summary>
Expand Down Expand Up @@ -109,7 +109,7 @@ class Object {
/// </summary>
class Switch : public Object {
public:
Pole a1, a2;
Pole s1, s2;

Switch(const std::string& name = "");

Expand All @@ -120,7 +120,30 @@ class Switch : public Object {
protected:
virtual const Pole* getPole(size_t idx) const;
};
class Lamp : public Object {
public:
Pole l1, l2;

Lamp(const std::string& name = "");

virtual size_t getPoleCount() const { return 2; }

virtual const Pole* getPole(const std::string& name) const;
protected:
virtual const Pole* getPole(size_t idx) const;
};
// TODO: класс светильника с двумя полюсами
class Generator : public Object {
public:
Pole p,m,g;

Generator(const std::string& name = "");

virtual size_t getPoleCount() const { return 3; }

virtual const Pole* getPole(const std::string& name) const;

protected:
virtual const Pole* getPole(size_t idx) const;
};
// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
Loading