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,7 +8,8 @@
#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);
@ -21,6 +22,5 @@ public:
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; up_lim = up;
if(i > up){ if (i > up)
{
init = up; init = up;
}else if(i < 0){ }
else if (i < 0)
{
init = 0; init = 0;
}else{ }
else
{
init = i; init = i;
} }
} }
void Counter::setInit(int i){ void
Counter::setInit (int i)
{
init = i; init = i;
} }