From 928dd6bf9c11b6bdb99f5a3fe3207b995765344d Mon Sep 17 00:00:00 2001 From: Polina-nest <46245789+Polina-nest@users.noreply.github.com> Date: Mon, 6 Jun 2022 09:43:45 +0300 Subject: [PATCH 1/4] Add files via upload --- ...0\276\321\202\320\275\321\213\320\265.cxx" | 174 +++++++++++++++ ...0\272\321\202\320\276\321\200\320\260.cxx" | 65 ++++++ ...0\265\321\201\321\202\320\262\320\276.cxx" | 206 ++++++++++++++++++ 3 files changed, 445 insertions(+) create mode 100644 "\320\226\320\270\320\262\320\276\321\202\320\275\321\213\320\265.cxx" create mode 100644 "\320\262\320\265\320\272\321\202\320\276\321\200\320\260.cxx" create mode 100644 "\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" diff --git "a/\320\226\320\270\320\262\320\276\321\202\320\275\321\213\320\265.cxx" "b/\320\226\320\270\320\262\320\276\321\202\320\275\321\213\320\265.cxx" new file mode 100644 index 00000000..43c2c724 --- /dev/null +++ "b/\320\226\320\270\320\262\320\276\321\202\320\275\321\213\320\265.cxx" @@ -0,0 +1,174 @@ +#include "animal.h" + +#include +#include +#include +using namespace std; + +int main() { + return 0; +} + + +class Animal { +private: + float the_average_value_of_the_duration_of_life; +public: + void set_the_average_value_of_the_duration_of_life(float new_the_average_value_of_the_duration_of_life) { the_average_value_of_the_duration_of_life = new_the_average_value_of_the_duration_of_life; } + float get_the_average_value_of_the_duration_of_life() const { return the_average_value_of_the_duration_of_life; } + + bool Gender; + virtual string about() const; +}; + +string Animal::about() const { + stringstream ss; + ss << "Gender = " << " " << Gender; + return ss.str(); +}; + +class Mammal : public Animal { +private: + int number_of_individuals; +public: + void set_number_of_individuals(int new_number_of_individuals) { number_of_individuals = new_number_of_individuals; } + int get_number_of_individuals() const { return number_of_individuals; } + + string Coat_color; + virtual string about() const; +}; + +string Mammal::about() const { + stringstream ss; + ss << Animal::about() << " " << " Coat_color = " << " " << Coat_color; + return ss.str(); +}; + + + +class Quadrupeds : public Mammal { +private: + float percentage_of_quality_of_life; +public: + void set_percentage_of_quality_of_life(float new_percentage_of_quality_of_life) { percentage_of_quality_of_life = new_percentage_of_quality_of_life; } + float get_percentage_of_quality_of_life() const { return percentage_of_quality_of_life; } + + bool limbs; + virtual string about() const; +}; + +string Quadrupeds::about() const { + stringstream ss; + ss << Animal::about() << " " << " limbs = " << " " << limbs; + return ss.str(); +}; + + +class Birds : public Mammal { +private: + int the_average_value_of_the_flight_for_one_season; +public: + void set_the_average_value_of_the_flight_for_one_season(int new_the_average_value_of_the_flight_for_one_season) { the_average_value_of_the_flight_for_one_season = new_the_average_value_of_the_flight_for_one_season; } + int get_the_average_value_of_the_flight_for_one_season() const { return the_average_value_of_the_flight_for_one_season; } + + + bool ability_to_fly; + virtual string about() const; +}; + +string Birds::about() const { + stringstream ss; + ss << Mammal::about() << " " << " ability_to_fly = " << " " << ability_to_fly; + return ss.str(); +}; + +class Cat : public Animal { +private: + int the_number_of_mice_caught_per_unit_of_time; +public: + void set_the_number_of_mice_caught_per_unit_of_time(int new_the_number_of_mice_caught_per_unit_of_time) { the_number_of_mice_caught_per_unit_of_time = new_the_number_of_mice_caught_per_unit_of_time; } + int get_the_number_of_mice_caught_per_unit_of_time() const { return the_number_of_mice_caught_per_unit_of_time; } + + + float vibrissaLength; + virtual string about() const; +}; + +string Cat::about() const { + stringstream ss; + ss << Animal::about() << " " << " vibrissaLength = " << " " << vibrissaLength; + return ss.str(); +}; + +class Manul : public Cat { +private: + int average_weight; +public: + void set_average_weight(int new_average_weight) { average_weight = new_average_weight; } + int get_average_weight() const { return average_weight; } + + + float Average_length_of_wool; + virtual string about() const; +}; + +string Manul::about() const { + stringstream ss; + ss << Cat::about() << " " << " Average_length_of_wool = " << " " << Average_length_of_wool; + return ss.str(); +}; + +class Mainkun : public Cat { +private: + string eye_color; +public: + void set_eye_color(string new_eye_color) { eye_color = new_eye_color; } + string get_eye_color() const { return eye_color; } + + float Number_of_fleas; + virtual string about() const; +}; + +string Mainkun::about() const { + stringstream ss; + ss << Cat::about() << " " << " Number_of_fleas = " << " " << Number_of_fleas; + return ss.str(); +}; + + +int main(){ + Mainkun kot_bOris; + Manul kot_Vasily; + Cat Roudi; + Birds ANgry_birds; + Quadrupeds dogs; + + kot_bOris.Gender = true; + kot_bOris.Number_of_fleas = 50000; + kot_bOris.vibrissaLength = 5; + + kot_Vasily.Average_length_of_wool = 3; + kot_Vasily.Gender = true; + kot_Vasily.vibrissaLength = 6; + + Roudi.Gender = true; + Roudi.vibrissaLength = 4; + + ANgry_birds.ability_to_fly = true; + ANgry_birds.Coat_color = "red"; + ANgry_birds.Gender = false; + + dogs.Coat_color = "red"; + dogs.Gender = true; + dogs.limbs = 4; + + cout << "-------------------------------------------------------------" << endl; + cout << "kot_bOris: " << kot_bOris.about() << endl; + cout << "kot_Vasily: " << kot_Vasily.about() << endl; + cout << "Roudi: " << Roudi.about() << endl; + cout << "ANgry_birds: " << ANgry_birds.about() << endl; + cout << "dogs: " << dogs.about() << endl; + cout << "-------------------------------------------------------------"; + + +} diff --git "a/\320\262\320\265\320\272\321\202\320\276\321\200\320\260.cxx" "b/\320\262\320\265\320\272\321\202\320\276\321\200\320\260.cxx" new file mode 100644 index 00000000..43eddc3d --- /dev/null +++ "b/\320\262\320\265\320\272\321\202\320\276\321\200\320\260.cxx" @@ -0,0 +1,65 @@ +#include +#include + +using namespace std; + +class vector3d { + float data[3]; +public: + vector3d() { data[2] = data[1] = data[0] = 0; } + vector3d(float value) { data[2] = data[1] = data[0] = value; } + vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; } + // lvalue = rvalue + float operator[](int idx) const { return data[idx]; } + float& operator[](int idx) { return data[idx]; } + +}; + + +ostream& operator <<(ostream& os, const vector3d& v) { + return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; +} + +vector3d operator + (const vector3d& v1, const vector3d& v2) { return vector3d(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]); } +vector3d operator - (const vector3d& v1, const vector3d& v2) { return vector3d(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]); } +vector3d operator * (const vector3d& v1, const float x) { return vector3d(v1[0] * x, v1[1] * x, v1[2] * x); } +vector3d operator / (const vector3d& v1, const float x) { return vector3d(v1[0] / x, v1[1] / x, v1[2] / x); } +const vector3d operator -(const vector3d& v1) { return vector3d(-v1[0], -v1[1], -v1[2]); } +const vector3d operator !(const vector3d& v1) { + if (v1[0] == 0 and v1[1] == 0 and v1[2] == 0) { return vector3d(1, 1, 1); } + else return vector3d(0, 0, 0); +} + +bool test_vector3d() { + vector3d v1(10, 85, 110); + vector3d v2(20, 65, 90); + + cout << "v1: " << "10, 85, 110" << endl; + cout << "v2: " << "20, 65, 90" << endl; + + bool ERROR = false; + + cout << "v1 + v2 = " << v1 + v2 << endl; + if ((v1 + v2)[0] != v1[0] + v2[0] or (v1 + v2)[1] != v1[1] + v2[1] or (v1 + v2)[2] != v1[2] + v2[2]) { + cerr << "invalid, should be " << "{ " << v1[0] + v2[0] << " " << v1[1] + v2[1] << " " << v1[2] + v2[2] << " }" << endl; + ERROR = true; + } + cout << "v1 - v2 = " << v1 - v2 << endl; + if ((v1 - v2)[0] != v1[0] - v2[0] or (v1 - v2)[1] != v1[1] - v2[1] or (v1 - v2)[2] != v1[2] - v2[2]) { + cerr << "invalid, should be " << "{ " << v1[0] - v2[0] << " " << v1[1] - v2[1] << " " << v1[2] - v2[2] << " }" << endl; + ERROR = true; + } + cout << "v1 * 5 = " << v1 * 5 << endl; + if ((v1 * 5)[0] != v1[0] * 5 or (v1 * 5)[1] != v1[1] * 5 or (v1 * 5)[2] != v1[2] * 5) { + cerr << "invalid, should be " << "{ " << v1[0] * 5 << " " << v1[1] * 5 << " " << v1[2] * 5 << " }" << endl; + ERROR = true; + } + cout << "v1 / 5 = " << v1 / 5 << endl; + if ((v1 / 5)[0] != v1[0] / 5 || (v1 / 5)[1] != v1[1] / 5 || (v1 / 5)[2] != v1[2] / 5) { + cerr << "invalid, should be " << "{ " << v1[0] / 5 << " " << v1[1] / 5 << " " << v1[2] / 5 << " }" << endl; + ERROR = true; + } + return ERROR; +} + +int main(int argc, char** argv) { return test_vector3d(); } diff --git "a/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" "b/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" new file mode 100644 index 00000000..dc95789a --- /dev/null +++ "b/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" @@ -0,0 +1,206 @@ + +#include +#include + + +using namespace std; + +class Object; + +@@ -50,7 +54,15 @@ class Object { + + Pole* getPole(size_t idx) { /* TODO */ return nullptr; } + Pole* getPole(size_t idx) { + if (idx > getPoleCount()) { + std::string str = "O" + std::to_string(idx); + return getPole(str); + } + else { + return nullptr; + } + }; + + +@@ -63,7 +75,7 @@ class Object { + 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; } + +@@ -114,13 +126,12 @@ class Object { + + bool disconnect(const std::string& poleName); +}; + + +class Switch : public Object { +public: + Pole a1, a2; + Pole A1, A2; + + Switch(const std::string& name = ""); + +@@ -132,6 +143,39 @@ class Switch : public Object { + virtual const Pole* getPole(size_t idx) const; +}; + + +class Power : public Object { +public: + Pole A1, A2, A3; + + Power(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; +}; + + +class Light : public Object { +public: + Pole A1, A2; + + bool st = false; + + Light(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; +}; + + + + + +bool Object::isConnectedTo(const Object& other) const +{ + // TODO + for (size_t idx_1 = 0; idx_1 < this->getPoleCount(); idx_1++) { + + for (size_t idx_2 = 0; idx_2 < other.getPoleCount(); idx_2++) { + + if (this->getPole(idx_1)->connectedObjectPole == other.getPole(idx_2)->name) return true; + + } + } + return false; +} + +bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) +bool Object::disconnect(const std::string& poleName) +{ + + return false; + if (getPole(poleName)->connectedObjectPole != "") { + + getPole(poleName)->connectedObjectPole = ""; + this->getPole(poleName)->connectedObject = nullptr; + return true; + } + else return false; +} + +bool Object::disconnect(const std::string& poleName) +bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) +{ + // TODO + this->getPole(poleName)->connectedObjectPole = otherPoleName; + this->getPole(poleName)->connectedObject = &other; + + other.getPole(otherPoleName)->connectedObject = this; + other.getPole(otherPoleName)->connectedObjectPole = poleName; + + return false; +} + +Switch::Switch(const std::string& name) + : Object(name) + , a1("A1") + , a2("A2") +Switch::Switch(const std::string& name) : Object(name), A1("A1"), A2("A2") {} + +Light::Light(const std::string& name_) : Object(name_), A1("A1"), A2("A2") {} + +Power::Power(const std::string& name_) : Object(name_), A1("A1"), A2("A2"), A3("A3") {} + +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(const string& name) const +const Pole* Light::getPole(const string& name) const +{ + if (name == A1.name) + return &A1; + if (name == A2.name) + return &A2; + return nullptr; +} +const Pole* Power::getPole(const string& name) const +{ + if (name == a1.name) + return &a1; + if (name == a2.name) + return &a2; + if (name == A1.name) + return &A1; + if (name == A2.name) + return &A2; + if (name == A3.name) + return &A3; + return nullptr; +} + +const Pole* Switch::getPole(size_t idx) const +{ + // TODO + return nullptr; + return (getPole("A" + std::to_string(idx + 1))); +} + +const Pole* Light::getPole(size_t idx) const +{ + return (getPole("A" + std::to_string(idx + 1))); +} +const Pole* Power::getPole(size_t idx) const +{ + return (getPole("A" + std::to_string(idx + 1))); +} +int main() +{ + Switch sw, sw2; + sw.connect("A2", sw2, "A1"); + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected!" << endl; + + + Power p; + Light l; + Switch sw3; + + + + p.connect("A1", l, "A1"); + l.connect("A2", sw, "A1"); + cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; + cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected!" << endl; + + p.disconnect("A1"); + cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; + + return 0; +} From 91f7a387eeeb799c51451ae8021bd84efd896d28 Mon Sep 17 00:00:00 2001 From: Polina-nest <46245789+Polina-nest@users.noreply.github.com> Date: Mon, 6 Jun 2022 10:13:04 +0300 Subject: [PATCH 2/4] Add files via upload --- memhacks.cxx | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ memhacks.h | 31 +++++++++++++++++++++++ newhacks.cxx | 52 ++++++++++++++++++++++++++++++++++++++ newhacks.h | 36 ++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 memhacks.cxx create mode 100644 memhacks.h create mode 100644 newhacks.cxx create mode 100644 newhacks.h diff --git a/memhacks.cxx b/memhacks.cxx new file mode 100644 index 00000000..2b4a58d9 --- /dev/null +++ b/memhacks.cxx @@ -0,0 +1,71 @@ + +using namespace std; + +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; +@@ -13,12 +15,13 @@ B::B() : b_s("It's b!") { + + + +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 printData: "; b.printData(cout); cout << endl; + cout << "B data printData2: "; b.printData2(cout); cout << endl; +} + + +@@ -27,7 +30,19 @@ void printInternals(const B& b) { + + + +std::string A::getBString() const { + + 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]; +} + + + +@@ -37,7 +52,8 @@ std::string A::getBString() const { + + + +void A::printData(std::ostream& os) { + + os << "A string: " << a_s << ", B string: " << getBString() << ", data: "; + for (int i = 0; i < 7; ++i) os << getData(i) << " "; +} + + + +@@ -46,7 +62,9 @@ void A::printData(std::ostream& os) { + + + +void A::printData2(std::ostream& os) { + + B b = *((B*)(this)); + os << "A string: " << a_s << "', B string: " << b.getBString() << ", data: "; + for (int i = 0; i < 7; ++i) os << b.getData(i) << " "; +} + +int main() diff --git a/memhacks.h b/memhacks.h new file mode 100644 index 00000000..cd0ce0eb --- /dev/null +++ b/memhacks.h @@ -0,0 +1,31 @@ +std::string a_s; + int foo; + + friend void printInternals(const B&); + friend void printInternals(B& b); + +public: + std::string getBString() const; + A(); + + virtual std::string getBString() const; + virtual float getData(int idx) const; + void printData(std::ostream& os); + void printData2(std::ostream& os); +}; +@@ -21,10 +24,13 @@ class B : public A { + std::string b_s; + float data[7]; + + friend void printInternals(const B&); + friend void printInternals(B& b); + +public: + B(); + + virtual std::string getBString() const; + virtual float getData(int idx) const; +}; + +void printInternals(const B& b); +void printInternals(B& b); diff --git a/newhacks.cxx b/newhacks.cxx new file mode 100644 index 00000000..3def5b8f --- /dev/null +++ b/newhacks.cxx @@ -0,0 +1,52 @@ +#include +#include "newhacks.h" + +using namespace std; + +Foo::Foo() : cat(), frog() { cerr << "constructed Foo" << this << endl; } +Foo::~Foo() { cerr << "destructed Foo" << this << endl; } + +void *Foo::operator new(size_t size) { + void *pVoid = malloc(size); + cerr << "operator new(" << size << ") returns " << pVoid << endl; + return pVoid; +} + +void Foo::operator delete(void *pVoid) { + cerr << "operator delete(" << pVoid << ")" << endl; + free(pVoid); +} + +Bar::Bar() : turtle(), dog() { cerr << "constructed Bar " << this << endl; } +Bar::~Bar() { cerr << "destructed Bar" << this << endl; } + +void *Bar::operator new(size_t size) { + void *pVoid = malloc(size); + cerr << "operator new(" << size << ") returns " << pVoid << endl; + return pVoid; +} + +void Bar::operator delete(void *pVoid) { + cerr << "operator delete(" << pVoid << ")" << endl; + free(pVoid); +} + +Buz::Buz() : snake(), rhinoceros() { cerr << "constructed Buz" << this << endl; } +Buz::~Buz() { cerr << "destructed Buz " << this << endl; } + +int main() +{ + Foo foo_stack = Foo(); + Bar bar_stack = Bar(); + Buz buz_stack = Buz(); + + Foo* foo_heap = new Foo(); + Bar* bar_heap = new Bar(); + + + delete(foo_heap); + delete(bar_heap); + + return 0; +} +} diff --git a/newhacks.h b/newhacks.h new file mode 100644 index 00000000..9e25f3ca --- /dev/null +++ b/newhacks.h @@ -0,0 +1,36 @@ +#include + +class Foo { + int cat; + float frog; +public: + Foo(); + ~Foo(); + + static void *operator new(size_t size); + static void operator delete(void *pVoid); +}; + +class Bar : public Foo { + int turtle; + std::string dog; + +public: + Bar(); + ~Bar(); + + static void *operator new(size_t size); + static void operator delete(void *pVoid); +}; + +class Buz : public Foo { + float snake; + short rhinoceros; + +public: + Buz(); + ~Buz(); + + static void *operator new(size_t size) = delete; + static void operator delete(void *pVoid) = delete; +}; From c4a5d97973880cc0da823001dd4cedbecb19db96 Mon Sep 17 00:00:00 2001 From: Polina-nest <46245789+Polina-nest@users.noreply.github.com> Date: Mon, 6 Jun 2022 10:36:29 +0300 Subject: [PATCH 3/4] Add files via upload --- ...0\265\321\201\321\202\320\262\320\276.cxx" | 196 ++++-------------- 1 file changed, 43 insertions(+), 153 deletions(-) diff --git "a/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" "b/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" index dc95789a..807eac43 100644 --- "a/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" +++ "b/\321\215\320\273\320\265\320\272\321\202\321\200\320\270\321\207\320\265\321\201\321\202\320\262\320\276.cxx" @@ -1,206 +1,96 @@ - -#include -#include - +#include +#include "electricity.h" using namespace std; -class Object; - -@@ -50,7 +54,15 @@ class Object { - - Pole* getPole(size_t idx) { /* TODO */ return nullptr; } - Pole* getPole(size_t idx) { - if (idx > getPoleCount()) { - std::string str = "O" + std::to_string(idx); - return getPole(str); - } - else { - return nullptr; - } - }; - - -@@ -63,7 +75,7 @@ class Object { - 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; } - -@@ -114,13 +126,12 @@ class Object { - - bool disconnect(const std::string& poleName); -}; - - -class Switch : public Object { -public: - Pole a1, a2; - Pole A1, A2; - - Switch(const std::string& name = ""); - -@@ -132,6 +143,39 @@ class Switch : public Object { - virtual const Pole* getPole(size_t idx) const; -}; - - -class Power : public Object { -public: - Pole A1, A2, A3; - - Power(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; -}; - - -class Light : public Object { -public: - Pole A1, A2; - - bool st = false; - - Light(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; -}; - - - - - bool Object::isConnectedTo(const Object& other) const { - // TODO - for (size_t idx_1 = 0; idx_1 < this->getPoleCount(); idx_1++) { - - for (size_t idx_2 = 0; idx_2 < other.getPoleCount(); idx_2++) { + for (size_t j = 0; j < getPoleCount(); j++) + { + const Pole * selfPole = getPole(j); + if (selfPole != nullptr && (selfPole->connectedObject) == &other) return true; + } - if (this->getPole(idx_1)->connectedObjectPole == other.getPole(idx_2)->name) return true; - } - } return false; } -bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) -bool Object::disconnect(const std::string& poleName) +bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName) + { + + if (poleName == otherPoleName && &other == this) return false; - return false; - if (getPole(poleName)->connectedObjectPole != "") { + Pole * selfPole = getPole(poleName); + Pole * otherPole = other.getPole(otherPoleName); + if (otherPole == nullptr || otherPole == nullptr) return false; + + (selfPole->connectedObject) = &other; + (selfPole->connectedObjectPole) = otherPoleName; + + (otherPole->connectedObject) = this; + + (otherPole->connectedObjectPole) = poleName; - getPole(poleName)->connectedObjectPole = ""; - this->getPole(poleName)->connectedObject = nullptr; - return true; - } - else return false; + return true; } bool Object::disconnect(const std::string& poleName) -bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName) { - // TODO - this->getPole(poleName)->connectedObjectPole = otherPoleName; - this->getPole(poleName)->connectedObject = &other; + return false; + Pole * p = getPole(poleName); + if (p == nullptr) return false; - other.getPole(otherPoleName)->connectedObject = this; - other.getPole(otherPoleName)->connectedObjectPole = poleName; + (p->connectedObject) = nullptr; + (p->connectedObjectPole) = ""; - return false; + return true; } + + Switch::Switch(const std::string& name) : Object(name) , a1("A1") , a2("A2") -Switch::Switch(const std::string& name) : Object(name), A1("A1"), A2("A2") {} - -Light::Light(const std::string& name_) : Object(name_), A1("A1"), A2("A2") {} - -Power::Power(const std::string& name_) : Object(name_), A1("A1"), A2("A2"), A3("A3") {} - -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(const string& name) const -const Pole* Light::getPole(const string& name) const -{ - if (name == A1.name) - return &A1; - if (name == A2.name) - return &A2; - return nullptr; -} -const Pole* Power::getPole(const string& name) const { if (name == a1.name) return &a1; if (name == a2.name) return &a2; - if (name == A1.name) - return &A1; - if (name == A2.name) - return &A2; - if (name == A3.name) - return &A3; return nullptr; } const Pole* Switch::getPole(size_t idx) const -{ - // TODO +{ if (idx == 0) return &a1; + else if (idx == 1) return &a2; + + return nullptr; - return (getPole("A" + std::to_string(idx + 1))); } -const Pole* Light::getPole(size_t idx) const -{ - return (getPole("A" + std::to_string(idx + 1))); -} -const Pole* Power::getPole(size_t idx) const -{ - return (getPole("A" + std::to_string(idx + 1))); -} int main() { Switch sw, sw2; sw.connect("A2", sw2, "A1"); cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; - cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected!" << endl; - - Power p; - Light l; - Switch sw3; + cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl; + Generator g; Light l; Switch sw3; + g.connect("p", l, "A1"); + l.connect("A2", sw3, "A1"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; + cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl; - p.connect("A1", l, "A1"); - l.connect("A2", sw, "A1"); - cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; - cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected!" << endl; + g.disconnect("p"); + cout << "is " << (g.isConnectedTo(l) ? "" : "not ") << "connected" << endl; - p.disconnect("A1"); - cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl; + return 0; return 0; } From 6fbac6bbc01449db0e1fd308c52d250b3b0adc43 Mon Sep 17 00:00:00 2001 From: Polina-nest <46245789+Polina-nest@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:09:41 +0300 Subject: [PATCH 4/4] Add files via upload --- electricity.h | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 electricity.h diff --git a/electricity.h b/electricity.h new file mode 100644 index 00000000..fa1b8f55 --- /dev/null +++ b/electricity.h @@ -0,0 +1,102 @@ +#pragma once +#include +class Object; + +struct Pole { + + std::string name; + + Object* connectedObject; + + std::string connectedObjectPole; + Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {} +}; + +class Object { + std::string name; +protected: + Object(const std::string& name_) : name(name_) {} + + Pole* getPole(size_t idx) { return nullptr; } + Pole* getPole(size_t idx) { + return const_cast(const_cast(this)->getPole(idx)); + } + +public: + 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; } + + + Pole* getPole(const std::string& name) { return const_cast(const_cast(this)->getPole(name)); } + + virtual const Pole* getPole(const std::string& name) const = 0; + + virtual size_t getPoleCount() const = 0; + + + bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName); + + bool disconnect(const std::string& poleName); +}; + +class Switch : public Object { +public: + Pole a1, a2; + 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 Light : public Object { + public: + Pole a1, a2; + + Light(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 { + if (name == a1.name) return &a1; + else if (name == a2.name) return &a2; + + return nullptr; + } + protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) return &a1; + else if (idx == 1)return &a2; + + return nullptr; + } +}; + +class Generator : public Object { + public: + Pole phase, neural, earth; + + Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {} + + virtual size_t getPoleCount() const { return 3; } + + virtual const Pole* getPole(const std::string& name) const { + if (name == phase.name) return &phase; + else if (name == neural.name) return &neural; + else if (name == earth.name) return &earth; + + return nullptr; + } + protected: + virtual const Pole* getPole(size_t idx) const { + if (idx == 0) return &phase; + else if (idx == 1)return &neural; + else if (idx == 2)return &earth; + + return nullptr; + } +};