-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
111 lines (97 loc) · 2.94 KB
/
electricity.cpp
File metadata and controls
111 lines (97 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include "electricity.h"
using namespace std;
bool Object::isConnectedTo(const Object& other) const
{
for (int i = 0; i < getPoleCount(); ++i) {
auto pole = getPole(i);
if (pole != nullptr && pole->connectedObject == &other)
return true;
}
}
bool Object::connect(const std::string& poleName, Object& other, const std::string& otherPoleName)
{
if (poleName == otherPoleName && &other == this)
return false;
auto pole = getPole(poleName);
auto otherPole = (Pole*)(other.getPole(otherPoleName));
pole->connectedObject = (Object*)(&other);
pole->connectedObjectPole = otherPoleName;
otherPole->connectedObject = this;
otherPole->connectedObjectPole = poleName;
return true;
}
bool Object::disconnect(const std::string& poleName)
{
if (getPole(poleName)->connectedObject != nullptr)
{
getPole(getPole(poleName)->connectedObjectPole)->connectedObject = nullptr;
getPole(getPole(poleName)->connectedObjectPole)->connectedObjectPole = "";
getPole(poleName)->connectedObject = nullptr;
getPole(poleName)->connectedObjectPole = "";
return true;
}
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
{
return getPole("A" + to_string(idx + 1));
}
Lamp::Lamp(const string& name)
: Object(name)
, a1("A1")
, a2("A2")
{
}
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 {
return getPole("A" + to_string(idx + 1));
}
Generator::Generator(const string& name)
: Object(name)
, a1("A1")
, a2("A2")
, a3("A3")
{
}
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 {
return getPole("A" + to_string(idx + 1));
}
int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
Generator generator_test;
Lamp lamp_test;
Switch switch_test;
generator_test.connect("A1", switch_test, "A2");
switch_test.connect("A1", lamp_test, "A2");
cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl;
cout << "is " << (lamp_test.isConnectedTo(switch_test) ? "" : "not ") << "connected" << endl;
generator_test.disconnect("A1");
cout << "is " << (generator_test.isConnectedTo(lamp_test) ? "" : "not ") << "connected" << endl;
return 0;
}