diff --git a/histogram.cpp b/histogram.cpp index 268ee01..3e35c3e 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,47 +1,43 @@ #include "histogram.h" #include +#include using namespace std; - void find_minmax(const vector& numbers, double& min, double& max) { - if (numbers.empty()) - { - min = 0; - max = 0; - return; - } + if (numbers.empty()) return; max = numbers[0]; min = numbers[0]; for (double x : numbers) { if (x < min) min = x; - else if (x > max) max = x; + if (x > max) max = x; } } - -vector make_histogram (vector numbers, size_t bin_count) -{ +vector make_histogram(vector numbers, size_t bin_count) { double min, max; - find_minmax (numbers, min, max); + find_minmax(numbers, min, max); double bin_size = (max - min) / bin_count; vector bins(bin_count); - for (size_t i = 0; i < numbers.size(); i++) - { + + for (size_t i = 0; i < numbers.size(); i++) { bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) - { + 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)) - { + if ((lo <= numbers[i]) && (numbers[i] < hi)) { bins[j]++; found = true; } } - if (!found) - { + if (!found) { bins[bin_count - 1]++; } } return bins; } + +double calculate_average_height(const vector& bins) { + if (bins.empty()) return 0.0; + double sum = accumulate(bins.begin(), bins.end(), 0.0); + return sum / bins.size(); +} diff --git a/histogram.h b/histogram.h index 1ee7f4d..4821faa 100644 --- a/histogram.h +++ b/histogram.h @@ -1,9 +1,8 @@ #ifndef HISTOGRAM_H_INCLUDED #define HISTOGRAM_H_INCLUDED - #include using namespace std; - vector make_histogram(vector numbers, size_t bin_count); +double calculate_average_height(const vector& bins); #endif diff --git a/lab03.cbp b/lab03.cbp index ee5e5bc..a413538 100644 --- a/lab03.cbp +++ b/lab03.cbp @@ -37,9 +37,10 @@ + + - diff --git a/main.cpp b/main.cpp index 584252c..8f132ec 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" +#include "histogram_internal.h" using namespace std; @@ -28,12 +30,8 @@ Input input_data(){ int main() { - if (in.numbers.empty()) { - cerr << "Error: The data set is empty." << endl; - return EXIT_FAILURE; - } auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins, in.bin_count); + show_histogram_svg(bins); return 0; } diff --git a/svg.cpp b/svg.cpp index f17f5a8..e6c6c64 100644 --- a/svg.cpp +++ b/svg.cpp @@ -2,7 +2,7 @@ #include #include #include "svg.h" - +#include "histogram.h" using namespace std; void svg_begin(double width, double height) { @@ -22,7 +22,7 @@ void svg_text(double left, double baseline, string text) { cout << "" << text << "\n"; } -void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { +void svg_rect(double x, double y, double width, double height, string stroke, string fill) { cout << "\n"; } @@ -51,10 +51,13 @@ void show_histogram_svg(const vector& bins) { } } + double avg_height = calculate_average_height(bins); + for (size_t bin : bins) { const double bin_width = MAX_WIDTH * (static_cast(bin) / max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "red"); + string fill_color = (bin > avg_height) ? "red" : "green"; + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", fill_color); top += BIN_HEIGHT; } diff --git a/svg.h b/svg.h index 44245d0..7b20dc0 100644 --- a/svg.h +++ b/svg.h @@ -1,9 +1,8 @@ #ifndef SVG_H_INCLUDED #define SVG_H_INCLUDED - #include #include void show_histogram_svg(const std::vector& bins); -#endif // SVG_H_INCLUDED +#endif