-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
153 lines (139 loc) · 3.9 KB
/
electricity.cpp
File metadata and controls
153 lines (139 loc) · 3.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include "electricity.h"
using namespace std;
bool Object::isConnectedTo(const Object& other) const
{
size_t n = (this)->getPoleCount();
for(size_t i = 0;i < n;i++){
if(((this->getPole(i+1))->connectedObject) == &other) return true;
}
// TODO
return false;
}
bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
if(this->isConnectedTo(other)) return false;
else {
//disconnect to this
Object* nowConnectThis = (this->getPole(poleName))->connectedObject;
string nwCnctThs = (this->getPole(poleName))->connectedObjectPole;
if (nowConnectThis != nullptr) {
(nowConnectThis->getPole(nwCnctThs))->connectedObject = nullptr;
(nowConnectThis->getPole(nwCnctThs))->connectedObjectPole = "";
(this->getPole(poleName))->connectedObject = const_cast<Object*>(&other);
(this->getPole(poleName))->connectedObjectPole = otherPoleName;
} //connect to this
else {
(this->getPole(poleName))->connectedObject = const_cast<Object*>(&other);
(this->getPole(poleName))->connectedObjectPole = otherPoleName;
}
//disconnect to other
Object* nowConnectOther = (other.getPole(otherPoleName))->connectedObject;
string nwCnctOthr = (other.getPole(otherPoleName))->connectedObjectPole;
Pole* othr = const_cast<Pole*>(other.getPole(otherPoleName));
if (nowConnectOther != nullptr){
(nowConnectOther->getPole(nwCnctOthr))->connectedObject = nullptr;
(nowConnectOther->getPole(nwCnctOthr))->connectedObjectPole = "";
othr->connectedObject = this;
othr->connectedObjectPole = poleName;
} //connect to other
else{
othr->connectedObject = this;
othr->connectedObjectPole = poleName;
}
return true;
}
}
Switch::Switch(const std::string& name)
: Object(name)
, s1("S1")
, s2("S2")
{
}
Lamp::Lamp(const std::string& name)
:Object(name)
, l1("L1")
, l2("L2")
{
}
Generator::Generator(const std::string& name)
:Object(name)
, p("Plus")
, m("Minus")
, g("Ground")
{
}
const Pole* Switch::getPole(const string& name) const
{
if (name == s1.name)
return &s1;
if (name == s2.name)
return &s2;
return nullptr;
}
const Pole* Lamp::getPole(const string& name) const
{
if (name == l1.name)
return &l1;
if (name == l2.name)
return &l2;
return nullptr;
}
const Pole* Generator::getPole(const string& name) const
{
if (name == p.name)
return &p;
if (name == m.name)
return &m;
if (name == g.name)
return &g;
return nullptr;
}
const Pole* Switch::getPole(size_t idx) const
{
if (idx == 1)
return &s1;
if (idx == 2)
return &s2;
// TODO
return nullptr;
}
const Pole* Lamp::getPole(size_t idx) const
{
if (idx == 1)
return &l1;
if (idx == 2)
return &l2;
// TODO
return nullptr;
}
const Pole* Generator::getPole(size_t idx) const
{
if (idx == 1)
return &p;
if (idx == 2)
return &m;
if (idx == 3)
return &g;
return nullptr;
}
int main()
{
Switch sw, sw2;
sw.connect("S2", sw2, "S1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
Switch sw0;
Lamp l;
Generator g;
sw0.connect("S1", l,"L1");
l.connect("L2",g,"Plus");
g.connect("Minus",sw0,"S2");
if(sw0.isConnectedTo(g) && sw0.isConnectedTo(l)) cout << "Switch is connected\n";
else cout << "Switch is not connected";
if(l.isConnectedTo(g) && l.isConnectedTo(sw0)) cout << "Lamp is connected\n";
else cout << "Lamp is not connected";
if(g.isConnectedTo(l) && g.isConnectedTo(sw0)) cout << "Generator is connected\n";
else cout << "Generator is not connected";
// TODO: создать цепь из генератора, выключателя и светильника
return 0;
}