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 Муравьева #53

Open
wants to merge 3 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
13 changes: 12 additions & 1 deletion animals/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@
using namespace std;

int main() {
Cat kity(10.5, 9, 5.5);
kity.setWeight(9);
int weigth = kity.getWeight();
Shark sharky(550, 40, 350);
sharky.setJawSize(200);
sharky.setSize(700);
Giraffe giraffy (300.56, 15, 1500);
giraffy.setpregnancyDuration(10);
cout << sharky.getJawSize() << "\t" << sharky.getSize();
return 0;
}
};


92 changes: 86 additions & 6 deletions animals/animal.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,98 @@
#pragma once

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

class Animal {
class Animal
{
public:
float weight; // kg
float getWeight() const { return weight; }
void setWeight(float weight) { this->weight = weight; }
virtual std::string about() const { return (std::stringstream() << "weight =" << weight).str(); }
private:
float weight;
protected:
Animal() //��� ����������� �� ���������
{
weight = 0.;
}
Animal(float weight)
{
this->weight = weight;
}
};

class Mammal : public Animal {
class Mammal : public Animal
{
private:
int pregnancyDuration;
public:
float pregnancyDuration; // days
int getPregnancyDuration() const { return pregnancyDuration; }
void setpregnancyDuration(int period) { pregnancyDuration = period; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "pregnancyDuration =" << pregnancyDuration).str(); }
protected:
Mammal() {
pregnancyDuration = 0;
}
Mammal(float weight, int period) :Animal(weight) { pregnancyDuration = period; }


};

class Cat : public Mammal
{
private:
float vibrissaLength;
public:
float getVibrissaLength() const { return vibrissaLength; }
void setvibrissaLength(int length) { vibrissaLength = length; }
Cat(float weight, int period, float length) :Mammal(weight, period) { vibrissaLength = length; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "vibrasaLength =" << vibrissaLength).str(); }
Cat() {
vibrissaLength = 0;
}
};

class Cat : public Mammal {
class Giraffe : public Mammal
{
private:
int height;
public:
float vibrissaLength; // meters
int getHeight() const { return height; }
void setHeight(int height) { this->height = height; }
Giraffe(float weight, int period, int height) :Mammal(weight, period) { setHeight(height); }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Mammal::about() << ", " << "heigth = " << height).str(); }
Giraffe() { height = 0; }
};

class Fish : public Animal
{
private:
int size;
public:
int getSize() const { return size; }
void setSize(int size) { this->size = size; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << "size =" << size).str(); }
protected:
Fish(float weight, int size) :Animal(weight) { setSize(size); }
Fish() {
size = 0;
}

};

class Shark : public Fish
{
private:
int jawSize;// ������ �������
public:
int getJawSize() const { return jawSize; }
void setJawSize(int size) { jawSize = size; }
Shark(float weight, int size, int jawSize) :Fish(weight, size) {setJawSize(jawSize);}
Shark() { jawSize = 0; }
virtual std::string about() const { return (std::stringstream() << Animal::about() << ", " << Fish::about() << ", " << "jawSize =" << jawSize).str();}

};

86 changes: 85 additions & 1 deletion electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@ using namespace std;
bool Object::isConnectedTo(const Object& other) const
{
// TODO
for (size_t i = 0; i < getPoleCount(); i++)
{
if (getPole(i)->connectedObject == &other) return true;
}

return false;
}

bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
// TODO
return false;
if (getPole(poleName) == nullptr || getPole(poleName)->name == otherPoleName) return false;
getPole(poleName)->connectedObject = &other;
getPole(poleName)->connectedObjectPole = otherPoleName;
return true;
}



bool Object::disconnect(const std::string& poleName)
{
getPole(poleName)->connectedObject = nullptr;
getPole(poleName)->connectedObjectPole = nullptr;

return true;
}
Switch::Switch(const std::string& name)
: Object(name)
, a1("A1")
Expand All @@ -34,6 +51,62 @@ const Pole* Switch::getPole(const string& name) const
const Pole* Switch::getPole(size_t idx) const
{
// TODO
if (idx == 0) {
return &a1;
}
else if (idx == 1) {
return &a2;
}
else {
return nullptr;
}
}

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

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

const Pole* Generator::getPole(const string& name) const
{
if (name == p.name) //phase
return &p;
if (name == n.name) //neutral
return &n;
if (name == e.name) //earth
return &e;
return nullptr;
}

const Pole* Generator::getPole(size_t idx) const
{
if (idx == 0) {
return &p;
}
else if (idx == 1) {
return &n;
}
else if (idx == 2) {
return &e;
}

return nullptr;
}

