Skip to content
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
11 changes: 8 additions & 3 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "animal.h"

using namespace std;

int main() {
Shark example1(240, 40.2, 32.7);
example1.setFinSize(30.6);
example1.setWeight(200);
Bass example2(12.3, 15.12, 6.2);
Horse example3(82.9, 12, 50.5);
Cat example4(5.2, 8, 4.9);
cout << example1.getFinSize() << " " << example1.getWeight();
return 0;
}
};
86 changes: 82 additions & 4 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,96 @@
#pragma once

#include <iostream>
#include <sstream>
#include <string>

class Animal {
private:
float weight;
protected:
Animal() { weight = 0.0; }
Animal(float weight_) { weight = weight_; };
public:
float weight; // kg
float getWeight() const { return weight; }
void setWeight(float value) { weight = value; }
virtual std::string about() const { return (std::stringstream() << "Weight =" << weight).str(); }
};


class Mammal : public Animal {
private:
int pregnancyDuration;
protected:
Mammal() { pregnancyDuration = 12; }
Mammal(float weight_, int pregnancyDuration_) : Animal(weight_) { pregnancyDuration = pregnancyDuration_; };
public:
float pregnancyDuration; // days
int getPregnancyDuration() const { return pregnancyDuration; }
void setPregnancyDuration(int value) { pregnancyDuration = value; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "PregnancyDuration =" << pregnancyDuration).str(); }
};


class Cat : public Mammal {
private:
float vibrissaLength;
protected:
public:
float vibrissaLength; // meters
Cat() { vibrissaLength = 0; };
Cat(float weight_, int pregnancyDuration_, float vibrissaLength_) : Mammal(weight_, pregnancyDuration_) { vibrissaLength = vibrissaLength_; };
float getVibrissaLength() const { return vibrissaLength; }
void setVibrissaLength(float vibrissaLength_) { vibrissaLength = vibrissaLength_; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "VibrissaLength =" << vibrissaLength).str(); }
};


class Horse : public Mammal {
private:
float runningSpeed;

protected:
public:
Horse() { runningSpeed = 0; };
Horse(float weight_, int pregnancyDuration_, float runningSpeed_) : Mammal(weight_, pregnancyDuration_) { runningSpeed = runningSpeed_; };
float getRunningSpeed() const { return runningSpeed; }
void setRunningSpeed(float runningSpeed_) { runningSpeed = runningSpeed_; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "RunningSpeed =" << runningSpeed).str(); }
};


class Fish : public Animal {
private:
float size;
protected:
Fish() { size = 0.0; }
Fish(float weight_, float size_) : Animal(weight_) { size = size_; };
public:
float getSize() const { return size; }
void setSize(float size_) { size = size_; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "Size =" << size).str(); }
};


class Bass : public Fish {
private:
float gillSize;
protected:
public:
Bass() { gillSize = 0; };
Bass(float weight_, float size_, float gillSize_) : Fish(weight_, size_) { gillSize = gillSize_; };
float getGillsSize() const { return gillSize; }
void setGillsSize(float value) { gillSize = value; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Fish::about() << ", " << "GillSize =" << gillSize).str(); }
};


class Shark : public Fish {
private:
float finSize;
protected:
public:
Shark() { finSize = 0; };
Shark(float weight_, float size_, float finSize_) : Fish(weight_, size_) { finSize = finSize_; };
float getFinSize() const { return finSize; }
void setFinSize(float finSize_) { finSize = finSize_; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Fish::about() << ", " << "FinSize =" << finSize).str(); }
};

58 changes: 17 additions & 41 deletions electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,29 @@

using namespace std;

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

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

bool Object::disconnect(const std::string& poleName)
{
// 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: создать цепь из генератора, выключателя и светильника

Switch swtch;
Light lght;
Generator gnr;
gnr.connect("A1", swtch, "A2");
gnr.connect("A2", lght, "A1");
swtch.connect("A1", lght, "A2");
cout << "Generator is" << (gnr.isConnectedTo(lght) ? " " : "not ") << "connected light" << endl;
cout << "Light is" << (lght.isConnectedTo(gnr) ? " " : "not ") << "connected to generator" << endl;
cout << "Generator is" << (gnr.isConnectedTo(swtch) ? " " : "not ") << "connected to switch" << endl;
cout << "Switch is" << (swtch.isConnectedTo(gnr) ? " " : "not ") << "connected to generator" << endl;
cout << "Switch is" << (swtch.isConnectedTo(lght) ? " " : "not ") << "connected to light" << endl;
cout << "Light is" << (lght.isConnectedTo(swtch) ? " " : "not ") << "connected to switch" << endl;

gnr.disconnect("A2");
cout << "Generator disconnected" << endl;
cout << "Generator is" << (gnr.isConnectedTo(lght) ? " " : " not ") << "connected light" << endl;
cout << "Light is" << (lght.isConnectedTo(gnr) ? " " : " not ") << "connected to generator" << endl;
return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не хватает демонстрации текущего состояния с выводом на экран.

}
139 changes: 125 additions & 14 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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 const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx)); }

