SNode.C
Loading...
Searching...
No Matches
TowerCalculator.cpp
Go to the documentation of this file.
1
/*
2
* SNode.C - a slim toolkit for network communication
3
* Copyright (C) Volker Christian <me@vchrist.at>
4
* 2020, 2021, 2022, 2023, 2024, 2025
5
*
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published
8
* by the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#
include
"TowerCalculator.h"
21
22
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
23
24
#
include
<
iostream
>
25
#
include
<
string
>
26
27
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
28
29
namespace
apps::
towercalculator
{
30
31
TowerCalculator
::
TowerCalculator
()
32
:
core
::
EventReceiver
(
"TowerCalculator"
)
33
,
currentValue
(0)
34
,
state
(
State
::
NEXT
) {
35
}
36
37
void
TowerCalculator
::
calculate
(
long
startValue) {
38
values.push_back(startValue);
39
40
calculate
(
)
;
41
}
42
43
void
TowerCalculator
::
calculate
() {
44
if
(!values.empty() && state == State::NEXT) {
45
currentValue = values.front();
46
values.pop_front();
47
48
state
=
State
::
MULTIPLY
;
49
multiplicator
= 1;
50
divisor
= 2;
51
52
std::cout << std::endl <<
"Starting calculation with value = "
<<
currentValue
<< std::endl;
53
54
span
();
55
}
56
}
57
58
void
TowerCalculator
::
onEvent
([[
maybe_unused
]]
const
utils::
Timeval
& currentTime) {
59
switch
(
state
) {
60
case
State
::
MULTIPLY
:
61
if
(
multiplicator
< 10) {
62
std::cout <<
currentValue
<<
" * "
<<
multiplicator
<<
" = "
;
63
currentValue
=
currentValue
*
multiplicator
;
64
std::cout <<
currentValue
<< std::endl;
65
multiplicator
++;
66
}
else
{
67
state
=
State
::
DIVIDE
;
68
}
69
span
();
70
break
;
71
case
State
::
DIVIDE
:
72
if
(
divisor
< 10) {
73
std::cout <<
currentValue
<<
" / "
<<
divisor
<<
" = "
;
74
currentValue
=
currentValue
/
divisor
;
75
std::cout <<
currentValue
<< std::endl;
76
divisor
++;
77
}
else
{
78
state
=
State
::
NEXT
;
79
}
80
span
();
81
break
;
82
case
State
::
NEXT
:
83
calculate
(
)
;
84
break
;
85
}
86
}
87
88
}
// namespace apps::towercalculator
apps::towercalculator::TowerCalculator
Definition
TowerCalculator.h:34
apps::towercalculator::TowerCalculator::onEvent
void onEvent(const utils::Timeval ¤tTime) override
Definition
TowerCalculator.cpp:58
apps::towercalculator::TowerCalculator::currentValue
long currentValue
Definition
TowerCalculator.h:49
apps::towercalculator::TowerCalculator::calculate
void calculate(long startValue)
Definition
TowerCalculator.cpp:37
apps::towercalculator::TowerCalculator::divisor
int divisor
Definition
TowerCalculator.h:54
apps::towercalculator::TowerCalculator::State
State
Definition
TowerCalculator.h:43
apps::towercalculator::TowerCalculator::State::MULTIPLY
@ MULTIPLY
apps::towercalculator::TowerCalculator::State::DIVIDE
@ DIVIDE
apps::towercalculator::TowerCalculator::State::NEXT
@ NEXT
apps::towercalculator::TowerCalculator::multiplicator
int multiplicator
Definition
TowerCalculator.h:53
apps::towercalculator::TowerCalculator::calculate
void calculate()
Definition
TowerCalculator.cpp:43
apps::towercalculator::TowerCalculator::state
State state
Definition
TowerCalculator.h:56
apps::towercalculator::TowerCalculator::TowerCalculator
TowerCalculator()
Definition
TowerCalculator.cpp:31
core::EventReceiver::span
void span()
Definition
EventReceiver.cpp:57
utils::Timeval
Definition
Timeval.h:36
apps::towercalculator
Definition
KeyboardReader.cpp:34
apps
towercalculator
TowerCalculator.cpp
Generated on Mon Feb 10 2025 20:20:40 for SNode.C by
1.11.0