-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
122 lines (104 loc) · 2.8 KB
/
electricity.cpp
File metadata and controls
122 lines (104 loc) · 2.8 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
112
113
114
115
116
117
118
119
120
121
122
#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;
}
return false;
}
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));
if (otherPole == nullptr || otherPole == nullptr) return false;
pole->connectedObject = (Object*)(&other);
pole->connectedObjectPole = otherPoleName;
otherPole->connectedObject = this;
otherPole->connectedObjectPole = poleName;
return true;
}
bool Object::disconnect(const std::string& poleName)
{
auto pole = getPole(poleName);
if (pole->connectedObjectPole == "")
return false;
else {
pole->connectedObjectPole = "";
pole->connectedObject = nullptr;
return true;
}
}
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;
Lamp lamp;
Switch sw3;
generator.connect("A1", lamp, "A1");
lamp.connect("A2", sw3, "A1");
cout << "is " << (generator.isConnectedTo(lamp) ? "" : "not ") << "connected" << endl;
cout << "is " << (lamp.isConnectedTo(sw3) ? "" : "not ") << "connected" << endl;
generator.disconnect("A1");
cout << "is " << (generator.isConnectedTo(lamp) ? "" : "not ") << "connected" << endl;
return 0;
}