Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm> // std::for_each
- #include <bitset> // std::bitset
- #include <iomanip> // std::setw
- #include <iostream> // std::wcout, std::endl
- #include <stddef.h> // ptrdiff_t
- #include <stdexcept> // std::exception, std::runtime_error
- #include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
- #include <string> // std::wstring
- #include <utility> // std::begin, std::end
- #include <vector> // std::vector
- using namespace std;
- typedef ptrdiff_t Size;
- bool throwX( string const& s ) { throw runtime_error( s ); }
- template< class Elem >
- void operator+=( vector<Elem>& v, Elem&& newElem )
- {
- v.emplace_back( move( newElem ) );
- }
- template< class Container >
- Size countOf( Container const& c ) { return end( c ) - begin( c ); }
- namespace growDirection { enum Enum{ prepend, append }; }
- namespace patternType { enum Enum { pure, gray }; }
- struct Options
- {
- growDirection::Enum direction;
- patternType::Enum type;
- Options(): direction(), type() {}
- Options( int const argc, char const* const argv[] )
- : direction()
- , type()
- {
- for( int i = 1; i < argc; ++i )
- {
- if( strcmp( argv[i], "-append" ) == 0 )
- {
- direction = growDirection::append;
- }
- else if( strcmp( argv[i], "-reflect" ) == 0 )
- {
- type = patternType::gray;
- }
- else
- {
- throwX( string() + "Usage: " + argv[0] + " [-reflect][-append]" );
- }
- }
- }
- };
- template< class C >
- auto rbegin( C& c ) -> decltype( c.rbegin() ) { return c.rbegin(); }
- template< class C >
- auto rend( C& c ) -> decltype( c.rend() ) { return c.rend(); }
- template< class It >
- void addExtendedStringsTo(
- vector<wstring>& result,
- wchar_t const d,
- It const itSourceBegin,
- It const itSourceEnd,
- growDirection::Enum direction
- )
- {
- for_each( itSourceBegin, itSourceEnd, [&]( wstring const& s )
- {
- result += (direction == growDirection::append? s + d : d + s);
- } );
- }
- vector<wstring> extended( vector<wstring> const& patterns, Options const& opt )
- {
- vector<wstring> result;
- addExtendedStringsTo(
- result, L'0', begin( patterns ), end( patterns ), opt.direction
- );
- switch( opt.type )
- {
- case patternType::pure:
- addExtendedStringsTo(
- result, L'1', begin( patterns ), end( patterns ), opt.direction
- );
- break;
- case patternType::gray:
- addExtendedStringsTo(
- result, L'1', rbegin( patterns ), rend( patterns ), opt.direction
- );
- }
- return result;
- }
- void display( vector<wstring> const& patterns )
- {
- for( auto it = begin( patterns); it != end( patterns ); ++it )
- {
- if( it > begin( patterns ) ) { wcout << L" "; }
- wcout << *it;
- }
- wcout << endl;
- }
- void cppMain( Options const& options )
- {
- vector<wstring> patterns( 1, L"" );
- for( int i = 1; i <= 3; ++i )
- {
- patterns = extended( patterns, options );
- display( patterns );
- }
- }
- int main( int argc, char* argv[] )
- {
- try
- {
- cppMain( Options( argc, argv ) );
- return EXIT_SUCCESS;
- }
- catch( exception const& x )
- {
- wcerr << "!" << x.what() << endl;
- }
- return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement