Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmoene/a592d8b5706edafea15c to your computer and use it in GitHub Desktop.
Save martinmoene/a592d8b5706edafea15c to your computer and use it in GitHub Desktop.
FizzBuzz GUI
#include <iostream>
#include <string>
#define sometimes if
bool is( std::string text )
{
return ! text.empty();
}
std::string number( int x )
{
return std::to_string( x );
}
std::string buzz( int x )
{
return 0 == x % 5 ? "Buzz":"";
}
std::string fizz( int x )
{
return 0 == x % 3 ? "Fizz":"";
}
std::string fizz_or_buzz( int x )
{
return fizz(x) + buzz(x);
}
std::string fizzbuzz( int x )
{
return is( fizz_or_buzz( x ) ) ? fizz_or_buzz( x ) : number( x );
}
bool enough( std::ostream & os, int x )
{
os << fizzbuzz( x ) << " "; return x != 99;
}
void is_enough( std::ostream & os, int x = 1 )
{
sometimes ( enough( os, x ) ) is_enough( os, x + 1 );
}
#ifdef LEST
#include "lest.hpp"
bool can_be_divided_by_3( int x ) { return 0 == x % 3; }
bool can_be_divided_by_5( int x ) { return 0 == x % 5; }
bool can_be_divided_by_3_not_5( int x ) { return can_be_divided_by_3( x ) && ! can_be_divided_by_5( x ); }
bool can_be_divided_by_5_not_3( int x ) { return can_be_divided_by_5( x ) && ! can_be_divided_by_3( x ); }
bool can_be_divided_by_3_and_5( int x ) { return can_be_divided_by_3( x ) && can_be_divided_by_5( x ); }
bool cannot_be_divided_by_3_or_5( int x ) { return ! can_be_divided_by_3( x ) && ! can_be_divided_by_5( x ); }
auto range( int first, int last, std::function<bool(int)> pred ) -> std::vector<int>
{
std::vector<int> result;
for ( int i = first; i != last; ++i )
if ( pred( i ) )
result.push_back( i );
return result;
}
const lest::test specification[] =
{
CASE( "A number itself is produced if it cannot be divided by 3 or 5" )
{
for ( auto x : range( 1, 100, cannot_be_divided_by_3_or_5 ) )
{
EXPECT( std::to_string( x ) == fizzbuzz( x ) );
};
},
CASE( "Fizz is produced for numbers that can be divided by 3 but not by 5" )
{
for ( auto x : range( 1, 100, can_be_divided_by_3_not_5 ) )
{
EXPECT( x > 0 );
EXPECT( "Fizz" == fizzbuzz( x ) );
};
},
CASE( "Buzz is produced for numbers that can be divided by 5 but not by 3" )
{
for ( auto x : range( 1, 100, can_be_divided_by_5_not_3 ) )
{
EXPECT( x > 0 );
EXPECT( "Buzz" == fizzbuzz( x ) );
};
},
CASE( "FizzBuzz is produced for numbers that can be divided by 3 and 5" )
{
for ( auto x : range( 1, 100, can_be_divided_by_3_and_5 ) )
{
EXPECT( x > 0 );
EXPECT( "FizzBuzz" == fizzbuzz( x ) );
};
},
};
int main( int argc, char * argv[] )
{
return lest::run( specification, argc, argv );
}
#elif defined( GUI )
#include <sstream>
#include <windows.h>
int main()
{
std::ostringstream os; is_enough( os );
MessageBox( nullptr, os.str().c_str(), "FizzBuzz GUI", MB_ICONSTOP | MB_OK );
}
#else
int main()
{
is_enough( std::cout );
}
#endif // TEST
// g++ -Wall -std=c++11 -o main.exe main.cpp && main
// g++ -DGUI -Wall -std=c++11 -o main.exe main.cpp && main
// g++ -DLEST -Wall -std=c++11 -o main.exe main.cpp && main
@paulburlumi
Copy link

s/devided/divided/g

@martinmoene
Copy link
Author

@paulburlumi Thanks! Corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment