/* */ #ifndef _D_ALPHA_NUMBER_DECORATOR_H_ #define _D_ALPHA_NUMBER_DECORATOR_H_ #include "NumberDecorator.h" #include "FatalException.h" class AlphaNumberDecorator : public NumberDecorator { private: int32_t _width; string _zero; string widen(const string& s, int32_t width) { string t = s; while(t.size() < (size_t)width) { t.insert(0, _zero); } return t; } public: AlphaNumberDecorator(int32_t width, bool uppercase = false): _width(width), _zero(uppercase?"A":"a") {} virtual ~AlphaNumberDecorator() {} virtual string decorate(int32_t number) { if(number < 0) { throw new FatalException("The number must be greater than 0."); } if(number == 0) { return widen(_zero, _width); } int32_t base = 26; string x; while(number > 0) { int32_t r = number%base; char alpha = _zero[0]+r; x.insert(0, string(1, alpha)); number /= base; } return widen(x, _width); } }; #endif // _D_ALPHA_NUMBER_DECORATOR_H_