Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <bool Condition, typename TrueResult, typename FalseResult>
- class if_;
- template <typename TrueResult, typename FalseResult>
- struct if_<true, TrueResult, FalseResult>
- {
- typedef TrueResult result;
- };
- template <typename TrueResult, typename FalseResult>
- struct if_<false, TrueResult, FalseResult>
- {
- typedef FalseResult result;
- };
- template <int dolars, int cents, int cent25 = 0, int cent10 = 0, int cent5 = 0, int cent1 = 0>
- struct coinConverter{
- enum {
- Cent25 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent25,
- Cent10 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent10,
- Cent5 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent5,
- Cent1 = coinConverter<dolars -1, cents, cent25 + 4, cent10, cent5, cent1>::Cent1,
- };
- };
- template <int cents, int cent25, int cent10, int cent5, int cent1>
- struct coinConverter<0, cents, cent25, cent10, cent5, cent1> {
- enum {
- Cent25 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
- if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
- if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
- coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent25,
- Cent10 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
- if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
- if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
- coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent10,
- Cent5 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
- if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
- if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
- coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent5,
- Cent1 = if_<(cents >= 25), coinConverter<0, cents - 25, cent25 + 1, cent10, cent5, cent1>,
- if_<(cents >= 10), coinConverter<0, cents - 10, cent25, cent10 + 1, cent5, cent1>,
- if_<(cents >= 5), coinConverter<0, cents - 5, cent25, cent10, cent5 + 1, cent1>,
- coinConverter<0, cents - 1, cent25, cent10, cent5, cent1 + 1>>::result>::result>::result::Cent1,
- };
- };
- template <int cent25, int cent10, int cent5, int cent1>
- struct coinConverter<0, 0, cent25, cent10, cent5, cent1> {
- enum {
- Cent25 = cent25,
- Cent10 = cent10,
- Cent5 = cent5,
- Cent1 = cent1,
- };
- };
- #define CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(n) \
- template <int cent25, int cent10, int cent5, int cent1> \
- struct coinConverter<0, ## n ##, cent25, cent10, cent5, cent1> {\
- enum { Dolars = 0, Cents = 0, Cent25 = 0, Cent10 = 0, Cent5 = 0, Cent1 = 0 }; };
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-1)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-2)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-3)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-4)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-5)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-6)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-7)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-8)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-9)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-10)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-11)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-12)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-13)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-14)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-15)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-16)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-17)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-18)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-19)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-20)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-21)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-22)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-23)
- CREATE_CATCHING_COIN_CONVERTER_TEMPLATE(-24)
- int main() {
- typedef coinConverter<0, 6> result;
- if (result::Cent25) std::cout << "$0.25 : " << result::Cent25 << std::endl;
- if (result::Cent10) std::cout << "$0.10 : " << result::Cent10 << std::endl;
- if (result::Cent5) std::cout << "$0.05 : " << result::Cent5 << std::endl;
- if (result::Cent1) std::cout << "$0.01 : " << result::Cent1 << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement