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