Родитель
							
								
									598ed6dc81
								
							
						
					
					
						Сommit
						6ad8baad0f
					
				| @ -1,39 +1,41 @@ | ||||
| #include "histogram.h" | ||||
| #include <vector> | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| void find_minmax(const vector<double>& numbers, double& min, double& max) { | ||||
|     if (numbers.empty()) { | ||||
|     if (numbers.empty()) | ||||
|     { | ||||
|         min = 0; | ||||
|         max = 0; | ||||
|         return; | ||||
|     } | ||||
|     min = numbers[0]; | ||||
|     max = numbers[0]; | ||||
|     for (float x : numbers) { | ||||
|     min = numbers[0]; | ||||
|     for (double x : numbers) { | ||||
|         if (x < min) min = x; | ||||
|         else if (x > max) max = x; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) { | ||||
| vector<size_t> make_histogram(vector<double> numbers, size_t bin_count) { | ||||
|     double min, max; | ||||
|     find_minmax(numbers, min, max); | ||||
|     double bin_size = (max - min) / bin_count; | ||||
|     vector<size_t> bins(bin_count); | ||||
| 
 | ||||
|     float k = (max - min) / bin_count; | ||||
|     vector<size_t> bins(bin_count, 0); | ||||
| 
 | ||||
|     for (double number : numbers) { | ||||
|         bool flag = false; | ||||
|         for (size_t j = 0; (j < bin_count && !flag); j++) { | ||||
|             if (number >= (min + k * j) && number < (min + k * (j + 1))) { | ||||
|     for (size_t i = 0; i < numbers.size(); i++) { | ||||
|         bool found = false; | ||||
|         for (size_t j = 0; (j < bin_count - 1) && !found; j++) { | ||||
|             auto lo = min + j * bin_size; | ||||
|             auto hi = min + (j + 1) * bin_size; | ||||
|             if ((lo <= numbers[i]) && (numbers[i] < hi)) { | ||||
|                 bins[j]++; | ||||
|                 flag = true; | ||||
|                 found = true; | ||||
|             } | ||||
|         } | ||||
|         if (!flag) bins[bin_count - 1]++; | ||||
|         if (!found) { | ||||
|             bins[bin_count - 1]++; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return bins; | ||||
| } | ||||
|  | ||||
| @ -1,4 +1,9 @@ | ||||
| #pragma once | ||||
| #ifndef HISTOGRAM_H_INCLUDED | ||||
| #define HISTOGRAM_H_INCLUDED | ||||
| 
 | ||||
| #include <vector> | ||||
| using namespace std; | ||||
| 
 | ||||
| vector<size_t> make_histogram(vector<double> numbers, size_t bin_count); | ||||
| 
 | ||||
| std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count); | ||||
| #endif | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| #pragma once | ||||
| #include <vector> | ||||
| #ifndef HISTOGRAM_INTERNAL_H_INCLUDED | ||||
| #define HISTOGRAM_INTERNAL_H_INCLUDED | ||||
| 
 | ||||
| void find_minmax(const std::vector<double>& numbers, double& min, double& max); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -1,42 +1,35 @@ | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include "histogram.h" | ||||
| #include "text.h" | ||||
| #include "svg.h" | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| // Ñòðóêòóðà äëÿ õðàíåíèÿ âõîäíûõ äàííûõ
 | ||||
| struct Input { | ||||
|     vector<double> numbers; | ||||
|     size_t bin_count{}; | ||||
|     size_t bin_count; | ||||
| }; | ||||
| 
 | ||||
| // Ôóíêöèÿ ââîäà äàííûõ
 | ||||
| Input input_data() { | ||||
|     Input in; | ||||
|     int number_count; | ||||
| 
 | ||||
|     size_t number_count; | ||||
|     cerr << "Enter number count: "; | ||||
|     cin >> number_count; | ||||
|     while (number_count < 1) { | ||||
|         cin >> number_count; | ||||
|     } | ||||
| 
 | ||||
|     cin >> in.bin_count; | ||||
|     while (in.bin_count < 1) { | ||||
|         cin >> in.bin_count; | ||||
|     } | ||||
| 
 | ||||
|     in.numbers.resize(number_count); | ||||
|     for (int i = 0; i < number_count; i++) { | ||||
|     cerr << "Enter numbers: "; | ||||
|     for (size_t i = 0; i < number_count; i++) { | ||||
|         cin >> in.numbers[i]; | ||||
|     } | ||||
| 
 | ||||
|     cerr << "Enter bin count: "; | ||||
|     cin >> in.bin_count; | ||||
|     return in; | ||||
| } | ||||
| 
 | ||||
| // Ãëàâíàÿ ôóíêöèÿ
 | ||||
| int main() { | ||||
|     auto in = input_data(); // Ââîä äàííûõ
 | ||||
|     auto bins = make_histogram(in.numbers, in.bin_count); // Ðàñ÷¸ò ãèñòîãðàììû
 | ||||
|     show_histogram_svg(bins); // Âûâîä ãèñòîãðàììû â ôîðìàòå SVG
 | ||||
|     auto in = input_data(); | ||||
|     auto bins = make_histogram(in.numbers, in.bin_count); | ||||
|     show_histogram_svg(bins); | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,8 @@ | ||||
| #pragma once | ||||
| #include <vector> | ||||
| #include <string> | ||||
| #ifndef SVG_H_INCLUDED | ||||
| #define SVG_H_INCLUDED | ||||
| 
 | ||||
| void svg_begin(double width, double height); | ||||
| void svg_end(); | ||||
| void svg_text(double left, double baseline, const std::string& text); | ||||
| void svg_rect(double x, double y, double width, double height, | ||||
|               const std::string& stroke = "black", | ||||
|               const std::string& fill = "black"); | ||||
| #include <vector> | ||||
| #include <cstddef> // | ||||
| void show_histogram_svg(const std::vector<size_t>& bins); | ||||
| 
 | ||||
| #endif // SVG_H_INCLUDED
 | ||||
|  | ||||
| @ -1,33 +1,37 @@ | ||||
| #include "text.h" | ||||
| #include <iostream> | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| void show_histogram_text(const vector<size_t>& bins) { | ||||
| void show_histogram_text(vector<size_t> bins, size_t bin_count) { | ||||
|     const size_t SCREEN_WIDTH = 80; | ||||
|     const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; | ||||
| 
 | ||||
|     size_t max_count = 0; | ||||
|     for (size_t count : bins) { | ||||
|         if (count > max_count) { | ||||
|             max_count = count; | ||||
|     double max_bin; | ||||
|     size_t height; | ||||
|     max_bin = bins[0]; | ||||
|     height = bins[0]; | ||||
|     for (size_t i = 0; i < bin_count; i++) { | ||||
|         if (bins[i] > max_bin) { | ||||
|             max_bin = bins[i]; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     for (size_t bin : bins) { | ||||
|         if (bin < 100) cout << " "; | ||||
|         if (bin < 10) cout << " "; | ||||
|         cout << bin << "|"; | ||||
| 
 | ||||
|         size_t height = bin; | ||||
|         if (max_count > MAX_ASTERISK) { | ||||
|             if (max_count != bin) | ||||
|                 height = MAX_ASTERISK * (static_cast<float>(bin) / max_count); | ||||
|             else | ||||
|                 height = MAX_ASTERISK; | ||||
|     bool flag = false; | ||||
|     if (max_bin > 80) { | ||||
|         flag = true; | ||||
|     } | ||||
|     for (size_t i = 0; i < bin_count; i++) { | ||||
|         if (bins[i] < 100) cout << " "; | ||||
|         if (bins[i] < 10) cout << " "; | ||||
|         cout << bins[i] << "|"; | ||||
|         if (flag == true) { | ||||
|             height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin); | ||||
|         } | ||||
| 
 | ||||
|         for (size_t i = 0; i < height; i++) cout << "*"; | ||||
|         cout << "\n"; | ||||
|         else { | ||||
|             height = bins[i]; | ||||
|         } | ||||
|         for (size_t j = 0; j < height; j++) { | ||||
|             cout << "*"; | ||||
|         } | ||||
|         cout << endl; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,4 +1,9 @@ | ||||
| #pragma once | ||||
| #ifndef TEXT_H_INCLUDED | ||||
| #define TEXT_H_INCLUDED | ||||
| 
 | ||||
| #include <vector> | ||||
| using namespace std; | ||||
| 
 | ||||
| void show_histogram_text(vector<size_t> bins, size_t bin_count); | ||||
| 
 | ||||
| void show_histogram_text(const std::vector<size_t>& bins); | ||||
| #endif | ||||
|  | ||||
					Загрузка…
					
					
				
		Ссылка в новой задаче
	
	 AndrosovDS
						AndrosovDS