alfps

hm de dum

May 8th, 2012
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <algorithm>    // std::swap
  2. #include <bitset>       // std::bitset
  3. #include <iomanip>      // std::setw
  4. #include <iostream>     // std::wcout, std::endl
  5. #include <stddef.h>     // ptrdiff_t
  6. #include <stdexcept>    // std::exception, std::runtime_error
  7. #include <stdlib.h>     // EXIT_SUCCESS, EXIT_FAILURE
  8. #include <string>       // std::wstring
  9. #include <utility>      // std::begin, std::end
  10. #include <vector>       // std::vector
  11.  
  12. using namespace std;
  13. typedef ptrdiff_t   Size;
  14.  
  15. bool throwX( string const& s ) { throw runtime_error( s ); }
  16.  
  17. template< class Elem >
  18. void operator+=( vector<Elem>& v, Elem&& newElem )
  19. {
  20.     v.emplace_back( move( newElem ) );
  21. }
  22.  
  23. template< class Container >
  24. Size countOf( Container const& c ) { return end( c ) - begin( c ); }
  25.  
  26. struct Options
  27. {
  28.     bool    append;     // Instead of prepend.
  29.     bool    reflect;    // Reflect second half of series each time.
  30.  
  31.     Options(): append(), reflect() {}
  32.  
  33.     Options( int const argc, char const* const argv[] )
  34.         : append()
  35.         , reflect()
  36.     {
  37.         for( int i = 1;  i < argc;  ++i )
  38.         {
  39.             if( strcmp( argv[i], "-append" ) == 0 )
  40.             {
  41.                 append = true;
  42.             }
  43.             else if( strcmp( argv[i], "-reflect" ) == 0 )
  44.             {
  45.                 reflect = true;
  46.             }
  47.             else
  48.             {
  49.                 throwX( string() + "Usage: " + argv[0] + " [-reflect][-append]" );
  50.             }
  51.         }
  52.     }
  53. };
  54.  
  55. vector<wstring> extended( vector<wstring> const& patterns, Options const& opt )
  56. {
  57.     int const           n   = countOf( patterns );
  58.     vector<wstring>      result;
  59.  
  60.     for( int i = 0; i < n; ++i )
  61.     {
  62.         result += (opt.append? patterns[i] + L'0' : L'0' + patterns[i]);
  63.     }
  64.    
  65.     int const   iStart  = (opt.reflect? n - 1 : 0);
  66.     int const   iEnd    = (opt.reflect? 0 - 1 : n);
  67.     int const   iDelta  = (opt.reflect? -1 : +1);
  68.  
  69.     for( int i = iStart; i != iEnd; i += iDelta )
  70.     {
  71.         result += (opt.append? patterns[i] + L'1' : L'1' + patterns[i]);
  72.     }
  73.     return result;
  74. }
  75.  
  76. void display( vector<wstring> const& patterns )
  77. {
  78.     for( auto it = begin( patterns); it != end( patterns ); ++it )
  79.     {
  80.         if( it > begin( patterns ) ) { wcout << L" "; }
  81.         wcout << *it;
  82.     }
  83.     wcout << endl;
  84. }
  85.  
  86. void cppMain( Options const& options )
  87. {
  88.     vector<wstring>     patterns( 1, L"" );
  89.  
  90.     for( int i = 1;  i <= 3;  ++i )
  91.     {
  92.         patterns = extended( patterns, options );
  93.         display( patterns );
  94.     }
  95. }
  96.  
  97. int main( int argc, char* argv[] )
  98. {
  99.     try
  100.     {
  101.         cppMain( Options( argc, argv ) );
  102.         return EXIT_SUCCESS;
  103.     }
  104.     catch( exception const& x )
  105.     {
  106.         wcerr << "!" << x.what() << endl;
  107.     }
  108.     return EXIT_FAILURE;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment