|  |  |  | @ -1,12 +1,13 @@ | 
			
		
	
		
			
				
					|  |  |  |  | #include <iostream> | 
			
		
	
		
			
				
					|  |  |  |  | #include <cmath> | 
			
		
	
		
			
				
					|  |  |  |  | #include <vector> | 
			
		
	
		
			
				
					|  |  |  |  | using namespace std; | 
			
		
	
		
			
				
					|  |  |  |  | #include "histogram.h" | 
			
		
	
		
			
				
					|  |  |  |  | #include "text.h" | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | const size_t SCREEN_WIDTH = 80; | 
			
		
	
		
			
				
					|  |  |  |  | const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; | 
			
		
	
		
			
				
					|  |  |  |  | using namespace std; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | struct Input { | 
			
		
	
		
			
				
					|  |  |  |  | struct Input | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  |     vector<double> numbers; | 
			
		
	
		
			
				
					|  |  |  |  |     size_t bin_count{}; | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
	
		
			
				
					|  |  |  | @ -23,7 +24,8 @@ Input in; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     in.numbers.resize(number_count); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | for (size_t i = 0; i < number_count; i++) { | 
			
		
	
		
			
				
					|  |  |  |  |     for (size_t i = 0; i < number_count; i++) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |        cin >> in.numbers[i]; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -33,88 +35,11 @@ cin >> in.bin_count; | 
			
		
	
		
			
				
					|  |  |  |  |     return in; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | void | 
			
		
	
		
			
				
					|  |  |  |  | find_minmax(const vector<double>& numbers, double &min, double &max) | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     min = numbers[0]; | 
			
		
	
		
			
				
					|  |  |  |  |     max = numbers[0]; | 
			
		
	
		
			
				
					|  |  |  |  |     for (size_t i = 0; i < numbers.size(); i++) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         if (numbers[i] > max) | 
			
		
	
		
			
				
					|  |  |  |  |             max = numbers[i]; | 
			
		
	
		
			
				
					|  |  |  |  |         if (numbers[i] < min) | 
			
		
	
		
			
				
					|  |  |  |  |             min = numbers[i]; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | vector <size_t> make_histogram (const vector<double>& numbers, size_t &bin_count) | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  |    vector <size_t> bins(bin_count); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |    double min, max; | 
			
		
	
		
			
				
					|  |  |  |  |    float low, hi; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |    find_minmax(numbers, min, max); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |    double bin_size = (max - min) / bin_count; | 
			
		
	
		
			
				
					|  |  |  |  |    low = min; | 
			
		
	
		
			
				
					|  |  |  |  |     hi = low + bin_size; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     for (size_t i = 0; i < numbers.size(); i++) | 
			
		
	
		
			
				
					|  |  |  |  |     { | 
			
		
	
		
			
				
					|  |  |  |  |         bool found = false; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         for (size_t j = 0; (j < bin_count - 1) && !found; j++) | 
			
		
	
		
			
				
					|  |  |  |  |         { | 
			
		
	
		
			
				
					|  |  |  |  |             low = min + j * bin_size; | 
			
		
	
		
			
				
					|  |  |  |  |             hi = min + (j + 1) * bin_size; | 
			
		
	
		
			
				
					|  |  |  |  |             if ((low <= numbers[i]) && (numbers[i] < hi)) | 
			
		
	
		
			
				
					|  |  |  |  |             { | 
			
		
	
		
			
				
					|  |  |  |  |                 bins[j]++; | 
			
		
	
		
			
				
					|  |  |  |  |                 found = true; | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         if (found==false) | 
			
		
	
		
			
				
					|  |  |  |  |         { | 
			
		
	
		
			
				
					|  |  |  |  |             bins[bin_count - 1]++; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     return bins; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | void show_histogram_text(const vector<size_t>& bins, size_t &bin_count) | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | size_t max_bin = bins[0]; | 
			
		
	
		
			
				
					|  |  |  |  | for (size_t bin : bins) | 
			
		
	
		
			
				
					|  |  |  |  | if (bin > max_bin) | 
			
		
	
		
			
				
					|  |  |  |  | max_bin = bin; | 
			
		
	
		
			
				
					|  |  |  |  | for (size_t bin : bins) | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  | int height = bin; | 
			
		
	
		
			
				
					|  |  |  |  | if (max_bin > MAX_ASTERISK) | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  | height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin); | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | if (bin < 100) | 
			
		
	
		
			
				
					|  |  |  |  | cout << " "; | 
			
		
	
		
			
				
					|  |  |  |  | if (bin < 10) | 
			
		
	
		
			
				
					|  |  |  |  | cout << " "; | 
			
		
	
		
			
				
					|  |  |  |  | cout << bin << "|"; | 
			
		
	
		
			
				
					|  |  |  |  | for (int i = 0; i < height; i++) | 
			
		
	
		
			
				
					|  |  |  |  | cout << "*"; | 
			
		
	
		
			
				
					|  |  |  |  | cout << endl; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | int main() | 
			
		
	
		
			
				
					|  |  |  |  | { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     auto in = input_data(); | 
			
		
	
		
			
				
					|  |  |  |  |     auto bins = make_histogram(in.numbers, in.bin_count); | 
			
		
	
		
			
				
					|  |  |  |  |     show_histogram_text(bins, in.bin_count); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return 0; | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
	
		
			
				
					|  |  |  | 
 |