Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm> // std::swap
- #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 ); }
- struct Options
- {
- bool append; // Instead of prepend.
- bool reflect; // Reflect second half of series each time.
- Options(): append(), reflect() {}
- Options( int const argc, char const* const argv[] )
- : append()
- , reflect()
- {
- for( int i = 1; i < argc; ++i )
- {
- if( strcmp( argv[i], "-append" ) == 0 )
- {
- append = true;
- }
- else if( strcmp( argv[i], "-reflect" ) == 0 )
- {
- reflect = true;
- }
- else
- {
- throwX( string() + "Usage: " + argv[0] + " [-reflect][-append]" );
- }
- }
- }
- };
- vector<wstring> extended( vector<wstring> const& patterns, Options const& opt )
- {
- int const n = countOf( patterns );
- vector<wstring> result;
- for( int i = 0; i < n; ++i )
- {
- result += (opt.append? patterns[i] + L'0' : L'0' + patterns[i]);
- }
- int const iStart = (opt.reflect? n - 1 : 0);
- int const iEnd = (opt.reflect? 0 - 1 : n);
- int const iDelta = (opt.reflect? -1 : +1);
- for( int i = iStart; i != iEnd; i += iDelta )
- {
- result += (opt.append? patterns[i] + L'1' : L'1' + patterns[i]);
- }
- 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