-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
150 lines (129 loc) · 3.73 KB
/
electricity.cpp
File metadata and controls
150 lines (129 loc) · 3.73 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
#include <iostream>
#include "electricity.h"
using namespace std;
bool Object::isConnectedTo(const Object& other) const
{
// TODO
for (int i = 1; i <= getPoleCount(); i++) {
for (int j = 1; j <= other.getPoleCount(); j++) {
if (getPole(i)->connectedObjectPole == other.getPole(j)->name && getPole(i)->connectedObject == const_cast<Object*>(&other))
return true;
}
}
return false;
}
bool Object::disconnect(const std::string& poleName)
{
//TODO
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;
}
bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
disconnect(poleName);
disconnect(otherPoleName);
getPole(poleName)->connectedObject = const_cast<Object*>(&other);
getPole(poleName)->connectedObjectPole = otherPoleName;
Pole* p = const_cast<Pole*>(other.getPole(otherPoleName));
p->connectedObjectPole = poleName;
p->connectedObject = const_cast<Object*>(this);
return true;
}
Switch::Switch(const std::string& name)
: Object(name)
, a1("A1")
, a2("A2")
{
}
Light::Light(const std::string& name)
: Object(name)
, a1("A1")
, a2("A2")
{
}
Generator::Generator(const std::string& name)
: Object(name)
, a1("A1") //фаза - faza
, a2("A2") //нейтраль - neytral
, a3("A3") //Земля - Zemlai
{
}
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
{
// TODO
if (idx == 1)
return &a1;
if (idx == 2)
return &a2;
return nullptr;
}
const Pole* Light::getPole(size_t idx) const
{
if (idx == 1)
return &a1;
if (idx == 2)
return &a2;
return nullptr;
}
const Pole* Light::getPole(const string& name) const
{
if (name == a1.name)
return &a1;
if (name == a2.name)
return &a2;
return nullptr;
}
const Pole* Generator::getPole(size_t idx) const
{
if (idx == 1)
return &a1;
if (idx == 2)
return &a2;
if (idx == 3)
return &a3;
return nullptr;
}
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;
}
int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
// TODO: создать цепь из генератора, выключателя и светильника
Generator g; //A1 - фаза, А2 - нейтрал, А3 - земля
Light l;
Switch s;
// к А1(фазу) подключим переключатель к полюсу А2
g.connect("A1", s, "A2");
cout << "is " << (g.isConnectedTo(s) ? "" : "not ") << "connected" << endl;
//к другому полюсу переключателя подключим А1 подключаем лампу полюсу А2
s.connect("A1", l, "A2");
cout << "is " << (s.isConnectedTo(l) ? "" : "not ") << "connected" << endl;
// подключим лампу другим полюсам к генератору, нейтрал
g.connect("A2", l, "A1");
cout << "is " << (s.isConnectedTo(l) ? "" : "not ") << "connected" << endl;
return 0;
}