-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.cpp
More file actions
115 lines (107 loc) · 2.99 KB
/
electricity.cpp
File metadata and controls
115 lines (107 loc) · 2.99 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
#include <iostream>
#include "electricity.h"
using namespace std;
bool Object::isConnectedTo(const Object& other) const
{
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)->name!=other.getPole(j)->name)
return true;
}
}
return false;
}
bool Object::connect(const std::string& poleName, const Object& other, const std::string& otherPoleName)
{
if (poleName!=otherPoleName){
getPole(poleName)->connectedObject=const_cast<Object*>(&other);
getPole(poleName)->connectedObjectPole=otherPoleName;
getPole(otherPoleName)->connectedObjectPole=poleName;
getPole(otherPoleName)->connectedObject=const_cast<Object*>(this);
return true;
}
return false;
}
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") //Zemly ~ земля
{}
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
{
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* Light::getPole(size_t idx) const
{
if (idx==1)
return &a1;
if (idx==2)
return &a2;
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;
}
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;
}
int main()
{
Switch sw, sw2;
sw.connect("A2", sw2, "A1");
cout << "is " << (sw.isConnectedTo(sw2) ? "" : "not ") << "connected" << endl;
//создать цепь из генератора, выключателя и светильника
Switch sw1;
Light lamp;
Generator gen;
gen.connect("A2",lamp,"A1"); // Нейтраль с 1ым полюсом лампы
gen.connect("A1",lamp,"A2"); // Фаза со 2ым полюсаом лампы
gen.connect("A2",sw1,"A1"); // Нейтраль с 1ым полюсом переключателя
gen.connect("A3",sw1,"A2"); //Земля со 2ым полюсом переключателя
return 0;
}