fork download
  1. //https://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/5st2so/20170208_challenge_302_intermediate_ascii/
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. class histogram_t
  7. {
  8. public:
  9. histogram_t()
  10. : m_bar_width( 10 )
  11. {}
  12. histogram_t( const std::pair< int, int > xrange, const std::pair< int, int >yrange )
  13. : m_bar_width( 10 ),
  14. m_width_min( xrange.first ),
  15. m_width_max( xrange.second ),
  16. m_height_max( yrange.second ),
  17. m_height_min( yrange.first )
  18. {
  19. create_and_init_bars();
  20. }
  21. void print( const char marker = '*')
  22. {
  23.  
  24. int fwidth = get_format_width();
  25.  
  26. for( int height = 0; height <= m_height_max; height++ )
  27. {
  28. printf( "%*d|",fwidth , m_height_max - height );
  29. if ( height != m_height_max )
  30. {
  31. for ( int width = 0; width < m_bars.size(); width++ )
  32. {
  33. printf( "%*.c", fwidth + 1, ( m_bars[width] < m_height_max - height ? ' ' : marker ) );
  34. }
  35. std::cout << std::endl;
  36. }
  37. else
  38. {
  39.  
  40. for ( int width = 0; width <= m_bars.size(); width++ )
  41. {
  42. printf("%*d ", fwidth, m_width_min + ( width * m_bar_width ) );
  43. }
  44. }
  45. }
  46.  
  47.  
  48. }
  49. void set_height( const std::pair< int, int > range )
  50. {
  51. m_height_max = range.second;
  52. m_height_min = range.first;
  53. }
  54. void set_width( const std::pair< int, int > range )
  55. {
  56. m_width_max = range.second;
  57. m_width_min = range.first;
  58. create_and_init_bars();
  59. }
  60.  
  61. void add_data( std::vector<std::pair<int,int>> values )
  62. {
  63. std::vector<std::pair<int, int>>::iterator it;
  64. for( it = values.begin(); it != values.end(); it++ )
  65. {
  66. add_data( *it );
  67. }
  68. }
  69. void add_data( std::pair<int, int> data )
  70. {
  71. add_data( data.first, data.second );
  72. }
  73.  
  74. protected:
  75. int calculate_bar( const int number ) const { return ( number - m_width_min ) / m_bar_width; }
  76. void create_and_init_bars( void )
  77. {
  78. m_bars.clear();
  79. int barn = ( m_width_max - m_width_min ) / m_bar_width;
  80. for( int i = 0; i < barn; i++ )
  81. {
  82. m_bars.push_back( 0 );
  83. }
  84. }
  85. int get_format_width( void )
  86. {
  87. int max = m_width_max;
  88. int rc = 0;
  89. while( max > 0)
  90. {
  91. rc++;
  92. max /= 10;
  93. }
  94. return rc;
  95. }
  96. void add_data( const int bar, const int frequency )
  97. {
  98. m_bars[ calculate_bar( bar ) ] += frequency;
  99. }
  100. const int m_bar_width;
  101. int m_height_min,
  102. m_height_max,
  103. m_width_min,
  104. m_width_max;
  105. std::vector<int> m_bars;
  106.  
  107. };
  108.  
  109. int main ()
  110. {
  111. histogram_t h( { 0, 50 }, { 1,10 } );
  112. h.add_data({ { 0, 1 }, { 10, 3 }, { 20, 6 }, { 30, 4 }, { 40, 2 } } );
  113. h.print( '|' );
  114. return 0;
  115. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10|               
 9|               
 8|               
 7|               
 6|        |      
 5|        |      
 4|        |  |   
 3|     |  |  |   
 2|     |  |  |  |
 1|  |  |  |  |  |
 0| 0 10 20 30 40 50