/// <summary>
/// Возвращает полюс по внутреннему индексу устройства.
Expand All @@ -61,9 +61,8 @@ class Object {

public:
virtual ~Object() {}

const std::string& getName() const { return name; }
void getName(const std::string &newName) { name = newName; }
void getName(const std::string& newName) { name = newName; }

/// <summary>
/// Возвращает полюс по имени.
Expand All @@ -87,8 +86,16 @@ class Object {
/// </summary>
/// <param name="other">Устройство, наличие прямой связи с которым проверяется.</param>
/// <returns><c>true</c> если устройства связаны напрямую, <c>false</c> в противном случае.</returns>
bool isConnectedTo(const Object& other) const;

bool isConnectedTo(const Object& other) const {
for (auto i = 0; i < getPoleCount(); i++) {
for (auto j = 0; j < other.getPoleCount(); j++) {
if ((getPole(i)->connectedObjectPole == other.getPole(j)->name) && (getPole(i)->connectedObject == &other)) {
return true;
}
}
}
return false;
}
/// <summary>
/// Соединяет указанные полюса текущего и указанного устройства.
/// Если к этим полюсам было что-то подключено, то такая связь разрушается.
Expand All @@ -102,8 +109,18 @@ class Object {
/// В этом случае в качестве <paramref name="other"/> следует передать то же устройство,
/// для которого вызывается этот метод.
/// </remarks>
bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName);

bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName) {
Pole* pl = getPole(poleName);
if (poleName != otherPoleName) {
pl->connectedObject = &other;
pl->connectedObjectPole = otherPoleName;
Pole* other_pl = other.getPole(otherPoleName);
other_pl->connectedObjectPole = poleName;
other_pl->connectedObject = this;
return true;
}
return false;
}
/// <summary>
/// Отключает указанный полюс, если к нему что-либо подключено.
/// </summary>
Expand All @@ -112,26 +129,120 @@ class Object {
/// <remarks>
/// Вызов этого метода для полюса, если к нему ничего не подключено, не является ошибкой.
/// </remarks>
bool disconnect(const std::string& poleName);
bool disconnect(const std::string& poleName) {
Pole* pl = getPole(poleName);
Pole* other_pl = getPole(poleName)->connectedObject->getPole(getPole(poleName)->connectedObjectPole);
if (pl->connectedObject) {
other_pl->connectedObject = nullptr;
other_pl->connectedObjectPole = "";
pl->connectedObject = nullptr;
pl->connectedObjectPole = "";
return true;
}
return false;
}
};

/// <summary>
/// Простой выключатель с двумя полюсами.
/// </summary>
class Switch : public Object {
std::string name;
public:
Pole a1, a2;

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

size_t getPoleCount() const { return 2; }

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

protected:
const Pole* getPole(size_t idx) const {
if (idx == 0)
return &a1;
if (idx == 1)
return &a2;
return nullptr;
}
};


class Light : public Object {
std::string name;
public:
Pole a1, a2;
Light(const std::string& name = " ") : Object(name), a1("A1"), a2("A2") {}
size_t getPoleCount() const { return 2; }
const Pole* getPole(const std::string& name) const {
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}

Switch(const std::string& name = "");
protected:
const Pole* getPole(size_t idx) const {
if (idx == 0)
return &a1;
if (idx == 1)
return &a2;
}
};

virtual size_t getPoleCount() const { return 2; }

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

class Generator : public Object {
std::string name;
public:
Pole a1; //фаза
Pole a2; //нейтраль
Pole a3; //земля

Generator(const std::string& name = " ") : Object(name), a1("A1"), a2("A2"), a3("A3") {}

size_t getPoleCount() const { return 3; }

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

};

// TODO: класс светильника с двумя полюсами
return nullptr;
}
protected:
const Pole* getPole(size_t idx) const {
if (idx == 0)
return &a1;
if (idx == 1)
return &a2;
if (idx == 2)
return &a3;
return nullptr;
}

// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
};
Loading