1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//==========================================================
vector<string> octagon( int side, char top, char bottom ) // side should be even
{
int height = 3 * side;
vector<string> result( height );
for ( int i = 1, letters = side; i <= side; i++, letters += 2 )
{
string blanks( side - i + 1, ' ' );
result[i ] = blanks + string( letters, top ) + blanks;
result[height - 1 - i] = blanks + string( letters, bottom ) + blanks;
}
for ( int i = side + 1; i <= side + side / 2 - 1; i++ )
{
result[i ] = result[side ];
result[height - 1 - i] = result[height-1-side];
}
return result;
}
//==========================================================
void wallpaper( string title, const vector<string> &shape, int vertical, int horizontal )
{
int height = shape.size();
cout << title << '\n';
for ( int v = 0; v < vertical; v++ )
{
for ( int i = 0; i < height; i++ )
{
for ( int h = 0; h < horizontal; h++ ) cout << shape[i];
cout << '\n';
}
}
}
//==========================================================
int main()
{
wallpaper( "4 octagons vertically" , octagon( 2, 'A', 'C' ), 4, 1 );
wallpaper( "\n\n4 octagons horizontally", octagon( 2, 'A', 'C' ), 1, 4 );
wallpaper( "\n\n2 x 3 large octagons" , octagon( 4, '*', 'O' ), 2, 3 );
}
|