-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
112 lines (86 loc) · 2.6 KB
/
electricity.cpp
File metadata and controls
112 lines (86 loc) · 2.6 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
#include <iostream>
using namespace std;
#include "electricity.h"
bool Object::isConnectedTo(const Object& other) const
{
for (size_t idx_1 = 0; idx_1 < this->getPoleCount(); idx_1++) {
for (size_t idx_2 = 0; idx_2 < other.getPoleCount(); idx_2++) {
if (this->getPole(idx_1)->connectedObjectPole == other.getPole(idx_2)->name) return true;
}
}
return false;
}
bool Object::disconnect(const string& poleName)
{
if (getPole(poleName)->connectedObjectPole != "") {
getPole(poleName)->connectedObjectPole = "";
this->getPole(poleName)->connectedObject = nullptr;
return true;
}
else return false;
}
bool Object::connect(const string& poleName, Object& other, const string& otherPoleName)
{
this->getPole(poleName)->connectedObjectPole = otherPoleName;
this->getPole(poleName)->connectedObject = &other;
other.getPole(otherPoleName)->connectedObject = this;
other.getPole(otherPoleName)->connectedObjectPole = poleName;
return false;
}
Switch::Switch(const string& name) : Object(name), A1("A1"), A2("A2") {}
Light::Light(const string& name_) : Object(name_), A1("A1"), A2("A2") {}
Power::Power(const string& name_) : Object(name_), A1("A1"), A2("A2"), A3("A3") {}
const Pole* Switch::getPole(const string& name) const
{
if (name == A1.name)
return &A1;
if (name == A2.name)
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* Power::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* Switch::getPole(size_t idx) const
{
return (getPole("A" + to_string(idx + 1)));
}
const Pole* Light::getPole(size_t idx) const
{
return (getPole("A" + to_string(idx + 1)));
}
const Pole* Power::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;
Power p;
Light l;
Switch sw3;
p.connect("A1", l, "A1");
l.connect("A2", sw, "A1");
cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl;
cout << "is " << (l.isConnectedTo(sw3) ? "" : "not ") << "connected!" << endl;
p.disconnect("A1");
cout << "is " << (p.isConnectedTo(l) ? "" : "not ") << "connected!" << endl;
return 0;
}