Merge Counter: fix #18 #22 from vas-dav/counter

Counter: fix #18
This commit is contained in:
Vasily Davydov 2022-10-16 15:40:32 +03:00 committed by GitHub
commit 511494f8d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 12 deletions

View File

@ -12,15 +12,16 @@ class Counter
{
public:
Counter (int i, int up);
Counter (unsigned int i, unsigned int up);
void inc ();
void dec ();
int getCurrent ();
void setInit (int i);
unsigned int getCurrent ();
void setInit (unsigned int i);
~Counter () = default;
private:
int init;
int up_lim;
unsigned int init;
unsigned int up_lim;
unsigned int down_lim;
};
#endif /* COUNTER_H_ */

View File

@ -19,37 +19,38 @@ Counter::inc ()
void
Counter::dec ()
{
if (init - 1 > 0)
if (init > down_lim)
{
--init;
}
}
int
unsigned int
Counter::getCurrent ()
{
return this->init;
}
Counter::Counter (int i, int up)
Counter::Counter (unsigned int down, unsigned int up)
{
up_lim = up;
if (i > up)
down_lim = down;
if (down > up)
{
init = up;
}
else if (i < 0)
else if (down < 0)
{
init = 0;
}
else
{
init = i;
init = down;
}
}
void
Counter::setInit (int i)
Counter::setInit (unsigned int i)
{
init = i;
}