Expand All @@ -43,6 +116,17 @@ int main()
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;

Generator g; Lamp l; Switch s;

g.connect("Phase", s, "A1");
s.connect("A2", l, "A1");
l.connect("A2", g, "Neutral");


cout << "is " << (g.isConnectedTo(s) ? "" : "not ") << "connected" << endl;
cout << "is " << (s.isConnectedTo(l) ? "" : "not ") << "connected" << endl;
cout << "is " << (l.isConnectedTo(g) ? "" : "not ") << "connected" << endl;

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

return 0;
Expand Down
33 changes: 28 additions & 5 deletions electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Pole {
/// два разных полюса.
/// Значение <c>nullptr</c> означает, что к данному полюсу ничего не подключено.
/// </summary>
Object* connectedObject;
const Object* connectedObject ;

/// <summary>
/// Полюс устройства, к которому подключен данный полюс.
Expand All @@ -50,8 +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) { const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx)); }
/// <summary>
/// Возвращает полюс по внутреннему индексу устройства.
/// </summary>
Expand All @@ -63,7 +62,7 @@ class Object {
virtual ~Object() {}

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

/// <summary>
/// Возвращает полюс по имени.
Expand All @@ -87,7 +86,7 @@ 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;

/// <summary>
/// Соединяет указанные полюса текущего и указанного устройства.
Expand All @@ -102,6 +101,7 @@ class Object {
/// для которого вызывается этот метод.
/// </remarks>
bool connect(const std::string& poleName, const Object& other, const std::string& otherPoleName);
bool disconnect(const std::string& poleName);
};

/// <summary>
Expand All @@ -113,6 +113,19 @@ class Switch : public Object {

Switch(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;
};

class Lamp : public Object {
public:
Pole a1, a2;

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

virtual size_t getPoleCount() const { return 2; }

virtual const Pole* getPole(const std::string& name) const;
Expand All @@ -122,5 +135,15 @@ class Switch : public Object {
};

// TODO: класс светильника с двумя полюсами
class Generator : public Object {
public:
Pole p, n, e;

Generator(const std::string& name = "") : Object(name), p("Phase"), n("Neutral"), e("Earth") {};

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: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
34 changes: 30 additions & 4 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@

using namespace std;

B::B() : b_s("It's b!") {
A::A() : a_s("It's--a"), foo(0) {}

B::B() : b_s("It's--b") {
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++)
data[i] = i * 2;
data[i] = (float)i * 2;
}

/// <summary>
/// Выводит на экран адреса и размеры объекта типа <see cref="B"/> и его содержимого.
/// Можно модифицировать для собственных отладочных целей.
/// </summary>
/// <param name="b">Изучаемый объект</param>
void printInternals(const B& b) {

void printInternals(B& b) {
const A* a = &b, * a2 = a + 1;
cout << "Address of b is 0x" << &b << ", address of b.a_s is 0x" << &b.a_s << ", address of b.b_s is 0x" << &b.b_s << endl;
cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << endl;
cout << "B string is '" << b.getBString() << "'" << endl;
//cout << "B data: "; b.printData(cout); cout << endl;
cout << "B data,using printData: "; b.printData(cout); cout << endl;
cout << "B data,using printData2: "; b.printData2(cout); cout << endl;
}

/// <summary>
Expand All @@ -27,9 +32,23 @@ void printInternals(const B& b) {
/// </summary>
/// <returns>Значение B::b_s</returns>
std::string A::getBString() const {
// TODO
return *((const string*)(this + 1));
}

std::string B::getBString() const {
return b_s;
}

float A::getData(int idx) const {
return ((float*)(this + 2))[idx];
}

float B::getData(int idx) const {
return data[idx];
}



/// <summary>
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/>
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток
Expand All @@ -38,6 +57,8 @@ std::string A::getBString() const {
/// </summary>
void A::printData(std::ostream& os) {
// TODO
os << "A string is '" << a_s << "', B string is '" << getBString() << "'" << endl;
for (int i = 0; i < 7; ++i) os << getData(i) << " ";
}

/// <summary>
Expand All @@ -47,8 +68,13 @@ void A::printData(std::ostream& os) {
/// </summary>
void A::printData2(std::ostream& os) {
// TODO
B b = *((B*)(this));
os << "A string is '" << a_s << "', B string is '" << b.getBString() << "'" << endl;
for (int i = 0; i < 7; ++i) os << b.getData(i) << " ";
}



int main()
{
B b;
Expand Down
Loading