Want more features on Pastebin? Sign Up, it's FREE!
Guest

/r/dailyprogrammer Challenge 119 - Rup-Inc's solution

By: a guest on Jan 28th, 2013  |  syntax: C++  |  size: 4.34 KB  |  views: 98  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2.  
  3. template <bool Condition, typename TrueResult, typename FalseResult>
  4. class if_;
  5.  
  6. template <typename TrueResult, typename FalseResult>
  7. struct if_<true, TrueResult, FalseResult>
  8. {
  9.         typedef TrueResult result;
  10. };
  11.  
  12. template <typename TrueResult, typename FalseResult>
  13. struct if_<false, TrueResult, FalseResult>
  14. {
  15.         typedef FalseResult result;
  16. };
  17.  
  18.  
  19. template <int dolars, int cents, int cent25 = 0, int cent10 = 0, int cent5 = 0, int cent1 = 0>
  20. struct coinConverter{
  21.         enum {
  22.                 Cent25 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent25,
  23.                 Cent10 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent10,
  24.                 Cent5 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent5,
  25.                 Cent1 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent1,
  26.         };
  27. };
  28.  
  29. template <int cents, int cent25, int cent10, int cent5, int cent1>
  30. struct coinConverter<0, cents, cent25, cent10, cent5, cent1> {
  31.         enum {
  32.                 Cent25 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
  33.                         if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
  34.                                 if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
  35.                                         coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent25,
  36.                 Cent10 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
  37.                         if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
  38.                                 if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
  39.                                         coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent10,
  40.                 Cent5 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
  41.                         if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
  42.                                 if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
  43.                                         coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent5,
  44.                 Cent1 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
  45.                         if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
  46.                                 if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
  47.                                         coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent1,
  48.         };
  49. };
  50.  
  51. template <int cent25, int cent10, int cent5, int cent1>
  52. struct coinConverter<0, 0, cent25, cent10, cent5, cent1> {
  53.         enum {
  54.                 Cent25 = cent25,
  55.                 Cent10 = cent10,
  56.                 Cent5 = cent5,
  57.                 Cent1 = cent1,
  58.         };
  59. };
  60.  
  61. #define CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(n) \
  62.         template <int cent25, int cent10, int cent5, int cent1> \
  63.         struct coinConverter<0, ## n ##, cent25, cent10, cent5, cent1> {\
  64.         enum { Dolars = 0, Cents = 0, Cent25 = 0, Cent10 = 0, Cent5 = 0, Cent1 = 0 }; };
  65.  
  66. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-1)
  67. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-2)
  68. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-3)
  69. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-4)
  70. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-5)
  71. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-6)
  72. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-7)
  73. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-8)
  74. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-9)
  75. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-10)
  76. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-11)
  77. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-12)
  78. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-13)
  79. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-14)
  80. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-15)
  81. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-16)
  82. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-17)
  83. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-18)
  84. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-19)
  85. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-20)
  86. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-21)
  87. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-22)
  88. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-23)
  89. CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-24)
  90.  
  91. int main() {
  92.         typedef coinConverter<0, 6> result;
  93.         if (result::Cent25) std::cout << "$0.25 : " << result::Cent25 << std::endl;
  94.         if (result::Cent10) std::cout << "$0.10 : " << result::Cent10 << std::endl;
  95.         if (result::Cent5)  std::cout << "$0.05 : " << result::Cent5 << std::endl;
  96.         if (result::Cent1)  std::cout << "$0.01 : " << result::Cent1 << std::endl;
  97. }
clone this paste RAW Paste Data