Guest User

FIZZBUZZZZ

a guest
Apr 25th, 2012
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const char FIZZ = 1;
  4. const char BUZZ = 2;
  5. const char FIZZBUZZ = 3;
  6. const char NONE = 0;
  7.  
  8. template<char fizz_flag, unsigned N>
  9. struct FBResult;
  10.  
  11. // FIZZ
  12. template<unsigned N> struct FBResult<FIZZ, N>
  13. {
  14.   enum { size = 5 };
  15.   char data[size];
  16.   constexpr FBResult() : data({ 'F','i','z','z','\n' }) {}
  17. };
  18.  
  19. // BUZZ
  20. template<unsigned N> struct FBResult<BUZZ, N>
  21. {
  22.   enum { size = 5 };
  23.   char data[size];
  24.   constexpr FBResult() : data({ 'B','u','z','z','\n' }) {}
  25. };
  26.  
  27. // FIZZBUZZ
  28. template<unsigned N> struct FBResult<FIZZBUZZ, N>
  29. {
  30.   enum { size = 9 };
  31.   char data[size];
  32.   constexpr FBResult() : data({ 'F','i','z','z','B','u','z','z','\n' }) {}
  33. };
  34.  
  35. // NUMBER
  36. template<unsigned N> struct FBResult<NONE, N>
  37. {
  38.   enum { size = 5 };
  39.   char data[size];
  40.   constexpr FBResult() : data({ N/1000 + 48, N/100 + 48, N/10 + 48, N%10 + 48, '\n' }) {}
  41. };
  42.  
  43. template <unsigned N>
  44. struct FizzFlag
  45. {
  46.   enum { value = (0x0000 | ((N % 3 == 0) * FIZZ) | ((N % 5 == 0) * BUZZ)) };
  47. };
  48.  
  49. template<unsigned N, unsigned c>
  50. struct FizzBuzzChar
  51. {
  52.   enum { value = FBResult<FizzFlag<N>::value, N>().data[c] };
  53. };
  54.  
  55. template<unsigned N>
  56. struct FizzStringSize
  57. {
  58.   enum { value = FBResult<FizzFlag<N>::value, N>::size };
  59. };
  60.  
  61. template<char... args>
  62. struct CompileTimeArray
  63. {
  64.   char data[sizeof...(args)];
  65.   constexpr CompileTimeArray() : data({args...}) {}
  66. };
  67.  
  68. template<unsigned C, unsigned N, char... args>
  69. struct generate_array_impl
  70. {
  71.   typedef typename generate_array_impl<C-1, N, FizzBuzzChar<N,C>::value, args...>::result result;
  72. };
  73.  
  74. template<unsigned N, char... args>
  75. struct generate_array_impl<0, N, args...>
  76. {
  77.   typedef typename generate_array_impl<FizzStringSize<N-1>::value - 1, N-1, FizzBuzzChar<N,0>::value, args...>::result result;
  78. };
  79.  
  80. template<char... args>
  81. struct generate_array_impl<0, 1, args...>
  82. {
  83.   typedef CompileTimeArray< FizzBuzzChar<1,0>::value, args...> result;
  84. };
  85.  
  86. template<unsigned N>
  87. struct GenerateFizzBuzzArray
  88. {
  89.   typedef typename generate_array_impl<FizzStringSize<N-1>::value - 1, N-1, '\0'>::result result;
  90. };
  91.  
  92. int main(void)
  93. {
  94.   const unsigned count = 100;
  95.   typedef GenerateFizzBuzzArray<count>::result A;
  96.  
  97.   for(size_t i = 0; i < sizeof(A().data); ++i)
  98.   {
  99.     std::cout << A().data[i];
  100.   }
  101.  
  102.   return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment