Сравнить коммиты
	
		
			6 Коммитов 
		
	
	
		
			ff46d5dc4c
			...
			b8ad5f3fa4
		
	
	| Автор | SHA1 | Дата | 
|---|---|---|
|  | b8ad5f3fa4 | 2 лет назад | 
|  | 1e34abf21f | 2 лет назад | 
|  | a31eb35d22 | 2 лет назад | 
|  | 4b0b829ebf | 2 лет назад | 
|  | 4b97489ed3 | 2 лет назад | 
|  | 585d877931 | 2 лет назад | 
											
												
													Разница между файлами не показана из-за своего большого размера
													Загрузить разницу
												
											
										
									
								| @ -0,0 +1,43 @@ | |||||||
|  | #include <vector> | ||||||
|  | #include "histogram.h" | ||||||
|  | #include "histogram_internal.h" | ||||||
|  | using namespace std; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | void find_minmax(const vector<double>& numbers, double& min, double& max) { | ||||||
|  |     max = 0; | ||||||
|  |     if (numbers.size() != 0) { | ||||||
|  |         min = numbers[0]; | ||||||
|  |         max = numbers[0]; | ||||||
|  |         for (auto el : numbers) { | ||||||
|  |             if (el > max) { | ||||||
|  |                 max = el; | ||||||
|  |             } | ||||||
|  |             if (el < min) { | ||||||
|  |                 min = el; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } else {min = 0;} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) { | ||||||
|  |     vector<size_t> bins(bin_count); | ||||||
|  |     double max, min; | ||||||
|  |     find_minmax(numbers, min, max); | ||||||
|  |     double binSize = (max - min) / bin_count; | ||||||
|  |     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 * binSize; | ||||||
|  |             auto hi = min + (j + 1) * binSize; | ||||||
|  |             if ((lo <= numbers[i]) && (numbers[i] < hi)) { | ||||||
|  |                 bins[j]++; | ||||||
|  |                 found = true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if (!found) { | ||||||
|  |             bins[bin_count - 1]++; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return bins; | ||||||
|  | } | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | #pragma once | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count); | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | #pragma once | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | void find_minmax(const std::vector<double>& numbers, double& min, double& max); | ||||||
| @ -1,67 +1,32 @@ | |||||||
| 
 |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <vector> | #include <vector> | ||||||
|  | #include "histogram.h" | ||||||
|  | #include "text.h" | ||||||
|  | #include "svg.h" | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| using namespace std; | using namespace std; | ||||||
| 
 | 
 | ||||||
| const size_t SCREEN_WIDTH = 80; | struct Input { | ||||||
| const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; |     vector<double> numbers; | ||||||
|  |     size_t bin_count{}; | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| int main() { | Input input_data() { | ||||||
|     size_t numberCount, binCount, maxCount; |     Input in; | ||||||
|     double maxx, minn; |     size_t number_count; | ||||||
|     cerr << "Enter number count: "; |     cin >> number_count; | ||||||
|     cin >> numberCount; |     in.numbers.resize(number_count); | ||||||
|     vector<double> numbers(numberCount); |     for (size_t i = 0; i < number_count; i++) { | ||||||
|     cerr << "Fill array \"numbers\"\n"; |         cin >> in.numbers[i]; | ||||||
|     for (int i = 0; i < numberCount; i++) { |  | ||||||
|         cin >> numbers[i]; |  | ||||||
|     } |  | ||||||
|     maxx = numbers[0]; |  | ||||||
|     minn = numbers[0]; |  | ||||||
|     for (auto el: numbers) { |  | ||||||
|         if (el > maxx) { |  | ||||||
|             maxx = el; |  | ||||||
|         } |  | ||||||
|         if (el < minn) { |  | ||||||
|             minn = el; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     cerr << "Enter bin count: "; |  | ||||||
|     cin >> binCount; |  | ||||||
|     double binSize = (maxx - minn) / binCount; |  | ||||||
|     vector<size_t> bins(binCount); |  | ||||||
|     maxCount = 0; |  | ||||||
|     for (size_t i = 0; i < numberCount; i++) { |  | ||||||
|     bool found = false; |  | ||||||
|     for (size_t j = 0; (j < binCount - 1) && !found; j++) { |  | ||||||
|         auto lo = minn + j * binSize; |  | ||||||
|         auto hi = minn + (j + 1) * binSize; |  | ||||||
|         if ((lo <= numbers[i]) && (numbers[i] < hi)) { |  | ||||||
|             bins[j]++; |  | ||||||
|             found = true; |  | ||||||
|             if (bins[j] > maxCount) { |  | ||||||
|                 maxCount = bins[j]; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|        if (!found) { |  | ||||||
|         bins[binCount - 1]++; |  | ||||||
|         if (bins[binCount - 1] > maxCount) { |  | ||||||
|                 maxCount = bins[binCount - 1]; |  | ||||||
|             } |  | ||||||
|     } |  | ||||||
|     } |  | ||||||
|     for (auto bin: bins) { |  | ||||||
|         if (bin < 100) { |  | ||||||
|             cout << " "; |  | ||||||
|         } |  | ||||||
|         if (bin < 10) { |  | ||||||
|             cout << " "; |  | ||||||
|         } |  | ||||||
|         size_t height = maxCount < MAX_ASTERISK? bin: MAX_ASTERISK * (static_cast<double>(bin) / maxCount); |  | ||||||
|         cout << bin << "|" << string(height, '*'); |  | ||||||
|         cout << "\n"; |  | ||||||
|     } |     } | ||||||
|  |     cin >> in.bin_count; | ||||||
|  |     return in; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     Input in = input_data(); | ||||||
|  |     auto bins = make_histogram(in.numbers, in.bin_count); | ||||||
|  |     show_histogram_svg(bins); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| @ -0,0 +1,61 @@ | |||||||
|  | #include <iostream> | ||||||
|  | #include "svg.h" | ||||||
|  | 
 | ||||||
|  | using namespace std; | ||||||
|  | 
 | ||||||
|  | const auto IMAGE_WIDTH = 400; | ||||||
|  | const auto IMAGE_HEIGHT = 310; | ||||||
|  | const auto TEXT_LEFT = 20; | ||||||
|  | const auto TEXT_BASELINE = 20; | ||||||
|  | const auto TEXT_WIDTH = 50; | ||||||
|  | const auto BIN_HEIGHT = 30; | ||||||
|  | const auto BLOCK_WIDTH = 10; | ||||||
|  | 
 | ||||||
|  | void svg_begin(double width, double height) { | ||||||
|  |     cout << "<?xml version='1.0' encoding='UTF-8'?>\n"; | ||||||
|  |     cout << "<svg "; | ||||||
|  |     cout << "width='" << width << "' "; | ||||||
|  |     cout << "height='" << height << "' "; | ||||||
|  |     cout << "viewBox='0 0 " << width << " " << height << "' "; | ||||||
|  |     cout << "xmlns='http://www.w3.org/2000/svg'>\n"; | ||||||
|  |     cout << "<line x1='10' y1='2' x2='" << width + 10 <<"' y2='2' stroke-dasharray = '10 10' stroke='black'/>\n"; | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void svg_end(double top) { | ||||||
|  |     cout << "<line x1='10' y1='" << top << "' x2='" << IMAGE_WIDTH <<"' y2='" << top << "' stroke-dasharray = '10 10' stroke='black'/>\n"; | ||||||
|  |     cout << "</svg>\n"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void svg_text(double left, double baseline, string text) { | ||||||
|  |     cout << "<line x1='10' y1='" << baseline -  TEXT_BASELINE << "' x2='10' y2='" << baseline <<"' stroke-dasharray = '10 10' stroke='black'/>\n"; | ||||||
|  |     cout << "<text x='" << left << "' y='" << baseline << "'>" << text <<"</text>\n"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { | ||||||
|  |     cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' stroke='" << stroke << "' height='" << height << "' fill='" << fill << "' />\n"; | ||||||
|  |     cout << "<line x1='" << IMAGE_WIDTH <<"' y1='" << y << "' x2='" << IMAGE_WIDTH <<"' y2='" << y + height <<"' stroke-dasharray = '10 10' stroke='black'/>\n"; | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | void show_histogram_svg(const vector<size_t>& bins) { | ||||||
|  |     double max = 0, scale = 1; | ||||||
|  |     int maxlen = IMAGE_WIDTH - TEXT_WIDTH; | ||||||
|  |     for (auto el: bins) { | ||||||
|  |         if (max < el) { | ||||||
|  |             max = el; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); | ||||||
|  |     double top = 5; | ||||||
|  |     scale = max * BLOCK_WIDTH / maxlen; | ||||||
|  |     for (size_t bin : bins) { | ||||||
|  |         const double bin_width = BLOCK_WIDTH * bin; | ||||||
|  |         svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); | ||||||
|  |         svg_rect(TEXT_WIDTH, top, bin_width / scale, BIN_HEIGHT); | ||||||
|  |         top += BIN_HEIGHT; | ||||||
|  |     } | ||||||
|  |     svg_end(top); | ||||||
|  | } | ||||||
| @ -0,0 +1,3 @@ | |||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | void show_histogram_svg(const std::vector<size_t>& bins); | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | #include <iostream> | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | using namespace std; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | const size_t SCREEN_WIDTH = 80; | ||||||
|  | const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; | ||||||
|  | 
 | ||||||
|  | void show_histogram_text(const vector<size_t>& bins) { | ||||||
|  |     size_t maxCount = bins[0]; | ||||||
|  |     for (auto bin : bins) { | ||||||
|  |         if (maxCount < bin) { | ||||||
|  |             maxCount = bin; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for (auto bin : bins) { | ||||||
|  |         if (bin < 100) | ||||||
|  |         { | ||||||
|  |             cout << " "; | ||||||
|  |         } | ||||||
|  |         if (bin < 10) | ||||||
|  |         { | ||||||
|  |             cout << " "; | ||||||
|  |         } | ||||||
|  |         size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount); | ||||||
|  |         cout << bin << "|" << string(height, '*'); | ||||||
|  |         cout << "\n"; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | #pragma once | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | void show_histogram_text(const std::vector<size_t>& bins); | ||||||
| @ -0,0 +1,36 @@ | |||||||
|  | #define DOCTEST_CONFIG_NO_MULTITHREADING | ||||||
|  | #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN | ||||||
|  | #include "doctest.h" | ||||||
|  | #include "histogram_internal.h" | ||||||
|  | 
 | ||||||
|  | TEST_CASE("distinct positive numbers") { | ||||||
|  |     double min = 0; | ||||||
|  |     double max = 0; | ||||||
|  |     find_minmax({1, 2}, min, max); | ||||||
|  |     CHECK(min == 1); | ||||||
|  |     CHECK(max == 2); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TEST_CASE("empty vector") { | ||||||
|  |     double min = 0; | ||||||
|  |     double max = 0; | ||||||
|  |     find_minmax({ }, min, max); | ||||||
|  |     CHECK(min != 0); | ||||||
|  |     CHECK(max != 0); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TEST_CASE("one element") { | ||||||
|  |     double min = 0; | ||||||
|  |     double max = 0; | ||||||
|  |     find_minmax({ 1 }, min, max); | ||||||
|  |     CHECK(min == 1); | ||||||
|  |     CHECK(max == 1); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TEST_CASE("negative elements") { | ||||||
|  |     double min = 0; | ||||||
|  |     double max = 0; | ||||||
|  |     find_minmax({ -81, 2 }, min, max); | ||||||
|  |     CHECK(min == -81); | ||||||
|  |     CHECK(max == 2); | ||||||
|  | } | ||||||
					Загрузка…
					
					
				
		Ссылка в новой задаче