1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <type_traits>
template <class N=int>
class basic_range
{
public:
typedef N number_type;
static_assert ( std::is_integral<number_type>::value ||
std::is_floating_point<number_type>::value,
"basic_range is only for numeric types" );
class iterator
{
private:
basic_range r;
number_type current;
friend class basic_range;
iterator ( basic_range r, number_type current ) :
r ( r ), current ( current ) {}
public:
iterator() : iterator ( {0,0}, 0, 0 ) {}
N operator* () const { return current; }
operator N () const { return current; }
iterator& operator+= ( int n )
{
current += n*r.step();
if ( current > r.last() )
current = r.last();
return *this;
}
iterator& operator-= ( int n )
{
current -= n*r.step();
if ( current < r.first() )
current = r.last();
return *this;
}
iterator& operator++ ()
{
return *this += 1;
}
iterator& operator-- ()
{
return *this -= 1;
}
iterator operator++ (int)
{
iterator copy;
*this += 1;
return copy;
}
iterator operator-- (int)
{
iterator copy;
*this -= 1;
return copy;
}
};
protected:
number_type nbegin, nend, nstep;
public:
basic_range ( number_type begin, number_type end, number_type step = 1 )
: nbegin ( begin < end ? begin : end ),
nend ( begin < end ? end : begin ),
nstep ( step ) {}
number_type first() const { return nbegin; }
number_type last() const { return nend; }
number_type step() const { return nstep; }
void first ( number_type n ) const { nbegin = n; }
void last ( number_type n ) const { nend = n; }
void step ( number_type n ) const { nstep = n; }
iterator begin () const
{
return iterator(*this,nbegin);
}
iterator end () const
{
return iterator(*this,nend);
}
iterator rbegin () const
{
return iterator(basic_range(nbegin,nend,-std::make_signed<number_type>(step)),nend);
}
iterator rend () const
{
return iterator(basic_range(nbegin,nend,-std::make_signed<number_type>(step)),nbegin);
}
};
template < class N >
basic_range<N> range ( N begin, N end, N step = 1 )
{
return basic_range<N>(begin,end,step);
}
template<class charT=char,class Traits=std::char_traits<charT>,class Numb=unsigned>
class basic_buzzer
{
static_assert ( std::is_integral<Numb>::value,
"basic_buzzer is only for integral types" );
public:
typedef std::map<unsigned,std::basic_string<charT,Traits>> map_type;
typedef basic_range<Numb> range_type;
protected:
range_type range;
std::map<unsigned,std::basic_string<charT,Traits>> buzz;
public:
basic_buzzer ( range_type range, std::map<unsigned,std::string> buzz )
: range(range), buzz(buzz) {}
basic_buzzer ( unsigned min, unsigned max, std::map<unsigned,std::string> buzz )
: basic_buzzer({min,max},buzz) {}
basic_buzzer ( unsigned min, unsigned max )
: basic_buzzer(min, max, {{3,"Fizz"},{5,"Buzz"}}) {}
basic_buzzer ( unsigned max, std::map<unsigned,std::string> buzz )
: basic_buzzer(1, max, buzz) {}
basic_buzzer ()
: basic_buzzer(1, 100) {}
friend std::basic_ostream<charT,Traits>& operator<< (
std::basic_ostream<charT,Traits>& out,
const basic_buzzer& buzz )
{
for ( auto i : buzz.range )
{
std::basic_string<charT,Traits> bzz;
for ( const auto& p : buzz.buzz )
if ( ( i % p.first ) == 0 )
bzz += p.second;
if ( bzz.empty() )
out << i << std::endl;
else
out << bzz << std::endl;
}
return out;
}
};
typedef basic_buzzer<char> buzzer;
buzzer operator "" _buzz ( unsigned long long n )
{
return buzzer ( 1, n );
}
buzzer operator "" _buzz ( const char* str, long unsigned sz )
{
std::istringstream ss ( std::string(str,sz) );
unsigned start = 1;
unsigned end = 1;
char la =';';
ss >> start >> la;
if ( la == '-' )
{
ss >> end >> la;
}
else
std::swap ( start, end );
if ( la == ';' )
{
buzzer::map_type map;
std::pair<unsigned,std::string> p;
while ( true )
{
ss >> p.first >> la;
if ( !ss || la != ':' )
break;
getline(ss,p.second,',');
map.insert(p);
}
return buzzer ( start, end, map );
}
return buzzer ( start, end );
}
int main()
{
std::cout << "1-100;3:Fizz,5:Buzz"_buzz;
return 0;
}
|