state-handler: counter: modify limits

This commit is contained in:
Vasily Davydov 2022-10-14 13:58:30 +03:00
parent acb1b73b9a
commit 1b4c37b9c5
2 changed files with 42 additions and 32 deletions

View File

@ -8,19 +8,19 @@
#ifndef COUNTER_H_ #ifndef COUNTER_H_
#define COUNTER_H_ #define COUNTER_H_
class Counter { class Counter
{
public: public:
Counter(int i, int up); Counter (int i, int up);
void inc(); void inc ();
void dec(); void dec ();
int getCurrent(); int getCurrent ();
void setInit(int i); void setInit (int i);
~Counter() = default; ~Counter () = default;
private: private:
int init; int init;
int up_lim; int up_lim;
}; };
#endif /* COUNTER_H_ */ #endif /* COUNTER_H_ */

View File

@ -7,39 +7,49 @@
#include "Counter.h" #include "Counter.h"
void Counter::inc() { void
if(init >= up_lim){ Counter::inc ()
init = 0; {
} else{ if (init < up_lim)
++init; {
++init;
} }
} }
void Counter::dec() { void
if(init <= 0){ Counter::dec ()
init = up_lim; {
} else{ if (init - 1 > 0)
--init; {
--init;
} }
} }
int
int Counter::getCurrent(){ Counter::getCurrent ()
{
return this->init; return this->init;
} }
Counter::Counter(int i, int up) { Counter::Counter (int i, int up)
up_lim = up; {
if(i > up){ up_lim = up;
init = up; if (i > up)
}else if(i < 0){ {
init = 0; init = up;
}else{ }
init = i; else if (i < 0)
{
init = 0;
}
else
{
init = i;
} }
} }
void Counter::setInit(int i){ void
Counter::setInit (int i)
{
init = i; init = i;
} }