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: public:
Counter (int i, int up); Counter (unsigned int i, unsigned int up);
void inc (); void inc ();
void dec (); void dec ();
int getCurrent (); unsigned int getCurrent ();
void setInit (int i); void setInit (unsigned int i);
~Counter () = default; ~Counter () = default;
private: private:
int init; unsigned int init;
int up_lim; unsigned int up_lim;
unsigned int down_lim;
}; };
#endif /* COUNTER_H_ */ #endif /* COUNTER_H_ */

View File

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