From 053ecdcdd4315c98f3b06788c39833323896d0c3 Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Tue, 15 May 2012 17:22:21 +0900 Subject: [PATCH] =?UTF-8?q?example=20=E3=81=AB=20FizzBuzz=20=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/fizzbuzz/main.cpp | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 example/fizzbuzz/main.cpp diff --git a/example/fizzbuzz/main.cpp b/example/fizzbuzz/main.cpp new file mode 100644 index 00000000..e287d27a --- /dev/null +++ b/example/fizzbuzz/main.cpp @@ -0,0 +1,99 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/osyo-manga/cpp-half/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +#include +#include +#include +#include + + +struct fizzbuzz{ + typedef sprout::string<12> result_type; + + constexpr result_type + operator ()(int n) const{ + return n % 15 == 0 ? sprout::to_string("FizzBuzz") + : n % 3 == 0 ? sprout::to_string("Fizz") + : n % 5 == 0 ? sprout::to_string("Buzz") + : sprout::to_string(n); + } +}; + + +int +main(){ + typedef fizzbuzz::result_type string; + + // + // Test + // + static_assert(fizzbuzz()( 1) == "1", ""); + static_assert(fizzbuzz()( 2) == "2", ""); + static_assert(fizzbuzz()( 3) == "Fizz", ""); + static_assert(fizzbuzz()( 5) == "Buzz", ""); + static_assert(fizzbuzz()(15) == "FizzBuzz", ""); + + // + // Sequence [1..15] + // + constexpr auto source = sprout::iota( + sprout::pit >(), + 1 + ); + + // + // Transform to FizzBuzz + // + constexpr auto result = sprout::transform( + sprout::begin(source), + sprout::end(source), + sprout::pit >(), + fizzbuzz() + ); + + // + // Check result + // + constexpr auto fizzbuzz_result = sprout::make_array( + sprout::to_string("1"), + sprout::to_string("2"), + sprout::to_string("Fizz"), + sprout::to_string("4"), + sprout::to_string("Buzz"), + sprout::to_string("Fizz"), + sprout::to_string("7"), + sprout::to_string("8"), + sprout::to_string("Fizz"), + sprout::to_string("Buzz"), + sprout::to_string("11"), + sprout::to_string("Fizz"), + sprout::to_string("13"), + sprout::to_string("14"), + sprout::to_string("FizzBuzz") + ); + // Equal result sequence + static_assert(result == fizzbuzz_result, ""); + + // + // Output + // + for(auto&& str : result){ + std::cout << str << ", "; + } + std::cout << std::endl; + + return 0; +}