-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_step.cpp
More file actions
128 lines (118 loc) · 1.69 KB
/
input_step.cpp
File metadata and controls
128 lines (118 loc) · 1.69 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
//
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
//
// sample step input
//
struct step
{
int v;
int c;
};
std::vector<step> step_list = {{1, 4}, {0, 2}, {1, 3}, {0, 5}, {1, 2}, {0, 1}, {1, 2}, {0, 1}};
namespace
{
int s_cnt = 0;
int c_cnt = 0;
std::pair<int, bool>
getInput()
{
bool result = true;
auto si = step_list[s_cnt];
if (c_cnt >= si.c - 1)
{
if (s_cnt < step_list.size() - 1)
{
s_cnt++;
c_cnt = 0;
}
else
result = false;
si = step_list[s_cnt];
}
else
c_cnt++;
return std::make_pair(si.v, result);
}
} // namespace
//
// on/off switch update
//
struct OnOffSwitch
{
enum class State : int
{
Off,
Hold,
On
};
bool on = false;
State state = State::Off;
int count = 0;
void On() { on = true; }
void Off() { on = false; }
void update()
{
bool hold = false;
if (count)
{
hold = true;
--count;
}
if (!count)
{
if (on)
{
count = 4;
state = State::On;
}
else if (hold)
state = State::Off;
}
else
state = State::Hold;
}
const char* getStete() const
{
const char* result = "Unknown";
switch (state)
{
case State::On:
result = "On";
break;
case State::Off:
result = "Off";
break;
case State::Hold:
result = "Hold";
break;
}
return result;
}
};
//
// main
//
int
main()
{
OnOffSwitch oos;
for (int i = 0; i < 30; ++i)
{
auto n = getInput();
if (n.first)
oos.On();
else
oos.Off();
oos.update();
printf("input: %d %s\n", n.first, oos.getStete());
}
return 0;
}
//
//
//