diff --git a/cs-lab34.cbp b/cs-lab34.cbp index 49b0653..9b59dbb 100644 --- a/cs-lab34.cbp +++ b/cs-lab34.cbp @@ -32,7 +32,14 @@ + + + + + + + diff --git a/histogram.cpp b/histogram.cpp index ae048fb..0b25335 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,16 +1,21 @@ #include "histogram.h" -void find_minmax(vector vec, double& min, double& max) { - min = vec[0]; - max = vec[0]; - for (double x : vec) { - if (x < min) { - min = x; - } - else if (x > max) - { - max = x; - } +#include "histogram_internal.h" +bool find_minmax(vector vec, double& min, double& max) { + if(vec.size() == 0){ + return false; } + min = vec[0]; + max = vec[0]; + for (double x : vec) { + if (x < min) { + min = x; + } + else if (x > max) + { + max = x; + } + } + return true; } vector make_histogram(size_t number, vector vec) { vector bins(number); diff --git a/histogram_internal.h b/histogram_internal.h index 819c07f..6631db5 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1 +1 @@ -void find_minmax(std::vector vec, double& min, double& max); +bool find_minmax(const std::vector vec, double& min, double& max); diff --git a/main.cpp b/main.cpp index 48bcd53..e108c01 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "histogram.h" #include "text.h" +#include "svg.h" struct Input { vector vec; size_t korz{}; @@ -20,5 +21,5 @@ Input input_data() { int main() { auto in = input_data(); auto bins = make_histogram(in.korz, in.vec); - show_histogram(bins); + show_histogram_svg(bins); } diff --git a/svg.cpp b/svg.cpp index 2c5a7c3..f763352 100644 --- a/svg.cpp +++ b/svg.cpp @@ -45,7 +45,7 @@ show_histogram_svg(const vector& bins) const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; const auto BLACK = "black"; - const auto RED = "red"; + const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; @@ -63,9 +63,10 @@ show_histogram_svg(const vector& bins) for (size_t bin : bins) { + const auto RED = "#" + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count))); double bin_width = (MAX_WIDTH)*(bin/max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK,RED); top += BIN_HEIGHT; } diff --git a/text.cpp b/text.cpp index 57e803a..03aa0c5 100644 --- a/text.cpp +++ b/text.cpp @@ -1,83 +1,44 @@ +#include +#include #include "text.h" -void show_histogram(std::vector bins) { - bool gigant = false; - auto spaces = 0; - size_t mx_count = 0; - for (auto x : bins) { - if (x > 76) { - gigant = true; - } - if (x > mx_count) { - mx_count = x; - } - auto len = 0; - while (x > 0) { - x /= 10; - len++; - } - if (len > spaces) { - spaces = len; - } +using namespace std; - } - if (spaces == 1) { - for (size_t i = 0; i < bins.size(); i++) { - std::cout << " " << bins[i] << "|"; - if (gigant) { - if (bins[i] == mx_count) { - for (size_t j = 0; j < 76; j++) { - std::cout << "*"; - } - } - else - { - for (size_t j = 0; j < 76 * static_cast(bins[i]) / mx_count; j++) { - std::cout << "*"; - } - } - } - else - { - for (size_t j = 0; j < bins[i]; j++) { - std::cout << "*"; - } - std::cout << std::endl; - } +void show_histogram_text(vector bins, size_t bin_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + + size_t max_bin = bins[0]; + for(size_t i = 0; i < bin_count; i++) + { + if(bins[i] > max_bin) + { + max_bin = bins[i]; } } - else + + for (size_t bin: bins) { - for (size_t i = 0; i < bins.size(); i++) { - int len = 1; - int k = bins[i]; - for (; k /= 10; ++len); - while (len < spaces) { - std::cout << " "; - len++; - } - std::cout << bins[i]; - std::cout << "|"; - if (gigant) { - if (bins[i] == mx_count) { - for (size_t j = 0; j < 76; j++) { - std::cout << "*"; - } - } - else - { - for (size_t j = 0; j < (76 * static_cast(bins[i]) / mx_count - 1); j++) { - std::cout << "*"; - } - } - } - else - { - for (size_t j = 0; j < bins[i]; j++) { - std::cout << "*"; - } - } - std::cout << std::endl; + size_t height = bin; + + if (max_bin > MAX_ASTERISK) + { + height = MAX_ASTERISK * (static_cast(bin) / max_bin); + } + + if (bin < 100) + { + cout << ' '; + } + if (bin < 10) + { + cout << ' '; + } + cout << bin << "|"; + for(size_t i = 0; i < height; i++) + { + cout << "*"; } + cout << endl; } } diff --git a/text.h b/text.h index 2bf2f23..ab14ac3 100644 --- a/text.h +++ b/text.h @@ -1,3 +1,3 @@ #include #include -void show_histogram(std::vector bins); +void show_histogram_text(std::vector bins);