Guest User

Untitled

a guest
Oct 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. //
  2. // Created by Ajay Guru on 10/17/17.
  3. //
  4.  
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. template<bool small, int I>
  11. struct pretty_printer;
  12.  
  13. #define SMALL_PRETTY_PRINTER(num, string) \
  14. template<>\
  15. struct pretty_printer<true, num>\
  16. {\
  17. static void print()\
  18. {\
  19. cout << string;\
  20. }\
  21. };
  22.  
  23. SMALL_PRETTY_PRINTER(0, "No")
  24. SMALL_PRETTY_PRINTER(1, "One")
  25. SMALL_PRETTY_PRINTER(2, "Two")
  26. SMALL_PRETTY_PRINTER(3, "Three")
  27. SMALL_PRETTY_PRINTER(4, "Four")
  28. SMALL_PRETTY_PRINTER(5, "Five")
  29. SMALL_PRETTY_PRINTER(6, "Six")
  30. SMALL_PRETTY_PRINTER(7, "Seven")
  31. SMALL_PRETTY_PRINTER(8, "Eight")
  32. SMALL_PRETTY_PRINTER(9, "Nine")
  33. SMALL_PRETTY_PRINTER(10, "Ten")
  34. SMALL_PRETTY_PRINTER(11, "Eleven")
  35. SMALL_PRETTY_PRINTER(12, "Twelve")
  36. SMALL_PRETTY_PRINTER(13, "Thirteen")
  37. SMALL_PRETTY_PRINTER(14, "Fourteen")
  38. SMALL_PRETTY_PRINTER(15, "Fifteen")
  39. SMALL_PRETTY_PRINTER(16, "Sixteen")
  40. SMALL_PRETTY_PRINTER(17, "Seventeen")
  41. SMALL_PRETTY_PRINTER(18, "Eighteen")
  42. SMALL_PRETTY_PRINTER(19, "Nineteen")
  43.  
  44. #undef SMALL_PRETTY_PRINTER
  45.  
  46. template<int ones>
  47. inline void
  48. print_ones();
  49.  
  50. #define ONES_PRINTER(ones, string) \
  51. template<> \
  52. inline void \
  53. print_ones<ones>() \
  54. {\
  55. cout << string;\
  56. }
  57.  
  58. ONES_PRINTER(0, " ")
  59. ONES_PRINTER(1, " one")
  60. ONES_PRINTER(2, " two")
  61. ONES_PRINTER(3, " three")
  62. ONES_PRINTER(4, " four")
  63. ONES_PRINTER(5, " five")
  64. ONES_PRINTER(6, " six")
  65. ONES_PRINTER(7, " seven")
  66. ONES_PRINTER(8, " eight")
  67. ONES_PRINTER(9, " nine")
  68.  
  69. #undef ONES_PRINTER
  70.  
  71. template<int tens>
  72. inline void
  73. print_tens();
  74.  
  75. #define TENS_PRINTER(tens, string) \
  76. template<> \
  77. inline void \
  78. print_tens<tens>() \
  79. {\
  80. cout << string;\
  81. }
  82.  
  83. TENS_PRINTER(2, "Twenty")
  84. TENS_PRINTER(3, "Thirty")
  85. TENS_PRINTER(4, "Forty")
  86. TENS_PRINTER(5, "Fifty")
  87. TENS_PRINTER(6, "Sixty")
  88. TENS_PRINTER(7, "Seventy")
  89. TENS_PRINTER(8, "Eighty")
  90. TENS_PRINTER(9, "Ninety")
  91.  
  92. #undef TENS_PRINTER
  93.  
  94. template<int I>
  95. struct pretty_printer<false, I>
  96. {
  97. static void print(){
  98. print_tens<(I - I%10)/10>();
  99. print_ones<(I%10)>();
  100. }
  101. };
  102.  
  103. template<int I>
  104. void pretty_print()
  105. {
  106. pretty_printer<(I<20), I>::print();
  107. }
  108.  
  109. template<int I>
  110. inline void
  111. BottlesOfBeer()
  112. {
  113. pretty_print<I>();
  114. cout << " bottles of beer" ;
  115. }
  116.  
  117. template<>
  118. inline void
  119. BottlesOfBeer<1>()
  120. {
  121. pretty_print<1>();
  122. cout << " bottle of beer" ;
  123. }
  124.  
  125. template<int I>
  126. inline void
  127. BottlesOfBeerOnTheWall()
  128. {
  129. BottlesOfBeer<I>();
  130. cout << " on the wall";
  131. }
  132.  
  133. template<int I>
  134. inline void stanza()
  135. {
  136. BottlesOfBeerOnTheWall<I>();
  137. cout << ",\n";
  138. BottlesOfBeer<I>();
  139. cout <<",\n";
  140. }
  141.  
  142. template<int I>
  143. inline void bridge()
  144. {
  145. cout << "Take one down, pass it around," << endl;
  146. BottlesOfBeerOnTheWall<I-1>();
  147. cout <<",\n";
  148. }
  149.  
  150. template<>
  151. inline void bridge<0>()
  152. {
  153. cout << "Go to the store and buy some more," << endl;
  154. BottlesOfBeerOnTheWall<99>();
  155. }
  156.  
  157. template<int I>
  158. inline void verse()
  159. {
  160. stanza<I>();
  161. bridge<I>();
  162. }
  163.  
  164. template<int I>
  165. inline void sing ()
  166. {
  167. verse<I>();
  168. cout << endl;
  169. sing<I-1>();
  170. }
  171.  
  172.  
  173. template<>
  174. inline void sing<0> ()
  175. {
  176. verse<0>();
  177. }
  178.  
  179. int main () {
  180. sing<99>();
  181. }
Add Comment
Please, Sign In to add comment